Hi :)
I was messing around with java applets just now, and I can't seem to figure out how to put more than one event handler in an applet. I'm just working on a really simple calculator. Three buttons, one to add, one to subtract and one to clear everything. I know it can easily be done with one event handler, but it's not about the program, just about learning how to use more than one. Here's the code, any idea what's wrong? Thanks!
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator extends JApplet
{
static JLabel resultLabel;
static JLabel entryLabel;
static JLabel outputLabel;
static JTextField enterNum;
static JButton adding;
static JButton subtracting;
static JButton clearbutton;
static double result;
//listener for clear button
static class clearHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
result=0.0;
outputLabel.setText("0.0");
enterNum.setText("");
}
}
//listener for adding and subtracting buttons
static class operatorHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double secondoperand=0.0;
secondoperand=Double.parseDouble(enterNum.getText());
String whichbutton=event.getActionCommand();
if (whichbutton.equals("+"))
result=result+secondoperand;
else if (whichbutton.equals("-"))
result =result-secondoperand;
outputLabel.setText(result+" ");
enterNum.setText("");
}
}
operatorHandler operation;
clearHandler clearing;
public void init()
{
result=0.0;
resultLabel=new JLabel("result: ");
entryLabel=new JLabel("enter #: ");
outputLabel=new JLabel("0.0");
adding=new JButton("+");
subtracting=new JButton("-");
clearbutton =new JButton("clear");
enterNum=new JTextField("value here ",10);
adding.addActionListener(operation);
subtracting.addActionListener(operation);
clearbutton.addActionListener(clearing);
add(resultLabel);
add(outputLabel);
add(entryLabel);
add(enterNum);
add(adding);
add(subtracting);
add(clearbutton);
setLayout(new GridLayout(4,1));
}
}