How to implement the system tray function in Java.
Example
Project Package Structure
System Operation
The core logic of the application indicates that hiding in the tray is actually about hiding the form. That is, setVisible (false), and displaying the form is about setVisible (true ).
The project code is as follows:
Copy codeThe Code is as follows: package org. pdp. frame;
Import java. awt. AWTException;
Import java. awt. MenuItem;
Import java. awt. PopupMenu;
Import java. awt. SystemTray;
Import java. awt. TrayIcon;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java.net. URL;
Import javax. swing. ImageIcon;
Import javax. swing. JFrame;
Import javax. swing. JMenu;
Import javax. swing. JMenuBar;
Import javax. swing. JMenuItem;
Public class MainFrame extends JFrame implements ActionListener {
Private static final long serialVersionUID =-707803031110839390l;
Private JMenu menu;
Private JMenuBar jmenuBar;
Private String [] jmItemName = {"place on Tray", "system exited "};
Public MainFrame (){
Super ("phone book ");
Init ();
This. setSize (500,400 );
This. setJMenuBar (jmenuBar );
This. setLocationRelativeTo (null );
SystemTray (); // System Tray
}
/**
* Initialization Interface
*/
Public void init (){
Menu = new JMenu ("system form ");
For (int I = 0; I <jmItemName. length; I ++ ){
JMenuItem menuItem = new JMenuItem (jmItemName [I]);
MenuItem. addActionListener (this );
Menu. add (menuItem );
}
This. jmenuBar = new JMenuBar ();
This. jmenuBar. add (menu );
}
@ Override
Public void actionreceivmed (ActionEvent e ){
String actions = e. getActionCommand ();
If ("place on Tray". equals (actions )){
This. setVisible (false );
}
If ("system exited". equals (actions )){
System. exit (0 );
}
}
/** System Tray Icon processing .*/
Private void systemTray (){
If (SystemTray. isSupported () {// determine whether the system supports the tray function.
URL resource = this. getClass (). getResource ("systray.jpg"); // obtain the image path
ImageIcon icon = new ImageIcon (resource); // create an image object
PopupMenu popupMenu = new PopupMenu (); // create a pop-up menu object
MenuItem itemExit = new MenuItem ("exit system"); // create an exit item in the pop-up menu
MenuItem itemShow = new MenuItem ("display form"); // create a display main form item in the pop-up menu.
ItemExit. addActionListener (new ActionListener () {// Add event listening to the exit Image
@ Override
Public void actionreceivmed (ActionEvent e ){
System. exit (0 );
}
});
ItemShow. addActionListener (new ActionListener () {// Add event listening to the form to minimize the need.
@ Override
Public void actionreceivmed (ActionEvent e ){
SetVisible (true );
}
});
PopupMenu. add (itemExit );
PopupMenu. add (itemShow );
TrayIcon trayIcon = new TrayIcon (icon. getImage (), "Phone Book System", popupMenu );
SystemTray restart Ray = SystemTray. getSystemTray ();
Try {
Using Ray. add (trayIcon );
} Catch (AWTException e1 ){}
}
}
/**
* Main method
* @ Param args
*/
Public static void main (String [] args ){
New MainFrame (). setVisible (true );
}
}