Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Calculator help

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Calculator help

    I am making a simple program the user enters in a number using JButtons 0-9 and then the user clicks gallons or miles to add the number to one or the other. Here is my code:

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
     
     
    public class SouthPanel extends JPanel{
    	JLabel milesTitle, fuelTitle, saveTitle, milesText, fuelText, saveText, topLabel;
    	JButton miles, gallons, save;
     
    	public SouthPanel (JLabel northLabel){
    		topLabel = northLabel;
    		milesTitle = new JLabel("total miles");
    		fuelTitle = new JLabel("fuel consumed");
    		saveTitle = new JLabel("save to file");
     
    		milesText = new JLabel("0.0");
    		fuelText = new JLabel("0.0");
    		saveText = new JLabel("\"statistics.txt\"");
     
    		JButton miles = new JButton("miles");
    		JButton gallons = new JButton("gallons");
    		JButton save = new JButton("Save");
     
    		ButtonListener listener = new ButtonListener();
    		miles.addActionListener(listener);
    		gallons.addActionListener(listener);
    		save.addActionListener(listener);
     
    		setLayout(new GridLayout(3,3));
     
    		add(milesTitle);
    		add(milesText);
    		add(miles);
    		add(fuelTitle);
    		add(fuelText);
    		add(gallons);
    		add(saveTitle);
    		add(saveText);
    		add(save);
    	}
    	private class ButtonListener implements ActionListener{
     
    		public void actionPerformed (ActionEvent event){
    			double temp = 0;
    			double temp2 = 0;
    			double sumit = 0;
     
    			if (event.getSource() == miles){
    				temp = Double.parseDouble(topLabel.getText());
    			    temp2 = Double.parseDouble(milesText.getText());
    				sumit = temp + temp2;
    			    milesText.setText("" + sumit);
    			    topLabel.setText("");
    			}
     
    			if (event.getSource() == gallons){
    				temp = Double.parseDouble(topLabel.getText());
    			    temp2 = Double.parseDouble(fuelText.getText());
    				sumit = temp + temp2;
    			    fuelText.setText("" +sumit);
    			    topLabel.setText("");
    			}
     
    	   }
       }
    }

    It runs and compiles fine, but my two button listeners dont seem to be working. When i click one of them nothing happens. Is there something wrong with-in the code of each button?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Calculator help

    The references to the buttons the ActionListener checks for are never initialized - the constructor creates JButtons which are added to the GUI, but these are out of scope after the constructor is called. Change it so the variables are initialized and used:

    miles = new JButton("miles");//as opposed to JButton miles = new JButton("miles");

  3. The Following User Says Thank You to copeg For This Useful Post:

    Bradshjo (November 1st, 2010)

  4. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Calculator help

    thanks a lot it worked...stupid error that costed me hours. But thats programming sometimes.

Similar Threads

  1. Java Calculator
    By helloworld922 in forum Algorithms & Recursion
    Replies: 7
    Last Post: January 10th, 2011, 06:01 AM
  2. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  3. Help codiing calculator
    By FM010 in forum Exceptions
    Replies: 7
    Last Post: June 12th, 2009, 02:25 PM
  4. Calculator application using java
    By fabolous04 in forum Paid Java Projects
    Replies: 4
    Last Post: March 25th, 2009, 11:29 AM
  5. Problem of implementing mathematic logic in Java applet
    By AnithaBabu1 in forum Java Applets
    Replies: 0
    Last Post: August 15th, 2008, 11:42 PM