登录后我正在开发基于 Swing 的应用程序,我有一个菜单窗口,其中包含不同的选项卡,这些选项卡打开不同的窗口,但是当我关闭其中一个选项卡时,会自动关闭所有窗口。请指导我如何解决这个问题?谢谢。
最佳答案
您正在调用 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE)
,因此当此窗口关闭时,您的整个应用程序将关闭。这是因为(如下所述)System.exit();
将被调用。
您应该根据应用程序的需要将其他值之一传递给 setDefaultCloseOperation(int operation)
。
无论如何请阅读The Use of Multiple JFrames, Good/Bad Practice?如果您使用多个框架。
<小时/>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/