Java application system tool quick tray instance code

Source: Internet
Author: User

1. Open various System Tools
2. Timed Shutdown (for restart and sleep failure, refer to the above twoArticleSelf-scaling)
3. Simple File Operations

CopyCode The 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 = 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 was ineffective)
2. garbled characters may occur when running the eclipse environment. Changing the encoding in the runtime environment configuration can solve this problem.
3. it is only a demonstration of the Program (which reflects the basic idea) and is not perfect, if you like it, you can complete it yourself
. the order of Menu Generation may be out of order, because map is used, and the traversal is out of order.
B. You can add multi-level menus to classify menus, which improves the user experience.
C. can be packaged into a jar package or EXE for 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.