Java implementation system tray example
- 2020-04-01 03:05:18
- OfStack
The system tray of the desktop is when the program minimizes or the close button the program does not exit, but minimizes the task status area (Windows), when the mouse clicks on the icon in that area there are prompts and other actions. On Microsoft Windows, it is called the Taskbar Status Area, on Gnome it is called the Notification Area, and on KDE it is called the System Tray. The system tray is Shared by all applications running on the desktop.
Two classes have been added to jdk1.6 to implement:
SystemTray and TrayIcon are described in detail below:
SystemTray
Class is introduced:
On some platforms, the SystemTray may not exist or be supported, so use SystemTray. IsSupported () first to check that the current system supports the SystemTray
The SystemTray can contain one or more trayicons, which can be added to the tray using the add(java.awt.trayicon) method, and remove(java.awt.trayicon) when the tray is no longer needed
Remove it.
TrayIcon consists of an image, a pop-up menu, and a set of associated listeners. Each Java application has an instance of the SystemTray, which allows the application to connect to the desktop SystemTray while the application is running.
The SystemTray instance can be obtained using the getSystemTray () method. The application cannot create its own SystemTray
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;
public class TrayByJdk extends JFrame implements ActionListener{
private JPanel pane = null;
private JButton button = null; //Button to launch tray icon
private JLabel label = null; //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; //An example of the operating system tray
boolean gengai = false;
//Jdk1.6 pallet technology to achieve cross-platform applications
public TrayByJdk() {
//Super (" pallet technology demonstration ");
icon1 = new ImageIcon("G:\javaworks\eclipsework\ shanzhai QQClient\images\16.gif"); //Icon to be displayed in tray
icon2 = new ImageIcon("G:\javaworks\eclipsework\ shanzhai QQClient\images\15.gif"); //Icon to be displayed in tray
try {
//Set the LookAndFeel to Windows style
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception ex) {
ex.printStackTrace();
}
pane = new JPanel();
button = new JButton(" Zoom down to pallet ");
button.setEnabled(false);
label = new JLabel(" This operating system 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());
}
private void tray() {
label.setText(" This operating system supports pallets ");
button.setEnabled(true);
tray = SystemTray.getSystemTray(); //Gets an example of the operating system tray
//ImageIcon icon = new ImageIcon("tray.gif"); //Icon to be displayed in tray
PopupMenu pop = new PopupMenu(); //Construct a right-click popup menu
MenuItem show = new MenuItem(" Display window ");
MenuItem exit = new MenuItem(" Exit the demo ");
MenuItem author = new MenuItem("Author");
trayIcon = new TrayIcon(icon1.getImage(), " Pallet technology demonstration ", 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); //Adds the tray icon to the tray instance of the system
setVisible(false); //Make the window invisible
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { //Mouse click
tray.remove(trayIcon); //Remove the tray icon from the tray instance of the system
setVisible(true); //Display window
}
}
});
show.addActionListener(new ActionListener() { //Click the "display window" menu to display the window
public void actionPerformed(ActionEvent e) {
tray.remove(trayIcon); //Remove the tray icon from the tray instance of the system
setVisible(true); //Display window
}
});
exit.addActionListener(new ActionListener() { //Click the "exit demo" menu to launch the program
public void actionPerformed(ActionEvent e) {
System.exit(0); //Exit the program
}
});
author.addActionListener(new ActionListener() { //Click the "exit demo" menu to launch the program
public void actionPerformed(ActionEvent e) {
showMessage();
}
});
pop.add(show);
pop.add(exit);
pop.add(author);
}
private void showMessage() {
JOptionPane.showMessageDialog(this, new JLabel(" This is a system tray "), " The 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();
} //Adds the tray icon to the tray instance of the system
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;
}
}
}