如果是要实绝樱现单击按钮退出,建议删掉这行代码:
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);
}
}
}