Results 1 to 14 of 14
  1. #1
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Basic Calculator

    I'm trying to create a basic calculator, ive got the layout fine with all the required buttons and 1 TextField. the calculator will be able to do simple addition and subtraction, so far its all good and im trying to assain function to the button when they are clicked. This is my code:

    Java Code:
    public class Gui extends JFrame{
        
        public String calc;
        public int ans;
        
        public Gui() {
            
            setLayout(new FlowLayout());
            
            setTitle("Basic Calculator - Bimz");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(360, 130);
            setResizable(false);
            setLocationRelativeTo(null);
            
            JTextField txt = new JTextField("", 30);
            txt.setEditable(false);
            add(txt);
            
            JButton btn0 = new JButton("0");
            add(btn0);
            
            JButton btn1 = new JButton("1");
            add(btn1);
            
            JButton btn2 = new JButton("2");
            add(btn2);
            
            JButton btn3 = new JButton("3");
            add(btn3);
            
            JButton btn4 = new JButton("4");
            add(btn4);
            
            JButton btn5 = new JButton("5");
            add(btn5);
            
            JButton btn6 = new JButton("6");
            add(btn6);
            
            JButton btn7 = new JButton("7");
            add(btn7);
            
            JButton btn8 = new JButton("8");
            add(btn8);
            
            JButton btn9 = new JButton("9");
            add(btn9);
            
            JButton btnP = new JButton("+");
            add(btnP);
            
            JButton btnM = new JButton("-");
            add(btnM);
            
            JButton btnE = new JButton("=");
            add(btnE);
            
                    
            JButton btnD = new JButton("DEL");
            add(btnD);
            
            Handler0 handler = new Handler0();
            btn0.addActionListener(handler);
            
            setVisible(true);
            
        }
                    
            private class Handler0 implements ActionListener {
                public void actionPerformed(ActionEvent event) {
                    calc = calc + "0";
                    txt = calc;
                }
            }
    }
    when the 0 button is clicked (so far im only trying to make the 0 button work) calc, the string variable will add 0 to itself. Im wondering how to display calc into the TextField which i have created? please help Bimz

    edit: so i forgot to mention the TextField is a variable called txt and i wrote "txt = calc;" but this comes up with an error, please help.

    Much Appreciated

  2. #2
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Basic Calculator

    I think a simple solution would be to write an action listener class which accepts a character and then passes it to some function that processes it.

    I'd go for something like:

    Java Code:
    public class ButtonClickListener implements ActionListener{
    
    private char character = '';
    
    public ButtonClickListener(char character){
    
    this.character = character;
    
    }
    
    public static void actionPerformed(ActionEvent e){
    
    //and you need to pass character to some method that will process it.
    
    
    }
    
    }
    I guess you could put another method into the GUI class which will process the value:

    Java Code:
    public void processValue(char character){
    
    // check if its an Integer or is it a command(add,sub,del)
    // build new string and update the textbox
    
    
    }
    Last edited by FlyNn; 09-08-2011 at 12:55 PM.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  3. #3
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: Basic Calculator

    would i then be able to use "+" or "-" as a character? because i was planning on converting the string to integer, then putting the solved integer value back into txt when the = button is clicked.

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Basic Calculator

    well you cant convert "+" to an integer.
    There's two parts to the process:
    1 - Make the program to distinguish between digits and commands(-,+,=,del);
    2 - Display inside the text box what the user has clicked as a String.

    The only I advised you to use chars is because with chars you only need one method that understands the values.

    You could have one ActionListener class that accepts Integers and another that accepts Strings(+,-,=,del);

    Are you going to display only the selection or keep track of what has been pressed?

    If the first option, then there is no reason to convert just display the selected value in the text box and do the conversions in the background to keep total.
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  5. #5
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: Basic Calculator

    well before i start dealing with the math part of the rogramming, i want to create it so that when you click the button it is displayed in the TextField? how would i do that? thank you

  6. #6
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Basic Calculator

    I've given you the code above.
    in the action performed you just have to add
    textbox.setText(char value);
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Basic Calculator

    Quote Originally Posted by FlyNn View Post
    I think a simple solution would be to write an action listener class which accepts a character and then passes it to some function that processes it.
    That would require creating a different Listener for each button. Create a single Listener that gets the value of the Button clicked and add that to the TextField.

  8. #8
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: Basic Calculator

    the problem im having is there is an error with saying "txt.setText(calc);", it says cannot find symbol when i looked over txt.

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

    Default Re: Basic Calculator

    Quote Originally Posted by Bimz View Post
    the problem im having is there is an error with saying "txt.setText(calc);", it says cannot find symbol when i looked over txt.
    Please don't paraphrase the error message, but instead give the full error message. Also show us where you are calling this from (show the new method and class).

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Basic Calculator

    Quote Originally Posted by Bimz View Post
    it says cannot find symbol
    Sounds like the TextField is not in the same scope.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: Basic Calculator

    i want to create it so that when you click the button it is displayed in the TextField?
    Here is something really basic that shows how to share an ActionListener:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonCalculator extends JFrame implements ActionListener
    {
    	private JButton[] buttons;
    	private JTextField display;
    
    	public ButtonCalculator()
    	{
    		display = new JTextField();
    		display.setEditable( false );
    		display.setHorizontalAlignment(JTextField.RIGHT);
    
    		JPanel buttonPanel = new JPanel();
    		buttonPanel.setLayout( new GridLayout(0, 5) );
    		buttons = new JButton[10];
    
    		for (int i = 0; i < buttons.length; i++)
    		{
    			String text = String.valueOf(i);
    			JButton button = new JButton( text );
    			button.addActionListener( this );
    			button.setMnemonic( text.charAt(0) );
    			buttons[i] = button;
    			buttonPanel.add( button );
    		}
    
    		getContentPane().add(display, BorderLayout.NORTH);
    		getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    		setResizable( false );
    	}
    
    	public void actionPerformed(ActionEvent e)
    	{
    		JButton source = (JButton)e.getSource();
    		display.replaceSelection( source.getActionCommand() );
    	}
    
    	public static void main(String[] args)
    	{
    		UIManager.put("Button.margin", new Insets(10, 10, 10, 10) );
    		ButtonCalculator frame = new ButtonCalculator();
    		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
    		frame.pack();
    		frame.setLocationRelativeTo( null );
    		frame.setVisible(true);
    	}
    }

  12. #12
    Bimz is offline Member
    Join Date
    Aug 2011
    Posts
    31
    Rep Power
    0

    Default Re: Basic Calculator

    Java Code:
    public class Gui extends JFrame{
        
        public String calc;
        public int ans;
        
        public Gui() {
            
            setLayout(new FlowLayout());
            
            setTitle("Basic Calculator - Bimz");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(360, 130);
            setResizable(false);
            setLocationRelativeTo(null);
            
            JTextField [COLOR="#FF0000"]txt[/COLOR] = new JTextField("", 30);
            txt.setEditable(false);
            add(txt);
            
            JButton btn0 = new JButton("0");
            add(btn0);
            
            JButton btn1 = new JButton("1");
            add(btn1);
            
            JButton btn2 = new JButton("2");
            add(btn2);
            
            JButton btn3 = new JButton("3");
            add(btn3);
            
            JButton btn4 = new JButton("4");
            add(btn4);
            
            JButton btn5 = new JButton("5");
            add(btn5);
            
            JButton btn6 = new JButton("6");
            add(btn6);
            
            JButton btn7 = new JButton("7");
            add(btn7);
            
            JButton btn8 = new JButton("8");
            add(btn8);
            
            JButton btn9 = new JButton("9");
            add(btn9);
            
            JButton btnP = new JButton("+");
            add(btnP);
            
            JButton btnM = new JButton("-");
            add(btnM);
            
            JButton btnE = new JButton("=");
            add(btnE);
            
                    
            JButton btnD = new JButton("DEL");
            add(btnD);
            
            Handler0 handler = new Handler0();
            btn0.addActionListener(handler);
            
            setVisible(true);
            
        }
                    
            private class Handler0 implements ActionListener {
                public void actionPerformed(ActionEvent event) {
                    calc = calc + "0";
                    [COLOR="#FF0000"]txt.setText(calc);[/COLOR]
                }
            }
    }
    the problem im having is that it says an error, cannot find symbol. i think its because txt is in a differant class
    Last edited by Bimz; 09-09-2011 at 07:55 AM.

  13. #13
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default Re: Basic Calculator

    Quote Originally Posted by Junky View Post
    That would require creating a different Listener for each button. Create a single Listener that gets the value of the Button clicked and add that to the TextField.
    Is it no possible to add the same action listener to multiple buttons?

    Java Code:
    button1.addActionListener(new ButtonClickListener('1'));
    button2.addActionListener(new ButtonClickListener('2'));
    button3.addActionListener(new ButtonClickListener('3'));
    sub.addActionListener(new ButtonClickListener('-'));
    Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Basic Calculator

    I find it interesting how the OP tries to develop this application by just concentrating on the view of it all and ignoring the controller part completely. Controllers (the 'intelligence' of it all) don't come as an afterthought ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 01:50 PM
  2. help with basic sum calculator in java
    By java157 in forum New To Java
    Replies: 8
    Last Post: 03-17-2011, 11:12 PM
  3. Basic Calculator
    By trtoy in forum New To Java
    Replies: 1
    Last Post: 12-25-2009, 01:06 AM
  4. Basic Calculator
    By Rose88 in forum Java Applets
    Replies: 3
    Last Post: 06-25-2009, 12:34 AM
  5. Calculator help.
    By madkidd02 in forum New To Java
    Replies: 2
    Last Post: 10-25-2008, 07:42 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
  •