Introduction to using Java to implement the system tray function (with source code and screenshots)

Source: Internet
Author: User

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

}

}

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.