Java 如何监视JDialog的关闭事件?
我有一个棘手的问题。我需要弹出一个确认消息框,供用户在有人单击窗口右上角的关闭标志时决定是否关闭。但它不起作用。无论您在弹出消息框中选择什么,对话框都将始终关闭。但是当我创建一个JFrame时,同样的逻辑也可以很好地工作 下面是我在netbeans IED中的代码。 我只是使用WindowAdpter捕捉关闭事件,plz igore由neatbean生成的伪代码 谢谢Java 如何监视JDialog的关闭事件?,java,swing,desktop-application,jdialog,windowlistener,Java,Swing,Desktop Application,Jdialog,Windowlistener,我有一个棘手的问题。我需要弹出一个确认消息框,供用户在有人单击窗口右上角的关闭标志时决定是否关闭。但它不起作用。无论您在弹出消息框中选择什么,对话框都将始终关闭。但是当我创建一个JFrame时,同样的逻辑也可以很好地工作 下面是我在netbeans IED中的代码。 我只是使用WindowAdpter捕捉关闭事件,plz igore由neatbean生成的伪代码 谢谢 import java.awt.event.WindowAdapter; import java.awt.event.Windo
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestClosingDialog extends javax.swing.JDialog {
public TestClosingDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
int a = JOptionPane.showConfirmDialog(null,"Are you sure you need to close?", "Tip", JOptionPane.YES_NO_OPTION);
if (a == 0) {
System.out.println("yes " + a);
System.exit(0); //close
} else if (a==1) {
System.out.println("no " + a);
}
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TestClosingDialog dialog = new TestClosingDialog(new JFrame(), true);
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
公共类TestClosingDialog扩展了javax.swing.JDialog{
公共TestClosingDialog(java.awt.Frame父级,布尔模式){
超级(父级、模态);
初始化组件();
this.addWindowListener(新的WindowAdapter(){
@凌驾
公共无效窗口关闭(WindowEvent e){
int a=JOptionPane.showConfirmDialog(null,“您确定需要关闭吗?”,“提示”,JOptionPane.YES\u NO\u选项);
如果(a==0){
系统输出打印项次(“是”+a);
System.exit(0);//关闭
}如果(a==1),则为else{
系统输出打印项次(“否”+a);
}
}
});
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//
私有组件(){
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,400,短。最大值)
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0,300,短。最大值)
);
包装();
}//
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示对话框*/
invokeLater(new Runnable()){
@凌驾
公开募捐{
TestClosingDialog=新建TestClosingDialog(新建JFrame(),true);
对话框.setVisible(true);
}
});
}
//变量声明-不修改
//变量结束声明
}
ChangesetDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE)
tosetDefaultCloseOperation(javax.swing.WindowConstants.DO\u NOTHING)代码>
这将需要您手动处理对话框的。根据您的需要,您只需更改System.exit(0)代码>到dispose()代码>
我个人建议不要直接从顶级容器(如JDialog
)进行扩展,但这只是我的想法
我更喜欢使用JPanel
,它有一个可以显示对话框的助手方法。此方法将创建对话框并向其中添加面板,例如更改setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE\u ON\u CLOSE)
tosetDefaultCloseOperation(javax.swing.WindowConstants.DO\u NOTHING)代码>
这将需要您手动处理对话框的。根据您的需要,您只需更改System.exit(0)代码>到dispose()代码>
我个人建议不要直接从顶级容器(如JDialog
)进行扩展,但这只是我的想法
我更喜欢使用JPanel
,它有一个可以显示对话框的助手方法。此方法将创建对话框并向其中添加面板,例如非常感谢,伙计。它起作用了。是的,我按照你的建议使用JPanel在我的常规代码中使用相同的方法。这只是一个测试演示。非常感谢,您为我节省了时间。顺便说一句,您对如何打包和分发swing桌面应用程序有何想法。我已经在netbeans中完成了一个swing项目,现在我需要打包并分发它。我使用mysql数据库。我可以将其打包为exe安装文件或其他文件吗?因此,用户可以像普通windows软件一样安装它,即使他们的计算机上没有JRE环境。提前谢谢。很明显,有很多方法,从干净整洁的建筑开始。我