【CXY】JAVA应用 之 快捷工具托盘

说明:

    1.本文是 《JAVA基础 之 Runtime》 的实例应用,阅读前建议先访问下面两篇文章

        《JAVA基础 之 Runtime》《windows7 常用简单命令总结》

    2.功能:

           1.打开各种系统工具

           2.定时关机(重启、睡眠未实现 请参照上面两个文章自行扩展)

           3.简单文件操作

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
 * 系统工具快捷托盘
 */
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运行环境实例
		SystemTray tray = SystemTray.getSystemTray();  //创建系统托盘
		PopupMenu trayMenu= new PopupMenu();  //创建托盘右键菜单
		
		//初始化命令库
		commandMap.put("计算器", "calc");
		commandMap.put("记事本", "notepad");
		commandMap.put("任务管理器", "taskmgr");
		commandMap.put("画图工具", "mspaint");
		commandMap.put("打开QQ", "C:\\Program Files (x86)\\Tencent\\QQ\\QQProtect\\Bin\\QQProtect.exe");
		commandMap.put("创建文件", "cmd /c echo 请关注我的博客 http://snkcxy.iteye.com/>d:\\cxyCommandShow.txt");
		commandMap.put("访问文件", "cmd /c d:\\cxyCommandShow.txt");
		commandMap.put("定时关机", "shutdown -s -t 600");
		commandMap.put("取消关机", "shutdown -a");
		
		//自动生成托盘右键菜单并绑定事件(执行命令)
		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("退出");
		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");  //载入图片
		TrayIcon trayIcon = new TrayIcon(image, "快捷工具", trayMenu);  //创建trayIcon
		tray.add(trayIcon);
	}
}

注意:

    1.记得改成自己的图标路径,否则生成的托盘没有图标(好似没效果)

    2.eclipse环境运行的时候可能会出现乱码问题,改变运行环境配置中的编码可以解决。

    3.由于只是一个演示程序(体现的是基本思路),并没有做到很完善,如果喜欢可自行完善

        a.菜单生成的顺序可能是乱序,因为使用的是map,遍历出来的是无序的。

        b.可以加多级菜单 对菜单进行分类,这样用户体验会好些。

        c.可以打成jar包 或者 exe 方便平时使用。

        d.其他用户体验 和 性能方面的完善。(这里就不具体说了)

    4.如果有时间的话我会将这个程序完善,并打成jar包供大家使用。

    5.例子中会创建一个文本文件 路径是:d:\\cxyCommandShow.txt 里面有我博客的地址,欢迎大家访问交流(这个文件记得自己删除哦~)

 

声明:

1.原创文章,转载请标明并加本文连接。

2.更多的文章请访问我的博客 http://blog.csdn.net/luckcxy

3.文章反映个人愚见,如有异议欢迎讨论指正 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } } package com.test; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class B { public static void main(String[] args) { File file = new File("c:\\a.jpg"); FileInputStream is = null; try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); // return rect; } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); } catch (IOException e1) { e1.printStackTrace(); // return rect; } System.out.println("width = " + sourceImg.getWidth() + "height = " + sourceImg.getHeight()); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值