java事件处理button如何单击按钮退出

2023-06-01 综合 24阅读
如果是要实绝樱现单击按钮退出,建议删掉这行代码:
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);
}
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com