JAVA应用 之 快捷工具托盘++

[size=small][color=darkblue]之所以叫《JAVA应用 之 快捷工具托盘++》,是因为昨天在站里看到一哥们发的《快捷工具托盘》[url]http://snkcxy.iteye.com/blog/1810057[/url],自己闲来无事,重构了下他的代码,用Swing写了个UI,本质上还是去调用Runtime执行OS命令。还是一样,opensource,可以下载全部源码, 有图有真相,承认难看的不是有一点:), 本人微博:[url]http://weibo.com/terrancetian[/url],欢迎留言
[/color][/size]
[IMG]http://www.freep.cc/di/L1HW/111.jpg[/IMG]
[size=small][color=darkblue]呵呵,找不到好看的logo,就随便用了新浪微博的。。。[/color][/size]

[IMG]http://www.freep.cc/di/M0ZK/222.jpg[/IMG]
[size=small][color=darkblue]这个关于,还是当年自己写的毕设时用的,直接拉来用了,懒的。。。

哦了,上code,首先,我把那一大堆命令都搞到了资源文件里,然后写个Properties去读:
[/color][/size]

public class PropertiesUtil {

public final static String COMMAND_LIB = "com/objectivasoft/terrance/properties/all_comand.properties";

/**
* 加载属性文件, 初始化命令库
* @param path
* @return
*/
public static Properties getProperties(){
Properties prop = new OrderedProperties();
try {
prop.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(COMMAND_LIB));
} catch (Exception e) {
// logger.error(path + " 加载失败!");
JOptionPane.showMessageDialog(null, "初始化命令库失败!", "ERROR", JOptionPane.ERROR_MESSAGE);
}
return prop;
}

[size=small]这里说明一下[url]http://snkcxy.iteye.com/blog/1810057[/url],这哥们也说到HashMap的问题,我的Properties也是继承自HashTable,都是无需的元素,所以我重写了Properties的主要方法,用LinkedHashSet去拿资源,这样保证map的有序性和资源文件保持一致,不想贴代码了,请参考:[url]http://unmi.cc/ordered-java-properties-class[/url]

下来就是来到久违的java Swing了:[/size]

public class ToolsPanel extends JFrame {
private static final long serialVersionUID = 1541995806128793897L;
//这玩意就是从资源文件里拿出来的map
private static final Properties commandProp = PropertiesUtil.getProperties();
//插件上的按钮
private JButton comandBtns[];

//构造方法
public ToolsPanel() {
super("快捷托盘");
init();
}

@PostConstruct
private void init() {
constractComponent();
constractLayout();
}

/** 构造界面上的按钮 */
private void constractComponent(){
List<String> commands = new ArrayList<String>();
for (Object item : commandProp.keySet()) {
commands.add((String)item);
}
//构造按钮,并注册事件
comandBtns = new JButton[commandProp.size()];
for (int i = 0; i < commands.size(); i++) {
//又是新浪。。。
comandBtns[i] = new JButton(commands.get(i), new ImageIcon("src/com/objectivasoft/terrance/misc/sina.png"));
comandBtns[i].setBackground(Color.LIGHT_GRAY);
//注册点击事件
comandBtns[i].addActionListener(new ComandHandler((String) commandProp.get(comandBtns[i].getText())));
this.add(comandBtns[i]);
}
}

/** 构造界面布局 */
private void constractLayout(){
//网格布局管理器
this.setLayout(new GridLayout(comandBtns.length, 1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setBounds(ComInfo.w/2-150,ComInfo.h/2-200,180,240);
//嘿嘿,直接就用新浪微博的logo了...
String src = "/img/sina.png";
try {
Image image=ImageIO.read(this.getClass().getResource(src));
this.setIconImage(image);
} catch (IOException e) {
e.printStackTrace();
}
}
}


[size=small]这个就是注册按钮事件的实现类,其实我更喜欢写Swing时候用匿名内部类,只不过不便读代码:[/size]

public class ComandHandler implements ActionListener {
private static final Runtime rt = Runtime.getRuntime();
private String comand;//这玩意就是按钮绑定的命令

public ComandHandler(String comand) {
super();
this.comand = comand;
}

@Override
public void actionPerformed(ActionEvent e) {
try {
//关于系统按钮
if (e.getActionCommand().equalsIgnoreCase("\u5173\u4E8E\u63D2\u4EF6")) {
new AboutInfo();
} else if (e.getActionCommand().equalsIgnoreCase("\u9690\u85CF\u5230\u6258\u76D8")) {
//到系统托盘状态,这个就是我重构后的那哥们代码
new SystemToolsTray();
} else {
//执行OS命令
rt.exec(comand);
}
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, "执行系统命令失败!", "错误", JOptionPane.ERROR_MESSAGE);
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "系统异常!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}


[size=small][color=blue]哦了,那哥们的SystemToolsTray类,就不贴了,我主要重构的也就是把那一大堆map从properties里读了下,剩下也没做什么,项目里重要的就是那些东东了,其他的都在项目附件里。对了,运行RunUI.java就是项目入口。

ps:项目没怎么仔细搞,有些bug懒得改了,不过有一个问题还请各位大虾帮忙解决下,就是在系统托盘状态有中文乱码问题,现在还没想到什么办法解决:)
[/color][/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值