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:
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);
}
}
05-15-2005, 09:58 AM
sjalle
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):
But if you have implemented an editable displayField you have
quite a different ballgame to tackle :)
05-15-2005, 04:31 PM
sonict
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?
05-16-2005, 06:55 AM
sjalle
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