Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ActionListener question

Status
Not open for further replies.

Alira

Programmer
Mar 21, 2001
77
CA
Me again...
I read few treads on the subject and tried to make something, but it does not work..
Please, tell me... what have I done wrong?
here is the list of errors:
C:\MyHost\New.java:17: <identifier> expected
btnRegister.addActionListener(new RegisterListener());
^
New.java:19: <identifier> expected
btnClear.addActionListener(new ClearListener());
^
New.java:17: cannot resolve symbol
symbol : class addActionListener
location: package btnRegister
btnRegister.addActionListener(new RegisterListener());
^
New.java:19: cannot resolve symbol
symbol : class addActionListener
location: package btnClear
btnClear.addActionListener(new ClearListener());
^
New.java:158: cannot resolve symbol
symbol : class ActionListener
location: class New.RegisterListener
protected class RegisterListener implements ActionListener
^
New.java:160: cannot resolve symbol
symbol : class ActionEvent
location: class New.RegisterListener
public void actionPerformed(ActionEvent e)
^
New.java:168: cannot resolve symbol
symbol : class ActionListener
location: class New.ClearListener
protected class ClearListener implements ActionListener
^
New.java:170: cannot resolve symbol
symbol : class ActionEvent
location: class New.ClearListener
public void actionPerformed(ActionEvent e)

Here is a code I was trying to compile:

import java.awt.*;
import java.applet.*;
import java.io.*;
import java.lang.*;
import javax.swing.*;


public class New extends Applet
{

TextField txtUserName;
TextField txtLastName;
TextField txtFirstName;
TextField txtEmail;

Button btnRegister = new Button(&quot;Register&quot;);
btnRegister.addActionListener(new RegisterListener());
Button btnClear = new Button(&quot;Clear&quot;);
btnClear.addActionListener(new ClearListener());

Label Label1 = new Label();
Label Label2 = new Label();
Label Label3 = new Label();
Label Label4 = new Label();
Label Label5 = new Label();
Label Label6 = new Label();
Label Label7 = new Label();
Label Label8 = new Label();
Label Label9 = new Label();

javax.swing.JPasswordField JPasswordField1 = new javax.swing.JPasswordField();
javax.swing.JPasswordField JPasswordField2 = new javax.swing.JPasswordField();

public void init()
{
resize(310,240);
setLayout(null);
//adding labels and textfields
Label1.setText(&quot;User Name&quot;);
add(Label1);
Label1.setBounds(65,12,60,20);
txtUserName=new TextField();
add(txtUserName);
txtUserName.setBounds(140,12,85,18);

Label2.setText(&quot;Password&quot;);
add(Label2);
Label2.setBounds(70,45,50,20);
add(JPasswordField1);
JPasswordField1.setBounds(140,45,85,18);

Label3.setText(&quot;Re-enter Password&quot;);
add(Label3);
Label3.setBounds(10,78,115,20);
add(JPasswordField2);
JPasswordField2.setBounds(140,78,85,18);

Label4.setText(&quot;Last Name&quot;);
add(Label4);
Label4.setBounds(65,111,55,20);
txtLastName=new TextField();
add(txtLastName);
txtLastName.setBounds(140,111,120,18);

Label5.setText(&quot;First Name&quot;);
add(Label5);
Label5.setBounds(65,144,55,20);
txtFirstName=new TextField();
add(txtFirstName);
txtFirstName.setBounds(140,144,120,18);

Label6.setText(&quot;E-mail&quot;);
add(Label6);
Label6.setBounds(75,177,55,20);
txtEmail=new TextField();
add(txtEmail);
txtEmail.setBounds(140,177,135,18);
add(btnRegister);
btnRegister.setBounds(140,200,65,23);
}


public String getUserName()
{
return txtUserName.getText();
}

public String setUserName(String s)
{
txtUserName.setText(s);
}

public String getFirstName()
{
return txtFirstName.getText();
}

public String getLastName()
{
return txtLastName.getText();
}

public String getEmail()
{
return txtEmail.getText();
}

public String getLevel()
{
return chLevel.getSelectedItem();
}

protected class RegisterListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource == btnRegister)
{
}
}
}

protected class ClearListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s=&quot;&quot;;
if(e.getSource == btnClear)
{
setUserName(s);
}
}
}
}
Have a great day!:)
 
You need to move these two lines:

btnRegister.addActionListener(new RegisterListener());
btnClear.addActionListener(new ClearListener());

into a method, like init(); You can initialize variables at the class level, but you can't call methods on them unless they're static and you enclose the calls in a static {} initialization block.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top