如果是要实现单击按钮退出,建议删掉这行代码:
if(e.getActionCommand().equals("Eixt"))
或者,将实现接口的两个类变为public的内部类,同时稍微修改下,如下
import java.awt.*;
import java.awt.event.*;
public class lesson1 {
private Frame f;
private Button b;
public lesson1() {
f = new Frame("event");
b = new Button("Exit");
}
public void launchFrame() {
b.addMouseListener(new ButtonHandler());
f.addWindowListener(new ClosingHander());
f.add(b, BorderLayout.CENTER);
f.setSize(400, 300);
f.setVisible(true);
f.setVisible(true);
f.setSize(400, 300);
}
public static void main(String args[]) {
lesson1 aa = new lesson1();
aa.launchFrame();
}
class ButtonHandler extends MouseAdapter {
public void mouseClicked(MouseEvent e)
{
if (e.getSource() == b) {
System.exit(0);
}
}
}
class ClosingHander extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
监听按钮,当按钮被点击时,就退出。代码如下:
jb_button.addMouseListener(new MouseAdapter() { // 对jb_button按钮添加监听事件
@Override
public void mouseClicked(MouseEvent e) { // 当鼠标点击时
System.exit(0); // 退出
}
});
if(e.getActionCommand().equals("Eixt"))
"exit" 写错了吧...