Java Implementation System Tray sample _java

Source: Internet
Author: User

The desktop's system tray, when the program is minimized or the Shutdown button program does not exit, is minimized in the Task status area (Windows system) when the mouse clicks on the area where the icon is prompted as well as other actions. On Microsoft Windows, it is referred to as the "taskbar status area (Taskbar)", which, on Gnome, is called the "notification area", which, on KDE, becomes the "system tray" (Syst EM Tray) ". The system tray is shared by all applications running on the desktop.
There are two new classes in jdk1.6 to implement:
Systemtray and TrayIcon, the following is a detailed description:

Systemtray
Class Description:
On some platforms, the system tray may not exist or is not supported, so first use systemtray.issupported () to check whether the current system supports the system tray

Systemtray can contain one or more trayicon, you can add them to the pallet using the Add (Java.awt.TrayIcon) method, and remove (Java.awt.TrayIcon) when the pallet is no longer needed
Remove it.

The TrayIcon consists of images, pop-up menus, and a set of related listeners. Each Java application has a Systemtray instance that allows the application to connect to the desktop system tray when the application is running.
Systemtray instances can be obtained by means of the Getsystemtray () method. The application cannot create its own systemtray

Copy Code code as follows:

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.MouseAdapter;
Import java.awt.event.MouseEvent;
Import java.awt.event.WindowEvent;
Import Java.awt.event.WindowStateListener;

Import Javax.swing.ImageIcon;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JLabel;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;
Import Javax.swing.Timer;
Import Javax.swing.UIManager;

/**
*
* @author Mr.liutao
*/
public class TRAYBYJDK extends JFrame implements actionlistener{

Private JPanel pane = null;
Private JButton button = null; button to start tray icon
Private JLabel label = NULL; Information to show whether the system supports pallets
Private TrayIcon TrayIcon = null; Tray icon
Private Timer Shanshuo = null;
Private ImageIcon icon1 = null;
Private ImageIcon icon2 = null;
Private Systemtray tray = null; Examples of this operating system tray
Boolean Gengai = false;
Using jdk1.6 pallet technology to realize cross-platform application
Public Traybyjdk () {
Super ("Pallet Technology demo");
Icon1 = new ImageIcon ("G:\\javaworks\\eclipsework\\ cottage qqclient\\images\\16.gif"); The icon to be displayed in the tray
Icon2 = new ImageIcon ("G:\\javaworks\\eclipsework\\ cottage qqclient\\images\\15.gif"); The icon to be displayed in the tray
try {
Set Lookandfeel to Windows style
Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
catch (Exception ex) {
Ex.printstacktrace ();
}
pane = new JPanel ();
button = new JButton ("Shrink to tray");
Button.setenabled (FALSE);
label = new JLabel ("This OS does not support pallets");
Pane.add (label);
Pane.add (button);
Determine if pallets are supported
if (systemtray.issupported ()) {
This.tray ();
}
Shanshuo = new Timer (1000,this);
Shanshuo.start ();
This.getcontentpane (). Add (pane);
This.setdefaultcloseoperation (Jframe.exit_on_close);
This.setsize (300, 200);
This.addwindowstatelistener (New Windowstatelistener () {
public void windowstatechanged (WindowEvent state) {
if (state.getnewstate () = 1 | | state.getnewstate () = 7) {
Yinca ();
}
}
});

This.setvisible (FALSE);
System.out.println ("==============" +this.isvisible ());
}

/**
* Pallet Related code
*/
private void tray () {
Label.settext ("Support tray for this operating system");
Button.setenabled (TRUE);
Tray = Systemtray.getsystemtray (); Get an example of this operating system tray
ImageIcon icon = new ImageIcon ("Tray.gif"); The icon to be displayed in the tray
PopupMenu pop = new PopupMenu (); Construct a right-click pop-up menu
MenuItem show = new MenuItem ("Display window");
MenuItem exit = new MenuItem ("Exit demo");
MenuItem author = new MenuItem ("author");
/**
* TrayIcon has three constructs
* TrayIcon (image image) with "icon" to construct
* TrayIcon (image image, String tooltip) constructed with "Icons" and "tooltip"
* TrayIcon (image image, String tooltip, PopupMenu popup) constructs a tray icon with "icon", "tooltip", "pop-up menu"
*/
TrayIcon = new TrayIcon (Icon1.getimage (), "Tray technology demo", Pop);
After clicking this button, the window is closed and the tray icon is added to the tray of the system.
Button.addactionlistener (new ActionListener () {

public void actionperformed (ActionEvent e) {

try {
Tray.add (TrayIcon); To add a tray icon to the tray instance of the system
SetVisible (FALSE); Make a window not view
catch (Awtexception ex) {
Ex.printstacktrace ();
}
}
});
/**
* Add mouse Listener, the default display window when the mouse is double-clicked on the tray icon
*/
Trayicon.addmouselistener (New Mouseadapter () {

            public void mouseclicked (MouseEvent e) {
                if (E.getclickcount () = 2) {//mouse double-click
                     Tray.remove (TrayIcon); Remove the tray icon from the system's pallet instance
                     setvisible (TRUE); Display window
               }
           }
        });
        Show.addactionlistener (new ActionListener () {//Click the "Show window" menu to display the window

            public void actionperformed (ActionEvent e) {
                Tray.remove ( TrayIcon); Remove the tray icon from the system's pallet instance
                 setvisible (TRUE); Display window
           }
        });
        Exit.addactionlistener (new ActionListener () {//click "Exit Demo" menu to launch program

public void actionperformed (ActionEvent e) {
System.exit (0); Exit program
}
});
Author.addactionlistener (new ActionListener () {//click "Exit Demo" menu and Launch program

public void actionperformed (ActionEvent e) {
ShowMessage ();
}
});
Pop.add (show);
Pop.add (exit);
Pop.add (author);
}
/**
* Display Information
*/
private void ShowMessage () {
Joptionpane.showmessagedialog (this, the new JLabel ("This is a system tray"), "message", joptionpane.information_message);
}
public static void Main (string[] args) {
New Traybyjdk (). Yinca ();
}
public void Yinca () {
try {
Tray.add (TrayIcon);
catch (Awtexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
///Add tray icon to system tray instance
SetVisible (FALSE); Make a window not view
}
@Override
public void actionperformed (ActionEvent arg0) {
if (!gengai) {
Trayicon.setimage (Icon2.getimage ());
Gengai = true;
}else{
Trayicon.setimage (Icon1.getimage ());
Gengai = false;
}
}
}

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.