不太明白你的意思,不过我实验下来好像可以,不知道你的情况是什么?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();
}
}
解决方案 »
- Exception in thread "main" java.lang.NullPointerException的问题
- 有没有用xml开发swing界面的?请说说常见开发包的特点
- powerdesigner(15.0)连接MYSQL报错。
- 小弟请教高手?? tomcat 在局域网内访问
- 基础问题:Object.equals()。请各位高手解惑!!~~谢谢!~~~
- 如何判断字符串中的中文?
- 关于按钮的问题。
- 基础界面问题
- 是path设置的问题吗?
- 怎么配置JAVA啊??
- 我是一位初学者想和大家交个朋友。进入请把QQ号留下好吗?(送高分)
- 那位大虾知道JRE1.1 or 1.2的是不是和P4的机器不兼容??
这没有什么不妥的 实在不行拿出来放到外面就是了在构造函数里写一个 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 来判断你想真正关闭还是在
//关闭之前先作其他事情,比如保存
}