JFrame的默认关闭操作不起作用 [英] Default Close Operation of a JFrame isn't working

查看:206
本文介绍了JFrame的默认关闭操作不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NetBeans。
我的 JFrame 默认具有关闭操作: EXIT_ON_CLOSE
我有一个按钮应该关闭此 JFrame 上的程序,这是执行的操作代码:

I'm using NetBeans. My JFrame has as default close operation: EXIT_ON_CLOSE. I have a button that is supposed to close the program on this JFrame, this is the action performed code:

private void bSalirActionPerformed(java.awt.event.ActionEvent evt){                                       
    this.dispose();
}  

我认为这会关闭全部 JFrame 的程序,但没有,您能解释一下原因或解决方法吗?

I thought this would close all the JFrames of the program but it doesn't, could you explain me why or how to fix it?

推荐答案

如果您要使用按钮退出应用程序,则可以使用 System.exit() frame.dispose()

If you want to exit your application with the button than you can use System.exit() or frame.dispose().

,但请注意 System.exit(),因为这将终止 JVM

but be careful with System.exit() as this will terminate the JVM.

因此最好在此之前先向用户确认。
JOptionPane.showConfirmDialog();

So it is better to first confirm from user before this. with JOptionPane.showConfirmDialog();

private void bSalirActionPerformed(java.awt.event.ActionEvent evt){ 

        int exit = JOptionPane.showConfirmDialog(
                frame,
                "Are you sure you want to exit the application?",
                "Exit Application",
                JOptionPane.YES_NO_OPTION);                             
        if(JOptionPane.YES_OPTION == exit){
             frame.dispose(); // or System.exit(1);
        }
    }

这篇关于JFrame的默认关闭操作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆