The system tray is the icon in the lower-right corner of the desktop.
The function of this program is to click the form Close button to not exit the program, but to hide it inside the system tray.
In essence, only the form is not visible ...
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.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
public class Testtray extends JFrame {private static final long serialversionuid = -7078030311369039390l;
Public Testtray () {this.setsize (500, 400); This.setlocationrelativeto (null);//Set the form in the middle of the screen Systemtray (); Set the system tray//Add Shutdown button event, the essence of the shutdown is to hide the form this.addwindowlistener (new Windowadapter () {@Override public void windowclosing (WindowEvent e)
{TestTray.this.setVisible (false);
}
});
This.setvisible (TRUE);
/** * Processing system tray/private void Systemtray () {if (systemtray.issupported ()) {//Determine if the system supports tray function.
Create Pallet Right-click pop-up menu PopupMenu popupmenu = new PopupMenu ();
Create the exit entry in the pop-up menu MenuItem itemexit = new MenuItem ("Exit system"); ItemeXit.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {system.exit (
0);
}
});
Popupmenu.add (Itemexit); Create tray icon ImageIcon icon = new ImageIcon ("Img/icon.png");
Create a Picture object TrayIcon TrayIcon = new TrayIcon (Icon.getimage (), "Test system Tray", popupmenu); Trayicon.addactionlistener (new ActionListener () {@Override public void actionperformed (ActionEvent e) {testtray.t
His.setvisible (TRUE);
}
});
Add tray icon to the system tray//This can be clicked off and then placed into the tray, where the open program directly shows the tray icon a try {Systemtray.getsystemtray (). Add (TrayIcon);
catch (Awtexception E1) {e1.printstacktrace ();
}} public static void Main (string[] args) {new Testtray ();
}
}