Results 1 to 9 of 9
  1. #1
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default 2 buttons trouble

    Hi everybody im pretty new to java but anyways I need some help. Im writing a program to make a simple java calculator. I have added buttons for 0 - 9, clear, equals, and basic operators (+, -, *, /). I need to add two more buttons to complete the program. I have understood everything so far but after adding the basic operators and looking to add two more buttons I have ran into trouble. I am attempting to add a button for PI next. I have the button added and 3.14 declared for it but when the PI button is pressed I cannot get the 3.14 value to be connected to it. If somebody could help me I would appreciate it. Thanks.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Calculator extends JFrame implements ActionListener {
    
        private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
        private JButton bplus, bminus, bmultiply, bdivide, bequals, bpi, bclear;
        private JTextField textField;
    
    
        private int total = 0;
        private double pi = 3.14;
       
    
    
        public static void main(String[] args) {
            Calculator demo = new Calculator ();
            demo.setSize(200,300);
            demo.createGUI();
            demo.show();
        }
    
        private void createGUI() {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            Container window = getContentPane();
            window.setLayout(new FlowLayout());
    
            textField = new JTextField(15);
            window.add(textField);
    
    		bpi = new JButton("Pi");
    		window.add(bpi);
    		bpi.addActionListener(this);
    
    
            b1 = new JButton("1");
            window.add(b1);
            b1.addActionListener(this);
    
            b2 = new JButton("2");
            window.add(b2);
            b2.addActionListener(this);
    
            b3 = new JButton("3");
            window.add(b3);
            b3.addActionListener(this);
    
            bplus = new JButton("+");
            window.add(bplus);
            bplus.addActionListener(this);
    
            b4 = new JButton("4");
            window.add(b4);
            b4.addActionListener(this);
    
            b5 = new JButton("5");
            window.add(b5);
            b5.addActionListener(this);
    
            b6 = new JButton("6");
            window.add(b6);
            b6.addActionListener(this);
    
            bminus = new JButton("-");
            window.add(bminus);
            bminus.addActionListener(this);
    
    		bmultiply = new JButton("*");
            window.add(bmultiply);
            bmultiply.addActionListener(this);
    
    
            b7 = new JButton("7");
            window.add(b7 );
            b7 .addActionListener(this);
    
            b8 = new JButton("8");
            window.add(b8);
            b8.addActionListener(this);
    
            b9 = new JButton("9");
            window.add(b9);
            b9.addActionListener(this);
    
            bequals = new JButton("=");
            window.add(bequals);
            bequals.addActionListener(this);
    
    				bdivide = new JButton("/");
            window.add(bdivide);
            bdivide.addActionListener(this);
    
    
            b0 = new JButton("0");
            window.add(b0);
            b0.addActionListener(this);
    
            bclear = new JButton("clear");
            window.add(bclear);
            bclear.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent event) {
            Object source = event.getSource();
    
    
    
    
            if (source == bclear) {
    				 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                textField.setText("");
    						total = 0;
    						 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                return;
            }
    
           else  if (source == bplus) {
    				 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                total = total + Integer.parseInt(textField.getText());
                textField.setText("");
    						System.out.println ( total );
                return;
            }
    
           else  if (source == bminus) {
    				 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                total = total - Integer.parseInt(textField.getText());
    
    						textField.setText("");
    						System.out.println (total);
                return;
            }
    
    				else if (source == bmultiply) {
    				 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                total = total * Integer.parseInt(textField.getText());
                textField.setText("");
    						 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                return;
            }
    
    				else if (source == bdivide) {
    				 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                total = total / Integer.parseInt(textField.getText());
                textField.setText("");
    						 total =  Integer.parseInt(textField.getText());
    			 System.out.println (total);
                return;
            }
           else if (source == bequals) {
                textField.setText(Integer.toString(total));
                return;
            }
    
            textField.setText(textField.getText()
                  + ((JButton) source).getText());
        }
    }
    Last edited by Fubarable; 12-06-2011 at 11:26 PM. Reason: code tags added

  2. #2
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: 2 buttons trouble

    Your current action handler does something for clear, 4 operations, equals, and 'everything else'. You need to handle Pi apart form 'everything else'.

  3. #3
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2 buttons trouble

    I dont really understand what you mean thanks though. What im trying to ask is my JButton Pi displays Pi on the button and then Pi when the button is clicked. Im trying to set my JButton Pi to display 3.14 when clicked and not the text Pi.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: 2 buttons trouble

    You've got one ActionListener present, the gui class itself which implements ActionListener, and since you're adding this one listener to every button, in order for it to respond correctly to every button, it is forced to act as a "switchboard" listener. If you want to continue working this way, then you'll have to add more "switchboard" options to your actionPerfromed method including a check to see if the pi button (bpi I think) has been pressed, and then proper code to respond to this press. In other words, another else if (source == ...etc...)

    Otherwise use inner ActionListener classes for different function -- one for the number buttons, one for the operations buttons, and one for other buttons -- and you will gain less switchboarding and easier debugging.

  5. #5
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2 buttons trouble

    Ive added a bpi else if statement to check the condition. I probably coded it wrong but I still cannot figure out how to make the JButton Pi equal to 3.14 when clicked.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class Calculator extends JFrame implements ActionListener {

    private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9;
    private JButton bpi, bplus, bminus, bmultiply, bdivide, bequals, bclear;
    private JTextField textField;




    private int total = 0;
    String s1 = "3.14";
    double pi;




    public static void main(String[] args) {
    Calculator demo = new Calculator ();
    demo.setSize(200,300);
    demo.createGUI();
    demo.show();
    }

    private void createGUI() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container window = getContentPane();
    window.setLayout(new FlowLayout());

    textField = new JTextField(15);
    window.add(textField);

    bpi = new JButton("Pi");

    window.add(bpi);
    bpi.addActionListener(this);




    b1 = new JButton("1");
    window.add(b1);
    b1.addActionListener(this);

    b2 = new JButton("2");
    window.add(b2);
    b2.addActionListener(this);

    b3 = new JButton("3");
    window.add(b3);
    b3.addActionListener(this);

    bplus = new JButton("+");
    window.add(bplus);
    bplus.addActionListener(this);

    b4 = new JButton("4");
    window.add(b4);
    b4.addActionListener(this);

    b5 = new JButton("5");
    window.add(b5);
    b5.addActionListener(this);

    b6 = new JButton("6");
    window.add(b6);
    b6.addActionListener(this);

    bminus = new JButton("-");
    window.add(bminus);
    bminus.addActionListener(this);

    bmultiply = new JButton("*");
    window.add(bmultiply);
    bmultiply.addActionListener(this);


    b7 = new JButton("7");
    window.add(b7 );
    b7 .addActionListener(this);

    b8 = new JButton("8");
    window.add(b8);
    b8.addActionListener(this);

    b9 = new JButton("9");
    window.add(b9);
    b9.addActionListener(this);

    bequals = new JButton("=");
    window.add(bequals);
    bequals.addActionListener(this);

    bdivide = new JButton("/");
    window.add(bdivide);
    bdivide.addActionListener(this);


    b0 = new JButton("0");
    window.add(b0);
    b0.addActionListener(this);

    bclear = new JButton("clear");
    window.add(bclear);
    bclear.addActionListener(this);
    }

    public void actionPerformed(ActionEvent event) {
    Object source = event.getSource();




    if (source == bclear) {
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    textField.setText("");
    total = 0;
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    return;
    }
    else if (source == bpi) {
    total = Integer.parseInt(textField.getText());
    System.out.println(total);
    s1 = Double.toString(pi);
    total = Integer.parseInt(textField.getText());
    textField.setText("");
    System.out.println(total);
    return;
    }
    else if (source == bplus) {
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    total = total + Integer.parseInt(textField.getText());
    textField.setText("");
    System.out.println ( total );
    return;
    }

    else if (source == bminus) {
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    total = total - Integer.parseInt(textField.getText());

    textField.setText("");
    System.out.println (total);
    return;
    }

    else if (source == bmultiply) {
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    total = total * Integer.parseInt(textField.getText());
    textField.setText("");
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    return;
    }

    else if (source == bdivide) {
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    total = total / Integer.parseInt(textField.getText());
    textField.setText("");
    total = Integer.parseInt(textField.getText());
    System.out.println (total);
    return;
    }
    else if (source == bequals) {
    textField.setText(Integer.toString(total));
    return;
    }

    textField.setText(textField.getText()
    + ((JButton) source).getText());
    }
    }

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: 2 buttons trouble

    This doesn't make sense. Your code seems to show that you know how to set JTextFields and all sorts of other things, but when you want to add a simple functionality of displaying a number in the JTextField, you fail completely, and this begs the question -- is this code yours, or did you borrow it from some online site?

    What do you expect will happen when you call setText(""); in that block of code?

  7. #7
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2 buttons trouble

    No the code was built in my class with the help of a teacher. I understand what was taught to me as far as my class goes but we didnt learn how to add this simple function. I was taught how to make a button, set its name, and use basic operators. I was not taught how to set a value to a button. Like I said im new to Java so its really hard for me to magically learn these things if its not taught. If I understood I wouldnt be asking for help.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: 2 buttons trouble

    Quote Originally Posted by 2wyked View Post
    No the code was built in my class with the help of a teacher. I understand what was taught to me as far as my class goes but we didnt learn how to add this simple function. I was taught how to make a button, set its name, and use basic operators. I was not taught how to set a value to a button. Like I said im new to Java so its really hard for me to magically learn these things if its not taught. If I understood I wouldnt be asking for help.
    Don't wait for it to be taught -- teach it to yourself. This is how most of us have learned most of our programming so we know it works. You will want to learn how to set the text of a JTextArea, and so I recommend that you go to the Swing tutorials which you can find here: The really big index, and then check out the text field tutorial. You will want to check out the setText(String text) method most of all (which is why I mentioned it above).

  9. #9
    2wyked is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Re: 2 buttons trouble

    Thanks for the links. I figured out how to add the text "3.14" when the Pi button is pressed. I know how the operators work in the if statements but I feel that this is pretty simple because what im testing (+, -, *, /) is an operator. I cannot figure out how to make Pi able to perform all the basic operators. Can u point me in the right direction how to perform basic operators on Pi.

Similar Threads

  1. Why can't I see the images in the buttons?
    By eLancaster in forum New To Java
    Replies: 4
    Last Post: 10-17-2011, 09:40 PM
  2. Mouse Buttons
    By xael in forum New To Java
    Replies: 1
    Last Post: 09-07-2010, 11:39 PM
  3. Help with Buttons
    By wld4ubabay in forum New To Java
    Replies: 20
    Last Post: 05-17-2010, 08:13 AM
  4. Where To Get Pop Up Buttons
    By hitmen in forum AWT / Swing
    Replies: 7
    Last Post: 03-26-2009, 04:05 AM
  5. How to use SWT Buttons
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 04:44 PM

Tags for this Thread

Posting Permissions

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