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 5 of 5

Thread: Need help building a simple calculator

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Need help building a simple calculator

    I am trying to get the number keys right now to post there number in the tf text field.
    Right now I am getting a few errors and noting happens when you click one the buttons.

    here my code

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
     
     
     
     
    public class BasicGUI extends JFrame implements ActionListener {
     
    	private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
    	private JTextField tf;
    	private JPanel display, keypad;
    	private GridLayout bGrid;
     
    	private class MyListener implements ActionListener {
    		public void actionPerformed(ActionEvent e){
    			JButton b = (JButton)e.getSource();
     
    			String output = "";
     
    			if (b == b0){
    				output += "0";
    			} else if (b == b1){
    				output += "1";
    			} else if (b == b2){
    				output += "2";
    			} else if (b == b3){
    				output += "3";
    			} else if (b == b4){
    				output += "4";
    			}else if (b == b5){
    				output += "5";
    			}else if (b == b6){
    				output += "6";
    			}else if (b == b7){
    				output += "7";
    			}else if (b == b8){
    				output += "8";
    			}else if (b == b9){
    				output += "9";
    			}
     
    			//tarea.setText(out);
    			tf.setText(output);
     
     
    		}
    	}
     
     
    	public BasicGUI() {
     
    		super("My First GUI Application");
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    		bGrid = new GridLayout(4, 3, 2, 2);
     
    		keypad = new JPanel(bGrid);
    		display = new JPanel();
     
    		b0 = new JButton("0");
    		b1 = new JButton("1");
    		b2 = new JButton("2");
    		b3 = new JButton("3");
    		b4 = new JButton("4");
    		b5 = new JButton("5");
    		b6 = new JButton("6");
    		b7 = new JButton("7");
    		b8 = new JButton("8");
    		b9 = new JButton("9");
    		plus = new JButton("+");
    		equal = new JButton("=");
     
    		keypad.add(b0);
    		keypad.add(b1);
    		keypad.add(b2);
    		keypad.add(b3);
    		keypad.add(b4);
    		keypad.add(b5);
    		keypad.add(b6);
    		keypad.add(b7);
    		keypad.add(b8);
    		keypad.add(b9);
    		keypad.add(plus);
    		keypad.add(equal);
     
     
    		tf = new JTextField(20);
    		display.add(tf);
     
     
     
     
    		this.add(display, BorderLayout.NORTH);
    		this.add(keypad, BorderLayout.SOUTH);
     
     
    		this.pack();
     
     
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		new BasicGUI().setVisible(true);
     
    	}
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    }

    Thank you for any help you can give.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help building a simple calculator

    What are your errors? Are the compiler errors or runtime errors? Post them here.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help building a simple calculator

    errors are fixed, but I am having problems register my buttons to post to the tf text field when you click on them.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help building a simple calculator

    lol, ok I see what i was doing wrong. sometimes a good night rest and a new look can solve many problems.

    here is my new working code,
    feel free to try it and use it

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
     
     
     
     
    public class BasicGUI extends JFrame implements ActionListener {
     
    	private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
    	private JTextField tf;
    	private JPanel display, keypad;
    	private GridLayout bGrid;
    	String strIn = "";
    	String strOut = "";
    	String str = "";
    	int output = 0;
     
     
    	public BasicGUI() {
     
    		super("My First GUI Application");
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    		bGrid = new GridLayout(4, 3, 2, 2);
     
    		keypad = new JPanel(bGrid);
    		display = new JPanel();
     
    		b0 = new JButton("0");
    		b1 = new JButton("1");
    		b2 = new JButton("2");
    		b3 = new JButton("3");
    		b4 = new JButton("4");
    		b5 = new JButton("5");
    		b6 = new JButton("6");
    		b7 = new JButton("7");
    		b8 = new JButton("8");
    		b9 = new JButton("9");
    		plus = new JButton("+");
    		equal = new JButton("=");
     
     
    		b0.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 0 button");
                    strIn = strIn+"0";
                    tf.setText(strIn);
     
                }});
    		b1.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 1 button");
                    strIn = strIn+"1";
                    tf.setText(strIn);
     
                }});
    		b2.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 2 button");
                    strIn = strIn+"2";
                    tf.setText(strIn);
     
                }});
    		b3.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 3 button");
                    strIn = strIn+"3";
                    tf.setText(strIn);
     
                }});
    		b4.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 4 button");
                    strIn = strIn+"4";
                    tf.setText(strIn);
     
                }});
    		b5.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 5 button");
                    strIn = strIn+"5";
                    tf.setText(strIn);
     
                }});
    		b6.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 6 button");
                    strIn = strIn+"6";
                    tf.setText(strIn);
     
                }});
    		b7.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 7 button");
                    strIn = strIn+"7";
                    tf.setText(strIn);
     
                }});
    		b8.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 8 button");
                    strIn = strIn+"8";
                    tf.setText(strIn);
     
                }});
    		b9.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the 9 button");
                    strIn = strIn+"9";
                    tf.setText(strIn);
     
                }});
    		plus.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the plus button");
                    output = Integer.parseInt( strIn )+output;
                    str = strIn +  "+" + str;	
                    strOut = str;
                    strIn = "";
                    tf.setText(strOut);
     
     
                }});
    		equal.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("You clicked the equal button");
                    output = Integer.parseInt( strIn )+output;
                    str = str + strIn;	
                    strOut = str;
                    strOut = strOut +  "=" + Integer.toString(output);	
                    tf.setText(strOut);
                    strIn = "";
                    strOut = "";
                    str = "";
                    output = 0;
     
                }});
     
    		keypad.add(b0);
    		keypad.add(b1);
    		keypad.add(b2);
    		keypad.add(b3);
    		keypad.add(b4);
    		keypad.add(b5);
    		keypad.add(b6);
    		keypad.add(b7);
    		keypad.add(b8);
    		keypad.add(b9);
    		keypad.add(plus);
    		keypad.add(equal);
     
     
    		tf = new JTextField(20);
    		display.add(tf);
     
     
     
    		this.add(display, BorderLayout.NORTH);
    		this.add(keypad, BorderLayout.SOUTH);
     
     
    		this.pack();
     
     
    	}
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		new BasicGUI().setVisible(true);
     
    	}
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
     
    	}
     
    }
    Last edited by zigma; October 13th, 2011 at 10:22 PM.

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help building a simple calculator

    I just improved my code, new code posted below

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
     
    public class BasicGUI extends JFrame implements ActionListener {
     
    	private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, plus, equal;
    	private JTextField tf;
    	private JPanel display, keypad;
    	private GridLayout bGrid;
    	String outputTf = "";
    	int output = 0;
    	String n = "";
    	String i = "";
    	int num = 0;
     
     
    	public void actionPerformed(ActionEvent e) { // event handler
    		JButton b = (JButton) e.getSource();
     
    		if (b == b0) {
    			i = Integer.toString(0);
    			n = n +  Integer.toString(0);
     
    		} else if (b == b1) {
    			i = Integer.toString(1);
    			n = n +  Integer.toString(1);
     
    		} else if (b == b2) {
    			i = Integer.toString(2);
    			n = n +  Integer.toString(2);
     
    		} else if (b == b3) {
    			i = Integer.toString(3);
    			n = n +  Integer.toString(3);
     
    		} else if (b == b4) {
    			i = Integer.toString(4);
    			n = n +  Integer.toString(4);
     
    		} else if (b == b5) {
    			i = Integer.toString(5);
    			n = n +  Integer.toString(5);
     
    		} else if (b == b6) {
    			i = Integer.toString(6);
    			n = n +  Integer.toString(6);
     
    		} else if (b == b7) {
    			i = Integer.toString(7);
    			n = n +  Integer.toString(7);
     
    		} else if (b == b8) {
    			i = Integer.toString(8);
    			n = n +  Integer.toString(8);
     
    		} else if (b == b9) {
    			i = Integer.toString(9);
    			n = n +  Integer.toString(9);
     
    		} else if (b == plus) {
    			num = num + Integer.parseInt(n);
    			n="";
    			i = "+";
     
     
    		} else if (b == equal) {
    			output = num + Integer.parseInt(n);
    			i = "="+ output;
    		}
     
    		outputTf = outputTf + i;
    		tf.setText(outputTf);
    	}
     
    	public BasicGUI() {
     
    		super("My First GUI Application");
     
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    		bGrid = new GridLayout(4, 3, 2, 2);
     
    		keypad = new JPanel(bGrid);
    		display = new JPanel();
     
    		b0 = new JButton("0");
    		b1 = new JButton("1");
    		b2 = new JButton("2");
    		b3 = new JButton("3");
    		b4 = new JButton("4");
    		b5 = new JButton("5");
    		b6 = new JButton("6");
    		b7 = new JButton("7");
    		b8 = new JButton("8");
    		b9 = new JButton("9");
    		plus = new JButton("+");
    		equal = new JButton("=");
     
    		keypad.add(b0);
    		keypad.add(b1);
    		keypad.add(b2);
    		keypad.add(b3);
    		keypad.add(b4);
    		keypad.add(b5);
    		keypad.add(b6);
    		keypad.add(b7);
    		keypad.add(b8);
    		keypad.add(b9);
    		keypad.add(plus);
    		keypad.add(equal);
     
    		tf = new JTextField(20);
    		display.add(tf);
     
    		this.add(display, BorderLayout.NORTH);
    		this.add(keypad, BorderLayout.SOUTH);
    		this.pack();
     
    		// register the event listener with the buttons
     
    		b0.addActionListener(this);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
    		b3.addActionListener(this);
    		b4.addActionListener(this);
    		b5.addActionListener(this);
    		b6.addActionListener(this);
    		b7.addActionListener(this);
    		b8.addActionListener(this);
    		b9.addActionListener(this);
    		plus.addActionListener(this);
    		equal.addActionListener(this);
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
     
    		new BasicGUI().setVisible(true);
    	}
    }

  6. The Following User Says Thank You to zigma For This Useful Post:

    ikocijan (June 20th, 2012)

Similar Threads

  1. Building your own Date class.
    By ShaunB in forum Java SE API Tutorials
    Replies: 5
    Last Post: October 25th, 2011, 05:37 PM
  2. Simple Calculator
    By JKDSurfer in forum Loops & Control Statements
    Replies: 3
    Last Post: June 28th, 2011, 01:37 PM
  3. Building your own Date class.
    By ShaunB in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 21st, 2011, 06:11 PM
  4. help building up GUI for poker game
    By Pencil in forum AWT / Java Swing
    Replies: 5
    Last Post: October 26th, 2010, 02:53 PM
  5. Building a Menu List
    By websey in forum AWT / Java Swing
    Replies: 1
    Last Post: November 15th, 2009, 10:34 AM