Java implementation hides the form to the system tray method _java

Source: Internet
Author: User

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 ();

 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.