关闭窗口时需要询问是否确定关闭,并且点击弹出的对话框X形按钮同样实现取消关闭功能
- import java.awt.*;
- import java.awt.event.*;
- class MyFrameDemo01
- {
- //创建Frame窗口
- private Frame f;
- private Button okBut,cancelBut;
- private Dialog dia;
- private Label lab;
- MyFrameDemo01(){
- init();
- }
- public void init(){
- f = new Frame("My Frame");
- f.setBounds(100,60,600,480);
- dia = new Dialog(f,"关闭提示",true);
- dia.setBounds(200,160,200,160);
- dia.setLayout(new FlowLayout());
- lab = new Label("确定关闭吗?",Label.CENTER);
- okBut = new Button("确定");
- cancelBut = new Button("取消");
- myEvent();
- f.setVisible(true);
- }
- public void myEvent(){
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
-
- dia.add(lab);
- dia.add(okBut);
- dia.add(cancelBut);
- //添加Dialog窗口监听器,点击Dialog的关闭按键时,同样实现取消关闭功能
- dia.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- dia.setVisible(false);
- }
- });
- //确定关闭Frame窗口
- okBut.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- System.exit(0);
- }
- });
- //取消关闭Frame窗口
- cancelBut.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- dia.setVisible(false);
- }
- });
- dia.setVisible(true);
- }
- });
- }
- public static void main(String[] args)
- {
- new MyFrameDemo01();
- }
- }
复制代码 |
|