| | |
Applet Calculator
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 0
the errors are commented in the code:
help???
help???
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.lang.*; public class Kol2_1 extends Applet implements ActionListener { Button plus = new Button("+"); Button minus = new Button("-"); Button times = new Button("*"); Button div = new Button("/"); Button eq = new Button("="); TextField pole = new TextField("E hej zver"); double x=0.0,y=0.0, z; public void init() { try{ x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:")); y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:")); } catch(StringIndexOutOfBoundsException n) {JOptionPane.showMessageDialog(this, "Vnesete broj.");} setLayout(new GridLayout(4,2)); add(plus); add(minus); add(times); add(div); add(eq); add(pole); plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ } public void actionPerformed(ActionEvent e) { switch(e.getSource()){ //incompatible types case plus: { z=x+y;} case minus: { z=x-y;} case times: { z=x*y;} case div: { try{z=x/y;} catch(NumberFormatException m) { if(y==0) JOptionPane.showMessageDialog(this,"Division by zero."); } } case eq: {pole.setText(""+z);} } } }
Last edited by PennyBoki; Feb 12th, 2007 at 7:42 pm.
*squints at code*
.....
Haha, silly me. I missed this at first glance.
1) instead of
2)
Might wanna reconsider, with casting and/or an if-else statement.
.....
Haha, silly me. I missed this at first glance.
1) instead of
*.addActionPerformed(this); use *.addActionListener(this);2)
e.getSource() returns an Object, and switch statements work with int values.
Might wanna reconsider, with casting and/or an if-else statement. synchronized (theWorld) { System.out.println ("It's all mine..."); }
How many people have code in their Sigs?
How many people have code in their Sigs?
•
•
Join Date: Feb 2007
Posts: 3
Reputation:
Solved Threads: 1
you are giving plus.addactionPerformed(this);. you should not give like that. please give plus.addActionListener(this);. and then after finishing the constructor write the method
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("+"))
{....}
}
here e.getActionCommand() method return the caption(that is the string that you written on the button). and then check whether it it equal to +,-,*,/ and then perform the relevant action.
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("+"))
{....}
}
here e.getActionCommand() method return the caption(that is the string that you written on the button). and then check whether it it equal to +,-,*,/ and then perform the relevant action.
•
•
•
•
the errors are commented in the code:
help???
Java Syntax (Toggle Plain Text)
import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.lang.*; public class Kol2_1 extends Applet implements ActionListener { Button plus = new Button("+"); Button minus = new Button("-"); Button times = new Button("*"); Button div = new Button("/"); Button eq = new Button("="); TextField pole = new TextField("E hej zver"); double x=0.0,y=0.0, z; public void init() { try{ x=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za x:")); y=Double.parseDouble(JOptionPane.showInputDialog("Vnesete vrednost za y:")); } catch(StringIndexOutOfBoundsException n) {JOptionPane.showMessageDialog(this, "Vnesete broj.");} setLayout(new GridLayout(4,2)); add(plus); add(minus); add(times); add(div); add(eq); add(pole); plus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ minus.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ times.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ div.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ eq.addActionPerformed(this); /*cannot find symbol method addActionPerformed(Kol2_1)*/ } public void actionPerformed(ActionEvent e) { switch(e.getSource()){ //incompatible types case plus: { z=x+y;} case minus: { z=x-y;} case times: { z=x*y;} case div: { try{z=x/y;} catch(NumberFormatException m) { if(y==0) JOptionPane.showMessageDialog(this,"Division by zero."); } } case eq: {pole.setText(""+z);} } } }
![]() |
Similar Threads
- Applet / Array help (Java)
- Writing Applet ActionListener Mortgage Calculator (Java)
- Review my applet (Java)
- help!! My applet can't run... Urgent!! (Java)
- Applet or Application? (Java)
Other Threads in the Java Forum
- Previous Thread: Re: AIRPLANE PROGRAM
- Next Thread: Accessing frame from Action listener
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





