After Java 1.6, you can easily add the tray icon and Tray Icon pop-up menu, the general code is as follows:
If (systemtray. issupported () {// determine whether the system tray URL is supported = This. getclass (). getresource ("/images/trayicon.gif"); // obtain the urlimageicon icon of the image = new imageicon (URL); // instantiate the image object image = icon. getimage (); // obtain the image object trayicon = new trayicon (image); // create a tray icon trayicon. addmouselistener (New mouseadapter () {// Add the mouse adapter public void mouseclicked (mouseevent E) for the tray {// mouse event if (E. getclickcount () = 2) {// determine whether the mouse has been double-clicked showframe (); // call the method display form}); trayicon. settooltip ("System Tray"); // Add the tooltip text popupmenu = new popupmenu (); // create the pop-up menu menuitem exit = new menuitem ("exit "); // create a menu item // response method exit. addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {// todo auto-generated method stub}); popupmenu. add (exit); // Add a menu item trayicon for the pop-up menu. setpopupmenu (popupmenu); // Add systemtray = systemtray to the tray icon. getsystemtray (); // obtain the system tray object try {systemtray. add (trayicon); // Add a tray icon to the system tray} catch (exception e) {e. printstacktrace ();}}
The showframe () method is called to display the main window. Generally:
Public void showframe () {This. setvisible (true); // display the form this. setstate (frame. Normal );}
Notes;
1. images that do not support ICO are most suitable for JPG, GIF, PNG, and 16*16 sizes.
2. JDK must be later than 1.6.
3. This code is generally placed in the constructor. You can add the response method in the pop-up menu.