Using Java to achieve the system tray function of the introduction of of source code and screenshots

  • 2020-04-01 01:55:08
  • OfStack

How to implement the system tray function in Java.

figure

Project package structure diagram

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050618015520.jpg ">   < img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050618015521.jpg ">

System operation screenshot

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/2013050618015522.jpg ">

Application of the core logic, hidden to the tray is the essence of the hidden form. That is, setVisible(false), the display of the form is that setVisible(true).

The project 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 = -7078030311369039390L;
     private JMenu menu;
     private JMenuBar jmenuBar;
     private String [] jmItemName = {" Placed in the tray "," System out "};

     public MainFrame(){
         super(" Phone book ");
         init();
         this.setSize(500,400);
         this.setJMenuBar(jmenuBar);
         this.setLocationRelativeTo(null);
         systemTray();    //The system tray
     }

     
     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 actionPerformed(ActionEvent e) {
         String actions = e.getActionCommand();
         if(" Placed in the tray ".equals(actions)){
             this.setVisible(false);
         }
         if(" System out ".equals(actions)){
             System.exit(0);
         }

     }

     
     private void  systemTray(){
         if(SystemTray.isSupported()){    //Determine whether the system supports the tray function.
             URL resource = this.getClass().getResource("systray.jpg");    //Get image path
             ImageIcon icon = new ImageIcon(resource); //Create a picture object
             PopupMenu popupMenu = new PopupMenu(); //Create a pop-up menu object
             MenuItem itemExit = new MenuItem(" Log out ");    //Creates the exit item in the pop-up menu
             MenuItem itemShow = new MenuItem(" According to the form "); //Creates a display main form item in the pop-up menu.
             itemExit.addActionListener(new ActionListener() {     //Add an event listener to the exit
                 @Override
                 public void actionPerformed(ActionEvent e) {
                     System.exit(0);
                 }            
             });
             itemShow.addActionListener(new ActionListener() { //Add an event listener to minimize the form.
                 @Override
                 public void actionPerformed(ActionEvent e) {
                     setVisible(true);
                 }
             });
             popupMenu.add(itemExit);
             popupMenu.add(itemShow);
             TrayIcon trayIcon = new TrayIcon(icon.getImage()," Telephone directory system ",popupMenu);
             SystemTray sysTray = SystemTray.getSystemTray();
             try {
                 sysTray.add(trayIcon);
             } catch (AWTException e1) {    }
         }
     }

     
     public static void main(String[] args) {

         new MainFrame().setVisible(true);

     }

 }


Related articles: