Java Application System Tool shortcut Tray Instance Code _java

Source: Internet
Author: User

1. Open various System Tools
2. Timed shutdown (reboot, sleep not implemented, please refer to the above two articles to expand themselves)
3. Simple File operation

Copy Code code 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 Tools Shortcut 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 Running Environment instance
Systemtray tray = Systemtray.getsystemtray (); Create System Tray
PopupMenu traymenu= new PopupMenu (); Create a tray Right-click menu

Initialize 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-a");

Automatically generate tray right-click menu and bind event (Execute command)
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
TrayIcon TrayIcon = new TrayIcon (Image, "shortcut tool", traymenu); Create TrayIcon
Tray.add (TrayIcon);
}
}

Attention:
1. Remember to change to their own icon path, otherwise the resulting tray does not have an icon (seems to have no effect)
2.eclipse environment may appear garbled problem, change the configuration of the operating environment can be solved by coding.
3. As just a demo program (embodies the basic idea), and did not do very well, if you like to perfect yourself
A. The order in which the menu is generated may be disorderly, because the map is used and the traversal is unordered.
B. Menus can be categorized with multilevel menus so that the user experience is better.
C. Can be used as a jar bag or EXE to facilitate normal use.
D. Other user experience and performance improvements. (It's not specifically said here)
4. If there is time, I will improve the program and make a jar package for everyone to 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.