-
Creating a calculator
I need help incorporating the buttons, when the user clicks the button, it should display in the textfield. How do I do this? I am using GUI. Any help would be appreciated. Here is the code:
import TerminalIO.KeyboardReader;
import javax.swing.*;
import BreezySwing.*;
public class Calculator extends GBFrame {
// declare variables for the window objects
private DoubleField ansField;
private JButton oneButton;
private JButton twoButton;
private JButton threeButton;
private JButton fouButton;
private JButton fivButton;
private JButton sixButton;
private JButton sevButton;
private JButton eigButton;
private JButton ninButton;
private JButton zerButton;
private JButton addiButton;
private JButton subButton;
private JButton mulButton;
private JButton divButton;
private JButton dotButton;
// Constructor
public Calculator(){
// Instantiate and add window objects to the window
ansField = addDoubleField (0, 1,1,4,1);
oneButton = addButton ("1", 4,1,1,1);
twoButton = addButton ("2", 4,2,1,1);
threeButton = addButton ("3", 4,3,1,1);
fouButton = addButton ("4", 3,1,1,1);
fivButton = addButton ("5", 3,2,1,1);
sixButton = addButton ("6", 3,3,1,1);
sevButton = addButton ("7", 2,1,1,1);
eigButton = addButton ("8", 2,2,1,1);
ninButton = addButton ("9", 2,3,1,1);
zerButton = addButton ("0", 5,3,1,1);
addiButton = addButton ("+", 2,4,1,1);
subButton = addButton ("-", 3,4,1,1);
mulButton = addButton ("*", 4,4,1,1);
divButton = addButton ("/", 5,4,1,1);
dotButton = addButton (".", 5,2,1,1);
}
Respond to button click events
public void buttonClicked (JButton buttonObj){
// Local variables
Calculator calculate = new Calculator();
// Determine which button was clicked
if (buttonObj == addButton){
calculate.setCalculate(ansField.getNumber());
}
}
// Execution begins in the method main as asual.
public static void main (String [] args) {
Calculator theGUI = new Calculator();
theGUI.setSize (250, 250);
theGUI.setVisible (true);
}
}
-
First of all, the instatiation of a new Calculator class inside a buttonClicked
method seems a bit awkward to me, but it may be ok (I haven't seen its implementation).
When one of the buttons are clicked you have three choices:
1: add a new number char or decimal point to the display
2: perform stacking (user enters an operator after some digits input)
3: calculate (user enters an operator after some digits input when there is already a stacked value).
For ease of logics you should add a "change sign" button too. Also you should not
try to give sane responses to any "ape-iput"; don't do anything.
To catch the button clicks you must implement the ActionListener interface
(obviously ?) and add your class to the buttons list of actionlisteners.
On each buttonclick that is a number char you should copy/append the
displayfield value (after sanity checks of course):
String dispVal=displayField.getText().trim()+charClicked;
displayField.setText(dispVal);
But if you have implemented an editable displayField you have
quite a different ballgame to tackle
eschew obfuscation
-
Re:
Thank you for your opinion. I am newb at Java, and don't know how to implement all the suggestions you made. Can you please help me?
1st: How do I make an editable text display?
2nd: Actionlistener, how do I implement this?
-
1:
JTextField aTextField=new JTextField();
aTextField.setEditable(true); // not required, is editable by default
2:
You make yourself an object (panel/frame/whatever) and implement the ActionListener
interface for it. Then you add this object to the textcomponents' list of listeners
like:
aTextField.addActionListener(aListener); // aListener=any java object instance
check this:
http://www.apl.jhu.edu/~hall/CWP-Cha...apter13.2.html
ref: google
eschew obfuscation
-
Thank you, sjalle. This helps a lot. If I have any more questions, I'll post here. Once again, I appreciate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|