Display of tray icon for Java program System tray

Source: Internet
Author: User
Import java.awt.AWTException;
Import Java.awt.Image;
Import Java.awt.MenuItem;
Import Java.awt.PopupMenu;
Import Java.awt.SystemTray;
Import Java.awt.Toolkit;
Import Java.awt.TrayIcon;
Import java.awt.event.ActionEvent;

Import Java.awt.event.ActionListener;


Import Javax.swing.JFrame;
	public class Mysystemtray extends jframe{public mysystemtray () {init ();
		public void init () {this.setsize (300, 200);
		This.setlocationrelativeto (NULL);
		This.settray ();
	This.setvisible (TRUE);
			//Add pallet display: 1. First determine if the current platform supports pallet display public void Settray () {if (systemtray.issupported ()) {/////Determine whether the current platform supports pallet functionality//Create pallet instances
			Systemtray tray = Systemtray.getsystemtray (); Create tray Icon: 1. Display icon Image 2. Stop tip text 3. pop-up menu PopupMenu 4. Create a tray icon instance//1
			Etimage ("trayiconimage/clienticon.jpg");
			2. Stop hint text String = "Mysystemtray";
			3. Pop-up menu PopupMenu popupmenu popmenu = new PopupMenu ();
			MenuItem Itmopen = new MenuItem ("open"); Itmopen. addActionListener (New ActionListener () {public void actionperformed (ActionEvent e) {show ();
			}				
			});
			MenuItem itmhide = new MenuItem ("hidden");
				Itmhide.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {unvisible ();
			}
			});
			MenuItem itmexit = new MenuItem ("Exit");
				Itmexit.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {Exit ();
			}
			});
			Popmenu.add (Itmopen);
			Popmenu.add (Itmhide);
			
			Popmenu.add (Itmexit);
			Create tray icon TrayIcon TrayIcon = new TrayIcon (image,text,popmenu);
			Add the tray icon to the tray to try {tray.add (TrayIcon);
			catch (Awtexception E1) {e1.printstacktrace ();
	The external class (this cannot point to) public void unvisible () {this.setvisible (false) cannot be directly invoked in the inner class.
	public void Show () {this.setvisible (true);
	public void Exit () {system.exit (0);
	public static void Main (string[] args) {new Mysystemtray (); }
}

Source: http://csuliunian.iteye.com/blog/1154407
Related Article

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.