The system tray on the desktop means that when the program is minimized or the close button is pressed, the program does not exit. Instead, it is minimized in the task status area (Windows system). When the mouse clicks on the icon in that area, there are prompts and other operations. On Microsoft Windows it is called the "Taskbar Status Area", on Gnome it is called the "Notification Area", on KDE it is called the "System Tray" Tray)". The system tray is shared by all applications running on the desktop.
Two new classes have been added in jdk1.6 to implement:
SystemTray and TrayIcon, the following is a detailed introduction:
SystemTray
Class introduction:
On some platforms, the system tray may not exist or be supported, so first use SystemTray.isSupported() to check whether the current system supports the system tray.
SystemTray can contain one or more TrayIcon, which can be added to the tray using the add(java.awt.TrayIcon) method. When the tray is no longer needed, use remove(java.awt.TrayIcon)
Remove it.
TrayIcon consists of an image, a popup menu, and a set of related listeners. Every Java application has a SystemTray instance, which allows the application to establish a connection to the desktop system tray while the application is running.
SystemTray instances can be obtained through the getSystemTray () method. Application cannot create its own SystemTray
Copy the 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 the tray icon
private JLabel label = null; // Used to display information about whether the system supports trays
private TrayIcon trayIcon = null; // tray icon
private Timer shanshuo = null;
private ImageIcon icon1 = null;
private ImageIcon icon2 = null;
private SystemTray tray = null; //Instance of this operating system tray
boolean gengai = false;
//Using jdk1.6's tray technology to achieve cross-platform applications
public TrayByJdk() {
//super("Pallet technology demonstration");
icon1 = new ImageIcon("G://javaworks//eclipsework//shanzhaiQQClient//images//16.gif"); // The icon to be displayed in the tray
icon2 = new ImageIcon("G://javaworks//eclipsework//shanzhaiQQClient//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 operating system does not support the tray");
pane.add(label);
pane.add(button);
//Determine whether the tray is 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 codes
*/
private void tray() {
label.setText("This operating system supports tray");
button.setEnabled(true);
tray = SystemTray.getSystemTray(); // Get the tray instance of this operating system
//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("Show window");
MenuItem exit = new MenuItem("Exit presentation");
MenuItem author = new MenuItem("Author");
/**
* TrayIcon has three structures
* TrayIcon(Image image) is constructed with "icon"
* TrayIcon(Image image, String tooltip) is constructed with "icon" and "ToolTip"
* TrayIcon(Image image, String tooltip, PopupMenu popup) uses "icon", "ToolTip" and "popup menu" to construct a tray icon
*/
trayIcon = new TrayIcon(icon1.getImage(), "Tray technology demonstration", pop);
// After clicking this button, the window is closed and the tray icon is added to the system tray.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
tray.add(trayIcon); // Add the tray icon to the system's tray instance
setVisible(false); // Make the window invisible
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
/**
* Add a mouse listener, when the mouse double-clicks on the tray icon, the window will be displayed by default
*/
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 tray 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 tray instance
setVisible(true); // Display window
}
});
exit.addActionListener(new ActionListener() { // Click the "Exit Demo" menu to exit the program
public void actionPerformed(ActionEvent e) {
System.exit(0); //Exit the program
}
});
author.addActionListener(new ActionListener() { // Click the "Exit Demo" menu to exit the program
public void actionPerformed(ActionEvent e) {
showMessage();
}
});
pop.add(show);
pop.add(exit);
pop.add(author);
}
/**
* Display information
*/
private void showMessage() {
JOptionPane.showMessageDialog(this, 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 the tray icon to the system's tray instance
setVisible(false); // Make the window invisible
}
@Override
public void actionPerformed(ActionEvent arg0) {
if(!gengai){
trayIcon.setImage(icon2.getImage());
gengai = true;
}else{
trayIcon.setImage(icon1.getImage());
gengai = false;
}
}
}