不太明白你的意思,不过我实验下来好像可以,不知道你的情况是什么?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();
}
}
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();
}
}
这没有什么不妥的 实在不行拿出来放到外面就是了在构造函数里写一个 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
}来处理了
但这是他仍然可以接受 windowClosing 事件
这是你就可以在void this_windowClosing(WindowEvent e) {
// your code goes here
}中弹出你的对话框 用户确认后
用 System.exit(0); 退出
//在这里调用JOptionPane.showComfirmDialog方法
//然后利用此方法返回的值:OK或Cancel 来判断你想真正关闭还是在
//关闭之前先作其他事情,比如保存
}