JAVA application system tool quick tray instance code, java Tray

Source: Internet
Author: User

JAVA application system tool quick tray instance code, java Tray

1. Open various System Tools
2. Timed Shutdown (restart and sleep are not implemented. Please refer to the above two articles to expand your own)
3. Simple File Operations

Copy codeThe Code is as follows:
[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;
/**
* @ Author cxy
* System Tool quick Tray
*/
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 (); // java Runtime Environment instance
SystemTray tray = SystemTray. getSystemTray (); // create a system tray
PopupMenu trayMenu = new PopupMenu (); // right-click the create tray menu

// Initialize the command Library
CommandMap. put ("Calculator", "calc ");
CommandMap. put ("notepad", "notepad ");
CommandMap. put ("Task Manager", "taskmgr ");
CommandMap. put ("drawing tool", "mspaint ");
CommandMap. put ("Open QQ", "C: \ Program Files (x86) \ Tencent \ QQ \ QQProtect \ Bin \ QQProtect.exe ");
CommandMap. put ("Access File", "cmd/c d: \ cxyCommandShow.txt ");
CommandMap. put ("Timed shutdown", "shutdown-s-t 600 ");
CommandMap. put ("cancel shutdown", "shutdown-");

// Automatically generate the right-click menu of the tray and bind the event (execute the command)
For (final String one: commandMap. keySet ())
{
MenuItem item = new MenuItem (one );
Item. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
Try
{
Rt.exe c (commandMap. get (one ));
} Catch (IOException e1)
{
E1.printStackTrace ();
}
}
});
TrayMenu. add (item );
}

MenuItem exitItem = new MenuItem ("quit ");
ExitItem. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
System. exit (0 );
}
});
TrayMenu. add (exitItem );

Image image = Toolkit. getdefatooltoolkit (). getImage ("src/com/cxy/f/play.png"); // load the Image
TrayIcon trayIcon = new TrayIcon (image, "Quick tool", trayMenu); // create a trayIcon
Tray. add (trayIcon );
}
}
 
Note:
1. Remember to change the icon path to your own. Otherwise, the generated tray does not have an icon (as if it had no effect)
2. garbled characters may occur when the eclipse environment is running. Changing the encoding in the runtime environment configuration can solve this problem.
3. As it is only a demo program (which reflects the basic idea), it is not perfect. If you like it, you can improve it on your own.
A. The order of Menu Generation may be out of order, because map is used and unordered is traversed.
B. You can add multi-level menus to classify menus, so that the user experience will be better.
C. You can compress it into a jar package or exe to facilitate normal use.
D. Excellent user experience and performance. (This is not specific here)
4. If you have time, I will complete the program and compress it into a jar package for your use.

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.