Results 1 to 14 of 14
Thread: Basic Calculator
- 09-08-2011, 12:38 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
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:
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 BimzJava 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; } } }
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
- 09-08-2011, 12:46 PM #2
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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:
I guess you could put another method into the GUI class which will process the value: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. } }
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.
- 09-08-2011, 12:56 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
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.
- 09-08-2011, 01:28 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.
- 09-08-2011, 01:34 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
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
- 09-08-2011, 08:01 PM #6
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
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.
- 09-09-2011, 01:10 AM #7
- 09-09-2011, 04:16 AM #8
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
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.
-
Re: Basic Calculator
- 09-09-2011, 04:28 AM #10
- 09-09-2011, 06:51 AM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Basic Calculator
Here is something really basic that shows how to share an ActionListener:i want to create it so that when you click the button it is displayed in the TextField?
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); } }
- 09-09-2011, 07:49 AM #12
Member
- Join Date
- Aug 2011
- Posts
- 31
- Rep Power
- 0
Re: Basic Calculator
the problem im having is that it says an error, cannot find symbol. i think its because txt is in a differant classJava 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] } } }Last edited by Bimz; 09-09-2011 at 07:55 AM.
- 09-09-2011, 09:40 AM #13
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Re: Basic Calculator
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.
- 09-09-2011, 10:47 AM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Basic Java Calculator Code Problem
By SerbianSergeant in forum New To JavaReplies: 7Last Post: 07-22-2011, 01:50 PM -
help with basic sum calculator in java
By java157 in forum New To JavaReplies: 8Last Post: 03-17-2011, 11:12 PM -
Basic Calculator
By trtoy in forum New To JavaReplies: 1Last Post: 12-25-2009, 01:06 AM -
Basic Calculator
By Rose88 in forum Java AppletsReplies: 3Last Post: 06-25-2009, 12:34 AM -
Calculator help.
By madkidd02 in forum New To JavaReplies: 2Last Post: 10-25-2008, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks