问题
I used a system tray in my java application. 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.
回答1:
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.
- set proper JFrames method for DefaultCloseOperation,
JFrame.setDefaultCloseOperation(HIDE_ON_CLOSE)
, by default implemented in API
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".
then from SystemTray to call
JFrame.setVisible(true)
, this event is accesible fromTrayIcon
JPopupMenu added to SystemTray
回答2:
Posting this as an answer
Just like MadProgrammer said:
Don't set the frame to EXIT_ON_CLOSE or call System.exit when the frame is closed. The event dispatching thread will continue to run until the JVM is terminated
回答3:
You can have the System Tray available in a separate thread.
来源:https://stackoverflow.com/questions/15441431/how-to-run-application-in-background-when-close-the-jframe