外部动作——>事件源(组件)——>事件对象——>监听器
获取Frame对象,与上节一样
调用Frame对象的addWindowListener()方法,参数:WindowListener对象,WindowListener是个接口,里面有七个方法要实现,找实现子类WindowAdapter,匿名内部类重写windowClosing()方法,传递进来参数:WindowEvent对象
方法中,调用System.exit(0)
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class GuiDemo {
/**
* @param args
*/
public static void main(String[] args) {
Frame frame=new Frame("我是窗体");
frame.setSize(400,300);
frame.setLocation(500,200);
frame.setLayout(new FlowLayout());
Button button=new Button("按钮");
frame.add(button);
//关闭按钮
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有