JAVA application system tool shortcut tray instance code

  • 2020-04-01 01:30:49
  • OfStack

  1. Open various system tools
                    2. Shutdown on a regular basis (please refer to the above two articles for expansion if reboot or sleep is not realized)
                    3. Simple file operation


[java]  
package com.cxy.f;  

import java.awt.Image;  
import java.awt.MenuItem;  
import java.awt.PopupMenu;  
import java.awt.SystemTray;  
import java.awt.Toolkit;  
import java.awt.TrayIcon;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.io.IOException;  
import java.util.HashMap;  
import java.util.Map;  
  
public class SystemToolsTray  
{  
    public static Runtime rt;  
    public static Map<String,String> commandMap=new HashMap<String, String>();  

    public static void main(String[] args) throws Exception  
    {  
        rt=Runtime.getRuntime();  //Example of a Java runtime environment & NBSP;
        SystemTray tray = SystemTray.getSystemTray();  //Create system tray & cake;
        PopupMenu trayMenu= new PopupMenu();  //Create tray right-click menu & menu;

        //Initialize the command base & NBSP;
        commandMap.put(" The calculator ", "calc");  
        commandMap.put(" notepad ", "notepad");  
        commandMap.put(" Task manager ", "taskmgr");  
        commandMap.put(" Drawing tools ", "mspaint");  
        commandMap.put(" Open the QQ", "C:\Program Files (x86)\Tencent\QQ\QQProtect\Bin\QQProtect.exe");  
        commandMap.put(" Access to the file ", "cmd /c d:\cxyCommandShow.txt");  
        commandMap.put(" Timing shutdown ", "shutdown -s -t 600");  
        commandMap.put(" Cancel shutdown ", "shutdown -a");  

        //Automatically generates a tray right-click menu and binds events (execute commands) & NBSP;
        for(final String one : commandMap.keySet())  
        {  
            MenuItem item = new MenuItem(one);  
            item.addActionListener(new ActionListener() {  
                public void actionPerformed(ActionEvent e) {  
                    try  
                    {  
                        rt.exec(commandMap.get(one));  
                    } catch (IOException e1)  
                    {  
                        e1.printStackTrace();  
                    }  
                }  
            });  
            trayMenu.add(item);  
        }  

        MenuItem exitItem = new MenuItem(" exit ");  
        exitItem.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
                System.exit(0);  
            }  
        });  
        trayMenu.add(exitItem);  

        Image image = Toolkit.getDefaultToolkit().getImage("src/com/cxy/f/play.png");  //Load picture & cake;
        TrayIcon trayIcon = new TrayIcon(image, " Fast tool ", trayMenu);  //Create trayIcon 
        tray.add(trayIcon);  
    }  
}  
 

Note:
      1. Remember to change the icon path to your own, otherwise the generated tray has no icon (it seems to have no effect)
      2. When the eclipse environment is running, there may be garbled code problems, which can be solved by changing the coding in the runtime environment configuration.
      3. Because it is just a demonstration program (which reflects the basic idea), it has not been very perfect, if you like, you can improve it by yourself
              A. The order of menu generation may be out of order, because map is used and traversal results in disorder.
              B. You can add multilevel menu to classify the menu, so that the user experience will be better.
              C. Can be used as jar or exe for convenience.
              D. Other improvements in user experience and performance. (I won't go into details here.)
      4. If I have time, I will perfect the program and make it into a jar for everyone to use.


Related articles: