6 Replies - 3125 Views - Last Post: 22 August 2010 - 02:19 PM Rate Topic: -----

#1 Guest_Anna*


Reputation:

Java Final Project

Posted 21 August 2010 - 06:27 PM

Need help asap!! My final is due tomorrow I have been glued to it all week and cant seem to figure out how to fix it... it doesnt show the results...please help me!!!!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FinalProject extends JFrame implements ActionListener
{
    JLabel textPrompt;
    JTextArea inputText;
    JLabel keyPrompt;
    JTextField inputKey;
    Container container;
    
    public FinalProject()
    {
        super( "Final Project " );
        JTabbedPane tab = new JTabbedPane();

        // constructing the Pool Panel

        JPanel p1 = new JPanel();
        JLabel label4,label5,lable6;
        final JTextField text4,text5,text6,text13;
        label4 = new JLabel();
        label4.setText("Height:");
        p1.add(label4);
        text4 = new JTextField(20);
        p1.add(text4);
        JLabel space3 = new JLabel("                                 ");
        p1.add(space3);
        label5 = new JLabel();
        label5.setText("Width:");
        p1.add (label5);
        text5 = new JTextField(20);
        p1.add(text5);
        JLabel space5 = new JLabel("                           ");
        p1.add(space5);
        lable6 = new JLabel();
        lable6.setText("Depth:");
        p1.add(lable6);
        text6 = new JTextField(20);
        p1.add(text6);
        JLabel space2 = new JLabel(" ");
        p1.add(space2);
        JButton button = new JButton("Enter");
        p1.add(button);
        tab.addTab( "Pool", null, p1, " Pool" );
        text13 = new JTextField();
        p1.add(text13);
        button.addActionListener(this);
        
        new ActionListener(){
            public void actionPerformed(ActionEvent e) {
e.getActionCommand();
                int length=Integer.parseInt(text4.getText());
                int width=Integer.parseInt(text5.getText());
                int depth=Integer.parseInt(text6.getText());
                
                if (e.getActionCommand().equals("Enter")) {
                    int volume = ((length * width) * depth);
                    JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
                }
                }
        };
// constructing the Hot Tube panel

        new JLabel("Hot Tube");
        JPanel p2 = new JPanel();
        JLabel label7,label8,lable9;
        final JTextField text7,text8,text9;{
            label7 = new JLabel();
            label7.setText("Height:");
            p2.add(label7);
            text7 = new JTextField(20);
            p2.add(text7);
            JLabel space6 = new JLabel("                       ");
            p2.add(space6);
            label8 = new JLabel();
            label8.setText("Width:");
            p2.add(label8);
            text8 = new JTextField(20);
            p2.add(text8);
            JLabel space7 = new JLabel("                       ");
            p2.add(space7);
            lable9 = new JLabel();
            lable9.setText("Depth:");
            p2.add(lable9);
            text9 = new JTextField(20);
            p2.add(text9);
            JLabel space8 = new JLabel("              ");
            p2.add(space8);
            JButton rbutton = new JButton("Oval");
            p2.add(rbutton);
            JButton rbutton1 = new JButton ("Round");
            p2.add(rbutton1);

            tab.addTab( "Hot Tube", null, p2, " Hot Tube" );
            rbutton.addActionListener(this);
            rbutton1.addActionListener(this);

            new ActionListener(){
                public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
                    
                    int length=Integer.parseInt(text7.getText());
                    int width=Integer.parseInt(text8.getText());
                    int depth=Integer.parseInt(text9.getText());
                    
                    if(e.getActionCommand().equals("Oval")){
                        int volume = ((length * width) * depth);
                        JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"" +
                                "for your oval hot tube.", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else if(e.getActionCommand().equals("Round")){
                        int volume1= (((length/2)^2) * depth);
                        JOptionPane.showMessageDialog(null, "The Volume is:"+volume1,"" +
                                "for your round hot tube.", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            };
// constructing the Temperature Change panel

            new JLabel( " Temperature Change!" );
            JPanel p3 = new JPanel();
            JLabel label10;
            final JTextField text10;
            label10 = new JLabel();
            label10.setText("Temperature:");
            p3.add(label10);
            text10 = new JTextField(20);
            p3.add(text10);
            JButton button3 = new JButton("Enter");
            p3.add(button3);
            JCheckBox boxF = new JCheckBox("F");
            p3.add(boxF);
            JCheckBox boxC = new JCheckBox("C");
            p3.add(boxC);
            tab.addTab( "Temperature Change", null, p3, " Temperature Change" );
            button3.addActionListener(this);
            boxF.addActionListener(this);
            boxC.addActionListener(this);

            new ActionListener(){   
                public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
                    int temperature=Integer.parseInt(text10.getText());

                    if(e.getActionCommand().equals("C")){
                        int temp = (( temperature* 9)/5+32);
                        JOptionPane.showMessageDialog(null, "The Celcuis temperature is changed to:"
                                +temp,"" +
                                "in fahrenheit", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else if(e.getActionCommand().equals("F")){
                        int temp1= (((temperature - 32)/9)*5);
                        JOptionPane.showMessageDialog(null, "The farhrenheight temperature is changed to:"+temp1,""+
                                "in Celcius.", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            };
// constructing the Customer panel
            
            JPanel p4 = new JPanel();
            JLabel label1,label2;
            final JTextField text1,text2;
            label1 = new JLabel();
            label1.setText("Name:");
            p4.add(label1);
            text1 = new JTextField(20);
            p4.add(text1);
            JLabel space = new JLabel("                  ");//MDB
            p4.add(space);
            label2 = new JLabel();
            label2.setText("Address:");
            p4.add(label2);
            text2 = new JTextField(20);
            p4.add(text2);
            JButton button4 = new JButton("Enter");
            p4.add(button4);
            tab.addTab( "Customer", null, p4, " Customer" );
            button4.setActionCommand ("Enter");

            new ActionListener(){
                public void actionPerformed(ActionEvent e) {
e.getActionCommand();
                    int Name=Integer.parseInt(text1.getText()) ;
                    int Address=Integer.parseInt(text2.getText());
                    if (e.getActionCommand().equals("Enter")) { 
                        JOptionPane.showMessageDialog(null, "Customer's name is:"+Name,"Their address is:"
                                +Address, JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            };
// constructing the General panel

            new JLabel( "General" );
            JPanel p5 = new JPanel();
            Date date = new Date();
            SimpleDateFormat dateformatMMDDYYYY = new SimpleDateFormat("MM/dd/yyyy");
            new StringBuilder( dateformatMMDDYYYY.format( date ) );
            JLabel space11 = new JLabel(" ");
            p5.add(space11);
            JLabel space12 = new JLabel(" ");
            p5.add(space12);
            JLabel space13 = new JLabel(" ");
            p5.add(space13);
            JLabel label11;
            label11 = new JLabel();
            label11.setText("Today's Date is:");
            p5.add(label11);    
            JLabel texta1;
            texta1 = new JLabel();
            texta1.setText(dateformatMMDDYYYY.format(date));
            p5.add(texta1);
            tab.addTab( "General", null, p5, " Today's Date");

            //constructing the Manufacture panel
            
            JPanel p6 = new JPanel();
            JLabel label3;
            final JTextField text3;
            label3 = new JLabel();
            label3.setText("Manufacture:");
            p6.add(label3);
            text3 = new JTextField(20);
            p6.add(text3);
            JLabel space1 = new JLabel(" ");
            p6.add(space1);
            tab.addTab( "Manufacture", null, p6, " Manufacture" );
            JButton button6 = new JButton("Enter");
            p6.add(button6);
            button6.addActionListener(this);
            {
                button6.setActionCommand ("Enter");

                new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
e.getActionCommand();
                        int Manufacture=Integer.parseInt(text3.getText()) ;

                        if (e.getActionCommand().equals("Enter")) {
                            JOptionPane.showMessageDialog(null, "Manufacture's name is:"
                                    +Manufacture,"", JOptionPane.INFORMATION_MESSAGE);
                        }
                    }
                };
//constructing the Comment panel

                new JLabel("Customer Comments?");
                JPanel p7 = new JPanel();
                new JPanel( );
                textPrompt = new JLabel("Customer comments:");
                inputText = new JTextArea(5, 15);
                inputText.setLineWrap(true);
                inputText.setWrapStyleWord(true);

//add components to window
                container = getContentPane();
//use borderlayout to position our two i/o panels
                container.setLayout(new BorderLayout());
                p7.setLayout(new FlowLayout());
//add components to panels
                p7.add(textPrompt);
                p7.add(inputText);
//add panels to our primary borderlayout
                add(p7, BorderLayout.NORTH);
// add JTabbedPane to container
                getContentPane().add( tab );
                setSize( 400, 250 );
                setVisible( true );
                JButton button7 = new JButton("Enter");
                p7.add(button7);
                tab.addTab ("Customer Comments?",null,p7,"Customer Comments?");
                button7.addActionListener(this);}

            new ActionListener(){
                public void actionPerformed(ActionEvent e) {
e.getActionCommand();
                    int Comments=Integer.parseInt(inputText.getText()) ;

                    if (e.getActionCommand().equals("Enter")) {
                        JOptionPane.showMessageDialog(null, "Customer's comment:"+Comments,"", JOptionPane.INFORMATION_MESSAGE);
                    }
                }

            };
        }
    }
@Override
    public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
    }

    public static void main( String args[] )
    {
        FinalProject demo = new FinalProject();
        demo.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}


This post has been edited by pbl: 21 August 2010 - 07:22 PM
Reason for edit:: Code tags added. Please :code:


Is This A Good Question/Topic? 0

Replies To: Java Final Project

#2 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 138
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Java Final Project

Posted 21 August 2010 - 06:30 PM

View PostAnna, on 22 August 2010 - 12:27 AM, said:

Need help asap!! My final is due tomorrow I have been glued to it all week and cant seem to figure out how to fix it... it doesnt show the results...please help me!!!!


What errors do you get ? How does your output differ from the expected output ?
Was This Post Helpful? 0
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Java Final Project

Posted 21 August 2010 - 06:31 PM

also, what is it supposed to do? I'm not going to piece it together.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Anna*


Reputation:

Re: Java Final Project

Posted 21 August 2010 - 06:37 PM

Each one does something different.

pool is suppose to take from height width and depth to make the volume when you click the enter button.

hot tube is suppose to take from height width and depth to make the volume when you click either oval or round.

Manufacture is suppost to show what is placed in the textfield when enter is pressed.

Customer Comments is suppost to show what is placed in the textarea when enter is pressed.

Customer is suppost to show the customer's address and name when enter is pressed.
Was This Post Helpful? 0

#5 Guest_Anna*


Reputation:

Re: Java Final Project

Posted 21 August 2010 - 06:39 PM

I am not getting any errors but it isnt doing what is suppose to be done when the button is pressed. It acts like I never hit the button.
Was This Post Helpful? 0

#6 guahguahmonster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 209
  • Joined: 29-August 07

Re: Java Final Project

Posted 22 August 2010 - 12:04 AM

View PostAnna, on 21 August 2010 - 05:39 PM, said:

I am not getting any errors but it isnt doing what is suppose to be done when the button is pressed. It acts like I never hit the button.


Doesn't this make you suspicious in any way? If NONE of your buttons are working, and you're quite sure nothing else is wrong with your code, there are no errors, and it compiles fine, then it's a great guess to say that something is wrong with your ActionListeners, because ActionListeners are in charge of executing code when "stuff happens".

A short explanation of what's wrong in your code. For all of your calls to addActionListener, you attempt to add this (a FinalProject) as the ActionListener. So that means the actionPerformed method of the FinalProject is called whenever you click a button. But what's the actionPerformed method of the FinalProject? It's completely blank. So when you click the button, nothing happens, because that's what you programmed it to do.

Instead, littered throughout your FinalProject constructor are numerous statements of the form new ActionListener{ /* definition here */ };. These are useless because these ActionListeners are being created and then immediately discarded (nobody's keeping a reference to them!) Here's one example of the offending code I'm talking about:

// ALERT! THIS CODE IS INCORRECT! DO NOT USE!!!
public class FinalProject extends JFrame implements ActionListener {
    // Code omitted...
    public FinalProject() {
        // Code omitted...

        // Okay, so this adds a FinalProject as the button's ActionListener...
        button.addActionListener(this);
        
        // Incorrect code! This ActionListener gets discarded instantly!
        new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int length=Integer.parseInt(text4.getText());
                int width=Integer.parseInt(text5.getText());
                int depth=Integer.parseInt(text6.getText());
                
                if (e.getActionCommand().equals("Enter")) {
                    int volume = ((length * width) * depth);
                    JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
                }
                }
        };
    }

    // Incorrect code! This method is empty!!!
    public void actionPerformed(ActionEvent e) {
         // TODO Auto-generated method stub
    }
}



I can think of a few options to fix this (there may be more, but these are the simplest). These first two examples show how to change the above incorrect code to be correct.

1. Pass anonymous classes to addActionListener.

public class FinalProject extends JFrame implements ActionListener {
    // Code omitted...
    public FinalProject() {
        // Code omitted...

        // Add an anonymous class as an ActionListener.
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int length=Integer.parseInt(text4.getText());
                int width=Integer.parseInt(text5.getText());
                int depth=Integer.parseInt(text6.getText());
                
                if (e.getActionCommand().equals("Enter")) {
                    int volume = ((length * width) * depth);
                    JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }
}



2. Make inner classes in FinalProject, instantiate them and pass them to addActionListener.

public class FinalProject extends JFrame implements ActionListener {
    // Code omitted...

    // Make an inner class to act as an ActionListener later on.
    private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            // NB: text{4, 5, 6} have to be class variables of FinalProject
            int length=Integer.parseInt(text4.getText());
            int width=Integer.parseInt(text5.getText());
            int depth=Integer.parseInt(text6.getText());
            
            if (e.getActionCommand().equals("Enter")) {
                int volume = ((length * width) * depth);
                JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }

    public FinalProject() {
        // Code omitted...

        // Instantiate the previously defined inner class,
        // and add that as an ActionListener
        button.addActionListener(new ButtonListener());
    }
}



Note that if you do this you can make more than one inner class, which makes sense as your ActionListeners are all different in some way.

3. Actually define actionPerformed of FinalProject. This is not easy because you need at least 6 different ActionListeners doing different things and therefore it's probably just eaiser to use anonymous classes or inner classes.

Hope this all made sense. Feel free to ask for clarification. Good luck.

This post has been edited by guahguahmonster: 22 August 2010 - 12:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,310
  • Joined: 06-March 08

Re: Java Final Project

Posted 22 August 2010 - 02:19 PM

View Postguahguahmonster, on 22 August 2010 - 01:04 AM, said:

1. Pass anonymous classes to addActionListener.

public class FinalProject extends JFrame implements ActionListener {
    // Code omitted...
    public FinalProject() {
        // Code omitted...

        // Add an anonymous class as an ActionListener.
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int length=Integer.parseInt(text4.getText());
                int width=Integer.parseInt(text5.getText());
                int depth=Integer.parseInt(text6.getText());
                
                if (e.getActionCommand().equals("Enter")) {
                    int volume = ((length * width) * depth);
                    JOptionPane.showMessageDialog(null, "The Volume is:"+volume,"for your pool.", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
    }
}


When using anonymous class no need to check for the actionCommand: can only be the actual button
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1