不太明白你的意思,不过我实验下来好像可以,不知道你的情况是什么?import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class TestFrame extends JFrame{
    JDialog dialog = new JDialog();
    /** Creates a new instance of TestFrame */
    public TestFrame() {
        JButton btn = new JButton("aaa");
        btn.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt)
            {
                dialog.setSize(300, 200);
                dialog.show();
            }
        });
        getContentPane().add(btn);
        dialog.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                int flag = JOptionPane.showConfirmDialog(dialog, "if close");
                if (flag == JOptionPane.OK_OPTION)
                {
                    System.out.println(evt.getClass().getName());
                }
                else
                {
                    dialog.hide();
                }
            }
        });
        
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                System.exit(0);
            }
        });
    }
    
    public static void main(String[] args)
    {
        JFrame f = new TestFrame();
        f.setSize(640, 480);
        f.show();
    }
    
}

解决方案 »

  1.   

    >因为WindowAdapter是awt的方法,在它里面去实现swing的Dialog可想而知了
    这没有什么不妥的 实在不行拿出来放到外面就是了在构造函数里写一个 WindowAdapter
    this.addWindowListener(new java.awt.event.WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            this_windowClosing(e);
          }
        });然后就可以用
    void this_windowClosing(WindowEvent e) {
      // your code goes here
    }来处理了
      

  2.   

    你们好象会错意思了,比如我正在做个项目,当我选种frame里的菜单中的“退出”时,往往要做的就是先弹个对话框来提示一下,当用户确认后才真正关闭frame,但我创建的frame不是点击右上角的关闭按扭就不声不响的退出应用程序了么,而没有任何提示信息,我就是指用什么方法来disable或hidden掉“关闭”按扭,或当点击关闭时有消息框提示,当确认才关闭
      

  3.   

    先在构造函树里确定这个 JFrame 的缺省 CloseOperation 为 DO_NOTHING_ON_CLOSEsetDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);这样你点击右上角的关闭按扭的时候窗口就不会退出了
    但这是他仍然可以接受 windowClosing 事件
    这是你就可以在void this_windowClosing(WindowEvent e) {
      // your code goes here
    }中弹出你的对话框 用户确认后
    用 System.exit(0); 退出
      

  4.   

    void this_windowClosing(WindowEvent e) {
      //在这里调用JOptionPane.showComfirmDialog方法
      //然后利用此方法返回的值:OK或Cancel 来判断你想真正关闭还是在
      //关闭之前先作其他事情,比如保存
    }