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.

View Poll Results: Do you think I could be a moderator?

Voters
7. You may not vote on this poll
  • Yes

    3 42.86%
  • No

    3 42.86%
  • Too soon to tell

    1 14.29%
Results 1 to 6 of 6

Thread: Calculator

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Wink Calculator

    I'm trying to make a calculator.

    I know how to handle it if the user hits 1 and + and 1 and then equals, but how do I get it to keep storing the values if the user enters 1+1 * 3 / 2 = (read (1+1) * 3)/2)?

    package calculator;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class MyCalculator extends JFrame {
     
    	private JButton zero, one, two, three, four, five, six, seven, eight, nine, times,
    	plus, minus, divide, clear, equals, decimal, percent;
    	private JTextField numbers;
    	private JPanel panel;
     
    	public MyCalculator()
    	{
    		super("Basic Calculator");
    		setVisible(true);
    		panel = new JPanel();
    		panel.setVisible(true);
    		zero = new JButton("0");
    		panel.add(zero);
    		zero.setLocation(160, 160);
    		zero.setVisible(true);
    		one = new JButton("1");
    		panel.add(one);
    		one.setLocation(190, 160);
    		one.setVisible(true);
    		two = new JButton("2");
    		panel.add(two);
    		two.setLocation(220, 160);
    		two.setVisible(true);
    		numbers = new JTextField(12);
    		panel.add(numbers);
    		numbers.setLocation(60, 60);
    		numbers.setVisible(true);
    		numbers.setText("0");
    		zero.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "0";
    				numbers.setText(values);
    			}
     
    		});
    		one.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "1";
    				numbers.setText(values);
    			}
     
    		});
     
    		two.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "2";
    				numbers.setText(values);
    			}
     
    		});
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator

    package calculator;
     
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class MyCalculator extends JFrame {
     
    	private JButton zero, one, two, three, four, five, six, seven, eight, nine, times,
    	plus, minus, divide, clear, equals, decimal, percent;
    	private JTextField numbers;
    	private JPanel panel;
     
    	public MyCalculator()
    	{
    		super("Basic Calculator");
    		setVisible(true);
    		panel = new JPanel();
    		panel.setVisible(true);
    		zero = new JButton("0");
    		panel.add(zero);
    		zero.setLocation(160, 160);
    		zero.setVisible(true);
    		one = new JButton("1");
    		panel.add(one);
    		one.setLocation(190, 160);
    		one.setVisible(true);
    		two = new JButton("2");
    		panel.add(two);
    		two.setLocation(220, 160);
    		two.setVisible(true);
    		numbers = new JTextField(12);
    		panel.add(numbers);
    		numbers.setLocation(60, 60);
    		numbers.setVisible(true);
    		numbers.setText("0");
                    three = new JButton("3");
                  panel.add(three);
    three.setVisible(true);
    three.setLocation(160, 260);
    four = new JButton("4");
    panel.add(four);
    four.setVisible(true);
    four.setLocation(190, 260);
    five = new JButton("5");
    panel.add(five);
    five.setVisible(true);
    five.setLocation(220, 260);
    six = new JButton("6");
    panel.add(six);
    six.setVisible(true);
    six.setLocaiton(160, 300);
     
    		zero.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "0";
    				numbers.setText(values);
    			}
     
    		});
    		one.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "1";
    				numbers.setText(values);
    			}
     
    		});
     
    		two.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "2";
    				numbers.setText(values);
    			}
     
    		});
     
    	three.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "3";
    				numbers.setText(values);
    			}
     
    		});
     
    	four.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "4";
    				numbers.setText(values);
    			}
     
    		});
     
    	five.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "5";
    				numbers.setText(values);
    			}
     
    		});
     
    	six.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				String values = numbers.getText();
    				values = values + "6";
    				numbers.setText(values);
    			}
     
    		});
    	}
     
    public static void main(String[] args)
    {
    MyCalculator mc = new MyCalculator();
    }
    }

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Window Listener not working and something is running in background.

    public class MyCalculator extends JFrame implements WindowListener{
     
        private JButton zero, one, two, three, four, five, six, seven, eight, nine, times,
        plus, minus, divide, clear, equals, decimal, percent;
        private JTextField numbers;
        private JPanel panel;
     
        public MyCalculator()
        {
            setTitle("Basic Calculator");
           setVisible(true);
            panel = new JPanel();
           add(panel);
            panel.setVisible(true);
            zero = new JButton("0");
            panel.add(zero);
            zero.setLocation(260, 160);
     
            zero.setVisible(true);
            System.out.println(  zero.getLocation());
            System.out.println(zero.getLocationOnScreen());
            one = new JButton("1");
            panel.add(one);
            one.setLocation(190, 160);
            one.setVisible(true);
            two = new JButton("2");
            panel.add(two);
            two.setLocation(220, 160);
            two.setVisible(true);
            numbers = new JTextField(12);
          panel.add(numbers);
            numbers.setLocation(60, 60);
            numbers.setVisible(true);
            numbers.setText("0");
                    three = new JButton("3");
                  panel.add(three);
    three.setVisible(true);
    three.setLocation(160, 260);
    four = new JButton("4");
    panel.add(four);
    four.setVisible(true);
    four.setLocation(190, 260);
    five = new JButton("5");
    panel.add(five);
    five.setVisible(true);
    five.setLocation(220, 260);
    six = new JButton("6");
    panel.add(six);
    six.setVisible(true);
    six.setLocation(160, 300);
     
     
     
            zero.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "0";
                    numbers.setText(values);
                }
     
            });
            one.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "1";
                    numbers.setText(values);
                }
     
            });
     
            two.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "2";
                    numbers.setText(values);
                }
     
            });
     
        three.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "3";
                    numbers.setText(values);
                }
     
            });
     
        four.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "4";
                    numbers.setText(values);
                }
     
            });
     
        five.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "5";
                    numbers.setText(values);
                }
     
            });
     
        six.addActionListener(new ActionListener() {
     
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    String values = numbers.getText();
                    values = values + "6";
                    numbers.setText(values);
                }
     
            });
        this.setContentPane(panel);
      //  setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
     
    public static void main(String[] args)
    {
    MyCalculator mc = new MyCalculator();
     
    }
     
    @Override
    public void windowOpened(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void windowClosing(WindowEvent e) {
    	// TODO Auto-generated method stub
    	JFrame frame = new JFrame();
    	int x = JOptionPane.showConfirmDialog(frame, "Are you sure you want to exit?", "Exiting", JOptionPane.QUESTION_MESSAGE);
    if (x == JOptionPane.YES_OPTION)
    {
    	System.exit(0);
    }
     
    else
    {
    	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    }
    }
     
    @Override
    public void windowClosed(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void windowIconified(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void windowDeiconified(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void windowActivated(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
     
    @Override
    public void windowDeactivated(WindowEvent e) {
    	// TODO Auto-generated method stub
     
    }
    }

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Calculator

    Ok, somehow I lost my text, but I was gonna say, how do you get everything nicely in a layout?

    I had it all written out but then I accidentally exited my browser somehow and lost all that I typed.

    I've looked at the help for layouts and still don't get how it works so nicely. Is there a CustomLayout or something where I can defer from a certain pattern if I need to?

    What kind of layout is best for making a calculator?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculator

    I would recommend using a GridBagLayout. It gives you the flexibility to put items in a pseudo-grid while leaving out some of the more mundane tasks of trying to properly size and align everything.

    You could also create a Custom Layout if you wanted to, though I think this is over-kill and completely unnecessary here.

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

    javapenguin (December 22nd, 2010)

  7. #6
    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: Calculator

    You've asked several questions here, none of which are specific enough to be answerable really. This isn't the first time you've done that.

    I suggest you read through this and try again: How To Ask Questions The Smart Way

    Break your problem up into smaller pieces. Tackle each small piece independently, one at a time. Ask specific questions with code that is specific to each question.

Similar Threads

  1. Calculator problem
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 4th, 2010, 01:01 PM
  2. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM
  3. STANDARD CALCULATOR
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 27th, 2010, 08:41 PM
  4. Calculator help.
    By Skinnyskinny in forum Java Theory & Questions
    Replies: 6
    Last Post: August 1st, 2009, 12:34 PM
  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