java - 为什么关闭一个 jframe 窗口时其他窗口会自动关闭?

标签 java

登录后我正在开发基于 Swing 的应用程序,我有一个菜单窗口,其中包含不同的选项卡,这些选项卡打开不同的窗口,但是当我关闭其中一个选项卡时,会自动关闭所有窗口。请指导我如何解决这个问题?谢谢。

最佳答案

您正在调用 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE),因此当此窗口关闭时,您的整个应用程序将关闭。这是因为(如下所述)System.exit(); 将被调用。

您应该根据应用程序的需要将其他值之一传递给 setDefaultCloseOperation(int operation)

无论如何请阅读The Use of Multiple JFrames, Good/Bad Practice?如果您使用多个框架。

<小时/>

来源setDefaultCloseOperation

public void setDefaultCloseOperation(int operation)

Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices:

  • DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.

  • HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.

  • DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener
    objects.

  • EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

The value is set to HIDE_ON_CLOSE by default. Changes to the value of this property cause the firing of a property change event, with property name "defaultCloseOperation".

关于java - 为什么关闭一个 jframe 窗口时其他窗口会自动关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25820308/

相关文章:

java - XML 验证似乎忽略了一些 XSD 元素

java - recyclerview 中的 Radiogroup 没有状态

java - NodeConnectorRef 的 java 文件在哪里?

java - 无法猜测 Search 5.5 中的 FieldBridge。未索引的字段

java - 在 Android 运行时将字符串编译为代码

java - 服务层和领域模型的问题

java - 卡在 ControllerLinkBuilder 上

java - 类路径中缺少的注释附加到类时的行为规范

java - 启动通过 Apache Cayenne ORM 返回数据的存储过程不起作用

java - String.replaceAll 变体