我在我的 java 应用程序中使用了一个系统托盘。我想消失 GUI 并在后台 运行应用程序,但是当用户单击 JFrame 的关闭 按钮时系统托盘必须保持可用。
最佳答案
I want to disappear the GUI and run the application in background, but system tray must remain available when user click on close button of JFrame.
- 设置正确JFrames method for
DefaultCloseOperation
,JFrame.setDefaultCloseOperation(HIDE_ON_CLOSE)
, 默认在API中实现
设置当用户在此框架上启动“关闭”时默认发生的操作。您必须指定以下选项之一:
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".
然后从
访问SystemTray
调用JFrame.setVisible(true)
,此事件可从
关于java - 关闭 JFrame 时如何在后台运行应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15441431/