取消Java上的按钮

时间:2014-07-23 23:30:52

标签: java swing mouseevent cancel-button

所以我试图创建一个java applet,我正在尝试创建一个取消按钮,所以当它被点击时,它会退出。我一直在说错误

  

无法找到符号 - 类MyMouseListener2
  button1.addMouseListener(new MyMouseListener2());

这是我的代码:

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!");
        }
    }
}

我不知道我做错了什么可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

对你的问题......

您正在尝试在匿名类中创建一个类...

f.addWindowListener(new WindowAdapter() {
    public class MyMouseListener2 extends MouseAdapter {

        public void mouseClicked(MouseEvent m) {
            System.exit(0);
        }
    }
});

这不起作用,除此之外,你完全忽略了WindowAdapter。这意味着(即使它确实有效),MyMouseListener2只能在anonymous WindowAdapter的上下文中访问...

解决方案......

不要使用MouseListener按钮,它不会考虑如果用户在按下按钮时按 Enter 会发生什么,而是要添加按钮ActionListener ...

button1.addMouseListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        System.exit(0);
    }
});

这将考虑鼠标点击和键盘事件(并在按钮上调用doClick)...

摆脱MyMouseListener2中的WindowAdapter,它毫无意义,无论如何都不会编译(是的,它会让它工作,但这样做是没有意义的,所以你不妨摆脱它。)

如果您愿意,您还可以创建一个从ActionListener扩展的类并使用其中的实例,或者您可以利用Action的API

使用Action的API,你可以做一些时髦的事情,例如使用key bindings API注册 Escape 来触发相同的操作并使用与{{菜单栏上的{1}} ...

仔细看看......

了解更多详情...

答案 1 :(得分:0)

MyMouseListener1和MyMouseListener2在侦听器中定义。它们在代码块之外是不可见的。

这是你修改的代码:

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!");
        }
    }
}