Reputation: 27
So I'm trying to make a java applet and I am trying to make a cancel button, so when it is clicked, it exits out. I keep getting an error where it says
cannot find symbol - class MyMouseListener2
button1.addMouseListener(new MyMouseListener2());
Here is my code:
import java.awt.*;
import java.awt.event.*;
public class registrations{
Button button1;
Button button2;
TextField objTextField1;
TextField objTextField2;
TextField objTextField3;
TextField objTextField4;
TextField objTextField5;
Label label1;
Label label2;
Label label3;
Label label4;
Label label5;
Label label6;
public static void main (String args[]){
registrations r = new registrations();
}
public registrations() {
Frame f = new Frame ("Sum of Numbers");
Button button1 = new Button("Ok");
button1.setBounds(30,305,150,75);
button1.addMouseListener(new MyMouseListener1());
Button button2 = new Button("Cancel");
button2.setBounds(230,305,150,75);
button1.addMouseListener(new MyMouseListener2());
f.add(button1);
f.add(button2);
label1 = new Label("First Number: ");
label1.setBounds(20,75,100,25);
f.add(label1);
label2 = new Label("Last Name: ");
label2.setBounds(20,115,100,25);
f.add(label2);
label3 = new Label("Email Address: ");
label3.setBounds(20,155,100,25);
f.add(label3);
label4 = new Label("Username: ");
label4.setBounds(20,195,100,25);
f.add(label4);
label5= new Label("Password: ");
label5.setBounds(20,235,100,25);
f.add(label5);
label6 = new Label();
objTextField1 = new TextField("", 15);
objTextField1.setBounds(140,75,75,25);
objTextField2 = new TextField("",0);
objTextField2.setBounds(140,115,75,25);
objTextField3 = new TextField("",0);
objTextField3.setBounds(140,155,75,25);
objTextField4 = new TextField("",0);
objTextField4.setBounds(140,195,75,25);
objTextField5 = new TextField("",0);
objTextField5.setBounds(140,235,75,25);
f.add(label6);
f.add(label1);
f.add(label2);
f.add(label3);
f.add(label4);
f.add(label5);
f.add(objTextField1);
f.add(objTextField2);
f.add(objTextField3);
f.add(objTextField4);
f.add(objTextField5);
f.add(label6);
f.addWindowListener(new WindowAdapter()
{
public class MyMouseListener2 extends MouseAdapter{
public void mouseClicked(MouseEvent m){
System.exit(0);
}
}
});
f.setSize(400,400);
f.setVisible(true);
}
public class MyMouseListener1 extends MouseAdapter{
public void mouseClicked(MouseEvent me){
label6.setText("Thank you for registering!");
}
}
}
I don't know what I'm doing wrong can you please help me?
Upvotes: 0
Views: 2717
Reputation: 8338
MyMouseListener1 and MyMouseListener2 are defined in the listener. They are not visible to outside of the block of code.
Here's your code fixed:
import java.awt.*;
import java.awt.event.*;
public class registrations{
Button button1;
Button button2;
TextField objTextField1;
TextField objTextField2;
TextField objTextField3;
TextField objTextField4;
TextField objTextField5;
Label label1;
Label label2;
Label label3;
Label label4;
Label label5;
Label label6;
private MouseAdapter mouseListener;
public static void main (String args[]){
registrations r = new registrations();
}
public registrations() {
mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
System.out.println("clicked");
}
};
Frame f = new Frame ("Sum of Numbers");
Button button1 = new Button("Ok");
button1.setBounds(30,305,150,75);
button1.addMouseListener(new MyMouseListener1());
Button button2 = new Button("Cancel");
button2.setBounds(230,305,150,75);
button1.addMouseListener(mouseListener);
f.add(button1);
f.add(button2);
label1 = new Label("First Number: ");
label1.setBounds(20,75,100,25);
f.add(label1);
label2 = new Label("Last Name: ");
label2.setBounds(20,115,100,25);
f.add(label2);
label3 = new Label("Email Address: ");
label3.setBounds(20,155,100,25);
f.add(label3);
label4 = new Label("Username: ");
label4.setBounds(20,195,100,25);
f.add(label4);
label5= new Label("Password: ");
label5.setBounds(20,235,100,25);
f.add(label5);
label6 = new Label();
objTextField1 = new TextField("", 15);
objTextField1.setBounds(140,75,75,25);
objTextField2 = new TextField("",0);
objTextField2.setBounds(140,115,75,25);
objTextField3 = new TextField("",0);
objTextField3.setBounds(140,155,75,25);
objTextField4 = new TextField("",0);
objTextField4.setBounds(140,195,75,25);
objTextField5 = new TextField("",0);
objTextField5.setBounds(140,235,75,25);
f.add(label6);
f.add(label1);
f.add(label2);
f.add(label3);
f.add(label4);
f.add(label5);
f.add(objTextField1);
f.add(objTextField2);
f.add(objTextField3);
f.add(objTextField4);
f.add(objTextField5);
f.add(label6);
f.addWindowListener(new WindowAdapter()
{
final class MyMouseListener2 extends MouseAdapter{
public void mouseClicked(MouseEvent m){
System.exit(0);
}
}
});
f.setSize(400,400);
f.setVisible(true);
}
final class MyMouseListener1 extends MouseAdapter{
public void mouseClicked(MouseEvent me){
label6.setText("Thank you for registering!");
}
}
}
Upvotes: 0
Reputation: 347194
You are trying to create a class within an anonymous class...
f.addWindowListener(new WindowAdapter() {
public class MyMouseListener2 extends MouseAdapter {
public void mouseClicked(MouseEvent m) {
System.exit(0);
}
}
});
This isn't going to work, besides which, you totally ignore the WindowAdapter
anyway. This means (even if it did work), MyMouseListener2
would only be accessible from within the context of the anonymous
WindowAdapter
...
Don't use a MouseListener
for buttons, it won't take into account what would happen if the user pressed Enter while the button was focused, instead, you want to add a ActionListener
to the button...
button1.addMouseListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
This will take into account both the mouse click and keyboard events (and calling doClick
on the button)...
Get rid of MyMouseListener2
in the WindowAdapter
, it makes no sense and simply won't compile anyway (yes there's away to make it work, but it makes no sense to do it this way, so you might as well get rid of it).
If you want to, you could also create a class that extends from ActionListener
and use an instance of that instead or you could take advantage of the Action
s API
With the Action
s API, you could do funky things like use the key bindings API to register the Escape to trigger the same action and use the same action as a JMenuItem
on a menu bar...
Take a closer look at...
for more details...
Upvotes: 1