Problem with Action Event
Hey guys,
i am quite new to Java and i just started learning this ActionEvent and im getting semicolons errors.
mind to help me?
Code :
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
public class LongDistanceCall extends JFrame implements ActionListener
{
JLabel lbl1,lbl2,lbl3,lbl4;
JTextField tf1,tf2,tf3;
JRadioButton rb1,rb2,rb3;
JPanel p1,p2,p3,p4,p5;
rb1.add ActionListener(this);
rb2.add ActionListener(this);
rb3.add ActionListener(this);
public static void main (String [] arg)
{
LongDistanceCall fr = new LongDistanceCall();
fr.setSize(300,250);//pack();
fr.setVisible(true);
fr.setTitle("Long Distance Call");
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public LongDistanceCall()
{
lbl1 = new JLabel("Enter hours of the call:");
lbl2 = new JLabel("Enter Minutes of the call:");
lbl3 = new JLabel("Rate Category:");
lbl4 = new JLabel("Charges for the call:RM");
tf1 = new JTextField(5);
tf2 = new JTextField(5);
tf3 = new JTextField(5);
rb1 = new JRadioButton("Daytime (6am - 5.59am)");
rb2 = new JRadioButton("Evening (6pm - 11.59pm)");
rb3 = new JRadioButton("off-Peak (12am - 5.59am)");
p1 = new JPanel();
p1.setLayout(new GridLayout(1,2));
p1.add(lbl1);
p1.add(tf1);
p2 = new JPanel();
p2.setLayout(new GridLayout(1,2));
p2.add(lbl2);
p2.add(tf2);
p3 = new JPanel();
p3.setLayout(new GridLayout(1,2));
p3.add(lbl3);
p4 = new JPanel();
p4.setLayout(new GridLayout(3,2));
p4.add(rb1);
p4.add(rb2);
p4.add(rb3);
p5 = new JPanel();
p5.add(lbl4);
p5.add(tf3);
setLayout(new GridLayout(5,1));
add(p1);
add(p2);
add(p3);
add(p4);
add(p5);
}
public void ActionPerformed(Action Event.e))
{
double hours = Double parseDouble(tf1.getText());
double minutes = Double parseDouble(tf2.getText());
double rate = 0;
if(e.getSource() == rb1)
{
rate = 0.07;
}
if(e.getSource() == rb2)
{
rate = 0.12;
}
if(e.getSource() == rb3)
{
rate = 0.05;
}
double duration = (hours*60)+(minutes);
double total = duration * rate;
tf3.setText(String ValueOf(total));
}
}
Hope to get help :)
thnx!
Re: Problem with Action Event
Please post the full text of the error messages that show where the problem is.
Re: Problem with Action Event
Quote:
Originally Posted by
Norm
Please post the full text of the error messages that show where the problem is.
Code :
illegal start of type
illegal start of type
illegal start of type
')' expected
<identifier> expected
illegal start of type
<identifier> expected
';' expected
<identifier> expected
';' expected
invalid method declaration; return type required
<identifier> expected
';' expected
illegal start of type
<identifier> expected
';' expected
<identifier> expected
';' expected
invalid method declaration; return type required
<identifier> expected
';' expected
illegal start of type
<identifier> expected
';' expected
<identifier> expected
illegal start of type
<identifier> expected
';' expected
illegal start of type
';' expected
illegal start of type
<identifier> expected
';' expected
illegal start of type
';' expected
illegal start of type
<identifier> expected
';' expected
illegal start of type
';' expected
<identifier> expected
')' expected
<identifier> expected
illegal start of type
<identifier> expected
';' expected
Re: Problem with Action Event
Please post the full text of the error messages that show where the problem is.
The error messages you posted do NOT show what source line the error is on.
Here is a sample of error message that shows where and what the error is:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^
These messages are from the javac command.
Re: Problem with Action Event
Sorry Norm,
Here it is.
Code :
--------------------Configuration: <Default>--------------------
C:\Users\Justin\Desktop\LongDistanceCall.java:11: illegal start of type
rb1.add ActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:12: illegal start of type
rb2.add ActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:13: illegal start of type
rb3.add ActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: ')' expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: <identifier> expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: illegal start of type
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: <identifier> expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:76: ';' expected
{
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: invalid method declaration; return type required
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: illegal start of type
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: invalid method declaration; return type required
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: illegal start of type
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:79: <identifier> expected
double rate = 0;
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: illegal start of type
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: <identifier> expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: ';' expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: illegal start of type
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: ';' expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: illegal start of type
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: <identifier> expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: ';' expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: illegal start of type
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: ';' expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: illegal start of type
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: <identifier> expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: ';' expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: illegal start of type
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: ';' expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ')' expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: illegal start of type
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ';' expected
tf3.setText(String ValueOf(total));
^
46 errors
Process completed.
Re: Problem with Action Event
Is there a space between add and ActionListener? Names can not have spaces in them.
Fix those errors and try again. Sometimes when the compiler gets lost because of this kind of error it flags too many other statements as errors.
Re: Problem with Action Event
Hi Norm, thnx for the replies.
i tried removing the space but it still have the same Errors.
Could it be something else?
Re: Problem with Action Event
Quote:
still have the same Errors.
Please post the full text of the error messages. Fixing the spaces problem should have changed the text of the messages. There must be new errors now.
Re: Problem with Action Event
Code :
--------------------Configuration: <Default>--------------------
C:\Users\Justin\Desktop\LongDistanceCall.java:11: <identifier> expected
rb1.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:11: illegal start of type
rb1.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:12: <identifier> expected
rb2.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:12: illegal start of type
rb2.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:13: <identifier> expected
rb3.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:13: illegal start of type
rb3.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: ')' expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: <identifier> expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: illegal start of type
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: <identifier> expected
public void ActionPerformed(Action Event.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:76: ';' expected
{
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: invalid method declaration; return type required
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: illegal start of type
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: <identifier> expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: invalid method declaration; return type required
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: illegal start of type
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: <identifier> expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:79: <identifier> expected
double rate = 0;
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: illegal start of type
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: <identifier> expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: ';' expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: illegal start of type
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:80: ';' expected
if(e.getSource() == rb1)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: illegal start of type
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: <identifier> expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: ';' expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: illegal start of type
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:84: ';' expected
if(e.getSource() == rb2)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: illegal start of type
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: <identifier> expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: ';' expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: illegal start of type
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:88: ';' expected
if(e.getSource() == rb3)
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ')' expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: illegal start of type
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: <identifier> expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ';' expected
tf3.setText(String ValueOf(total));
^
49 errors
Process completed.
Re: Problem with Action Event
Are those calls to the addActionListener() method inside of a method? They should be in a method, not just inside the class definition.
Quote:
public void ActionPerformed(Action Event.e))
I thought you removed the all the spaces inside of names. The above statement has a name with a space in it.
What is the . before the e for?
Re: Problem with Action Event
My mistake Norm,
there are still errors, but not as much as the previous one.
Code :
--------------------Configuration: <Default>--------------------
C:\Users\Justin\Desktop\LongDistanceCall.java:11: <identifier> expected
rb1.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:11: illegal start of type
rb1.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:12: <identifier> expected
rb2.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:12: illegal start of type
rb2.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:13: <identifier> expected
rb3.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:13: illegal start of type
rb3.addActionListener(this);
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: <identifier> expected
public void ActionPerformed(ActionEvent.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:75: ';' expected
public void ActionPerformed(ActionEvent.e))
^
C:\Users\Justin\Desktop\LongDistanceCall.java:77: ';' expected
double hours = Double parseDouble(tf1.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:78: ';' expected
double minutes = Double parseDouble(tf2.getText());
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ')' expected
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: not a statement
tf3.setText(String ValueOf(total));
^
C:\Users\Justin\Desktop\LongDistanceCall.java:95: ';' expected
tf3.setText(String ValueOf(total));
^
13 errors
Process completed.
Re: Problem with Action Event
Where are the statements causing the first errors with the calls to addActionListener()?
Are they inside a method?
Quote:
public void ActionPerformed(ActionEvent.e))
Why is there a . before the e? When defining an argument to a method give the name of the class and the name of the variable which are separated by a space.
Quote:
Double parseDouble(tf2.getText());
What is that expression supposed to be doing?
Re: Problem with Action Event
Quote:
Originally Posted by
Norm
What is that expression supposed to be doing?
isnt that expression converts from string to double?
I got this from my lecturer... and i am confused the way she teaches.
Re: Problem with Action Event
When you call a method, you must specify what class the method is in followed by a . followed by the method name:
theClass.theMethod(...);
For static methods you use the classname for theClass.
For non static methods you use a reference to a class object.
Re: Problem with Action Event
Norm, im sorry could you explain it in a more basic war :(
i cant really understand actually, Im sorry
Re: Problem with Action Event
What text book or tutorial are you using? These are very basic concepts that should have been covered in a student's textbook.
Here are some tutorials. Also ask google about calling methods in java.
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Re: Problem with Action Event
Thnx Norm for helping me!
I will go re-read my tutorials and try to find out more of the problems.
and thanks again for helping me!