| | |
Applet -more than one event handler
![]() |
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
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!

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!
Java Syntax (Toggle Plain Text)
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)); } }
Last edited by Natique; Aug 27th, 2008 at 8:03 pm.
1. what errors do you actually get?
2. ALL class names should begin with a capital
3. you never actually initialize your handlers
2. ALL class names should begin with a capital
3. you never actually initialize your handlers
operatorHandler operation = new operatorHandler(); My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
oh, i didn't notice nice catch
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "-Albert Einstein
•
•
Join Date: Feb 2008
Posts: 17
Reputation:
Solved Threads: 0
Sorry if this is the wrong forum now. But how can I view my applet in a web browser? I used the code below, and I also uploaded operatorHandler.class and clearHandler.class to the directory. Should I have made a .jar file? If so, how?
Java Syntax (Toggle Plain Text)
<html> <head> </head> <body> <applet code="calculator.class" codebase="I put it- don't know it off hand" width=200 height=200></applet> </body> </html>
![]() |
Similar Threads
- isFixReg() ??? (Java)
- Amaena, SysProtect, WinAntivirus popups (Viruses, Spyware and other Nasties)
- Help Please! (Viruses, Spyware and other Nasties)
- Hijackthis report, I just don't know (Viruses, Spyware and other Nasties)
- Cant stop pop ups, computer lagging (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: do cookies contain passwords??
- Next Thread: Demo projects
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






