Results 1 to 3 of 3
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default Problem with if statement in a calculator program

    I'm trying to write a calculator program but i can't get if statement in the inner class, which is supposed to find out which button was clicked, to work.

    Also, would it be better to use a seperate anonymous inner class for each button to handle its events or just one inner class to handle all ActionListener events and a giant if else statement to find the button which was clicked (which is what i'm trying to do at the moment)?

    Here's a little runnable program which demonstrates my problem:

    Java Code:
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class CalculatorJFrame extends JFrame{
    	//Class objects
    	private ActionListenerClass actionListener = new ActionListenerClass();
    	//JCompnents
    	private JPanel primaryDisplayJPanel = new JPanel();
    	private JPanel buttonsJPanel = new JPanel();
    	private JButton one, two, three, four, five, six, seven, eight, nine, zero, dot, multiply, divide, add, minus, equals, canel, clear, posNeg;
    	
    	public CalculatorJFrame(){
    		
    		//setting JFrame
    		setTitle("Calculator");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(240, 280);
    		setResizable(false);
    		setVisible(true);
    		
    		//setting layout of JFrame
    		primaryDisplayJPanel.setPreferredSize(new Dimension(getWidth(), 70));
    		primaryDisplayJPanel.setBackground(Color.WHITE);
    		add(primaryDisplayJPanel, BorderLayout.NORTH);
    		add(buttonsJPanel, BorderLayout.CENTER);
    		
    		//setting buttons
    		setButton(canel, "C", new Dimension(50, 30));
    		setButton(clear, "CE", new Dimension(50, 30));
    		setButton(posNeg, "+/-", new Dimension(50, 30));
    		setButton(divide, "/", new Dimension(50, 30));
    		setButton(one, "1", new Dimension(50, 30));
    		setButton(two, "2", new Dimension(50, 30));
    		setButton(three, "3", new Dimension(50, 30));
    		setButton(multiply, "*", new Dimension(50, 30));
    		setButton(four, "4", new Dimension(50, 30));
    		setButton(five, "5", new Dimension(50, 30));
    		setButton(six, "6", new Dimension(50, 30));
    		setButton(minus, "-", new Dimension(50, 30));
    		setButton(seven, "7", new Dimension(50, 30));
    		setButton(eight, "8", new Dimension(50, 30));
    		setButton(nine, "9", new Dimension(50, 30));
    		setButton(add, "+", new Dimension(50, 30));
    		setButton(zero, "0", new Dimension(50, 30));
    		setButton(dot, ".", new Dimension(50, 30));
    		setButton(equals, "=", new Dimension(105, 30));
    	}
    	public static void main(String args[]){
    		new CalculatorJFrame();
    	}
    	private void setButton(JButton button, String tag, Dimension dimension){
    		button = new JButton(tag);
    		button.setPreferredSize(dimension);
    		button.addActionListener(actionListener);
    		buttonsJPanel.add(button);
    	}
    	private class ActionListenerClass implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
    			System.out.println("ActionListenerClass");
    			
    			if(event.getSource().equals(one))
    				System.out.println("one");
    			else
    				System.out.println("fail");
    		}	
    	}
    }
    Last edited by peterhabe; 08-26-2011 at 04:30 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default

    You aren't actually setting the one reference in your setButton method. You're just replacing the reference you pass in (which is null) with a new JButton. That doesn't change the variable you passed in. Add this to your ActionListener to prove it:

    System.out.println("Null: " + (one == null));

    It's sorta like this:

    Java Code:
    public void doMath(int i){
       i = 7;
    }
    
    int x = 4;
    doMath(x);
    //what is x now? It's still 4, of course!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

Similar Threads

  1. Getting Backspace on a java calculator program to work
    By marylanddem in forum New To Java
    Replies: 1
    Last Post: 12-09-2010, 01:01 AM
  2. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 01:43 PM
  3. Calculator Program HELP NEEDED FAST! Homework assignment
    By SteroidalPsycho in forum New To Java
    Replies: 3
    Last Post: 03-05-2009, 04:02 AM
  4. unreachable statement - Java calculator program
    By V2001Gordon in forum New To Java
    Replies: 3
    Last Post: 12-13-2008, 12:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •