GUI Error and Button Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    GUI Error and Button Question

    Hello guys. I am trying to create a calculator program in a GUI window. When I compile the code I get the following errors:
    Severity and Description Path Resource Location Creation Time Id
    The local variable add is never read CalculatorProgr am/src/Calculator Calculator.java line 19 1211809496146 548
    The local variable contentPane is never read CalculatorProgr am/src/Calculator Calculator.java line 17 1211809496146 545
    The local variable divide is never read CalculatorProgr am/src/Calculator Calculator.java line 19 1211809496146 551
    The local variable layout is never read CalculatorProgr am/src/Calculator Calculator.java line 20 1211809496146 552
    The local variable multiply is never read CalculatorProgr am/src/Calculator Calculator.java line 19 1211809496146 550
    The local variable num1 is never read CalculatorProgr am/src/Calculator Calculator.java line 18 1211809496146 546
    The local variable num2 is never read CalculatorProgr am/src/Calculator Calculator.java line 18 1211809496146 547
    The local variable subtract is never read CalculatorProgr am/src/Calculator Calculator.java line 19 1211809496146 549

    Here is my code:
    Code:
    package Calculator;
    
    //import declarations
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import java.awt.Container;
    import java.awt.FlowLayout;
    
    //create a new class
    public class Calculator {
    	
    	//create the main function
    	public static void main(String[] args) {
    		//variable declaration
    		JFrame frame;
    		Container contentPane;
    		JTextField num1, num2;
    		JButton add, subtract, multiply, divide;
    		JLabel result;
    		FlowLayout layout;
    		
    		frame = new JFrame();
    		frame.setTitle("Calculator");
    		
    		contentPane = frame.getContentPane();
    		
    		num1 = new JTextField("Number 1");
    		num2 = new JTextField("Number 2");
    		
    		add = new JButton("Add");
    		subtract = new JButton("Subtract");
    		multiply = new JButton("Multiply");
    		divide = new JButton("Divide");
    		
    		result = new JLabel("Result");
    		
    	}
    
    }
    The code is not yet finished. My second question is how do you cause an event to happen when a button is clicked. When the for example add button is clicked I want to do this:
    Code:
    		int number1, number2, sum;
    		number1 = (int)((Double.parseDouble(num1.getText())));
            number2 = (int)((Double.parseDouble(num2.getText())));
            sum = number1 + number2;
            result.setText("" + sum);
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You'd use an ActionListener for the button event. As to the others...beats me.

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by Laharl
      You'd use an ActionListener for the button event. As to the others...beats me.
      I knew I needed to use an actionlistener but I don't know how.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        What compiler are you using? Those are odd messages. I think the latter ones are because your code is incomplete -- you create those widgets but do nothing with them.

        Comment

        • Kid Programmer
          New Member
          • Mar 2008
          • 176

          #5
          Originally posted by BigDaddyLH
          What compiler are you using? Those are odd messages. I think the latter ones are because your code is incomplete -- you create those widgets but do nothing with them.
          I am not sure what compiler I am using. The mac I use had a java compiler built in. I have a mac version 10.5.2. I could download a new compiler though...

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            That'd be Apple's compiler then...as for the ActionListener, Google 'actionlistener tutorial' or something similar.

            Comment

            • Kid Programmer
              New Member
              • Mar 2008
              • 176

              #7
              Originally posted by Laharl
              That'd be Apple's compiler then...as for the ActionListener, Google 'actionlistener tutorial' or something similar.
              I already did and found this:
              https://cis.med.ucalgar y.ca/http/java.sun.com/docs/books/tutorial/uiswing/events/actionlistener. html

              But I didn't understand 100% of it. Mainly I just didn't get what this code was for
              Code:
              add(b);
              add(text);

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                You're looking at an old pre-Swing tutorial, there. I suggest you bookmark this page to the real thing:

                java programming, learn java, java examples, java sample code, getting started with java

                Comment

                • Kid Programmer
                  New Member
                  • Mar 2008
                  • 176

                  #9
                  Well guys I fixed up my code and I think I will just make a new thread actually all about ActionListeners . So here is my new code:
                  Code:
                  /**
                   * Written By: Edward Sanger
                   * Coded in: Java
                   * IDE Used: IntelliJ IDEA
                   * Program Name: Calculator
                   * Program Description: This program will create a GUI window with 4 buttons for 4 mathematical operators and 2 text
                   * fields for the numbers to add, subtract, multiply, and divide.
                   * Date: May 26, 2008
                   * Time: 2:34:52 PM
                   */
                  
                  //import declarations
                  import java.awt.*;
                  import javax.swing.*;
                  
                  public class Calculator implements ActionListener {
                      public static void main(String[] args) {
                          //create a new jframe
                          JFrame frame = new JFrame();
                          frame.setTitle("Calculator");
                          //create a container
                          Container contentPane = frame.getContentPane();
                  
                          //create the frame objects
                          JTextField num1 = new JTextField("Number 1");
                          JTextField num2 = new JTextField("Number 2");
                  
                          JButton add = new JButton("Add");
                          JButton subtract = new JButton("Subtract");
                          JButton multiply = new JButton("Multiply");
                          JButton divide = new JButton("Subtract");
                  
                          JLabel result = new JLabel("Result");
                  
                          //add the objects
                          contentPane.add(num1);
                          contentPane.add(num2);
                          
                          contentPane.add(add);
                          contentPane.add(subtract);
                          contentPane.add(multiply);
                          contentPane.add(divide);
                  
                          contentPane.add(result);
                  
                          //create the layout
                          FlowLayout layout = new FlowLayout();
                          contentPane.setLayout(layout);
                  
                          //create an action listener
                          
                  
                          //frame settings
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.pack();
                          frame.setVisible(true);
                      }
                  }

                  Comment

                  Working...