posts - 59, comments - 244, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

一个快捷打开工具的实现

Posted on 2010-01-26 13:01 penngo 阅读(1869) 评论(3)  编辑  收藏 所属分类: Java
看了下,已经差不多三个星期没有写过文章了。今天主要介绍下自己之前写的一个小工具,本来是想用QT做的,但做了两个小时,也没出什么成果(......学艺未精),改用swing试下,发现一个小时就能做好大半功能,所以最终用swing实现了它。不过这个工具纯属无聊之作,纯属做出来玩玩的。

平时工作,经常要打开不同的文件夹找东西,打开多了,占满任务栏的位置,自己也觉得乱。而且每次打开文件夹时,都要经历“我的电脑→E盘→project......”,这个让我觉得太麻烦。因此自己做个小工具,实现快捷打开文件夹、程序的功能。

整个程序的主角是Runtime.getRuntime().exec()的使用,不懂Runtime.getRuntime().exec(),请自己Google、Baidu了解下,这样不作解释。

啥也不说,先上两个图:


先介绍点击列表时执行的代码,主要实现功能:鼠标经过时,选项背景色会改变;单击选项时,执行Runtime.getRuntime().exec()记录在Item中的命令代码。
list.addMouseListener(new MouseListener() {
            
public void mouseClicked(MouseEvent e) {
                Object item 
= list.getSelectedValue();
                
                
if (item != null && item instanceof ItemStruct) {
                    ItemStruct is 
= (ItemStruct) item;
                
                    
try {
                        
if (is.getDir() == null || is.getDir().equals("")) {
                            Process process 
= Runtime.getRuntime().exec(
                                    is.getCmd());
                        } 
else if (!is.getDir().equals("")) {
                            File dir 
= new File(is.getDir());
                            Process process 
= Runtime.getRuntime().exec(
                                    is.getCmd(), 
null, dir);
                        }
                    } 
catch (Exception ex) {
                        javax.swing.JOptionPane.showMessageDialog(
null"打开"
                                
+ is.getLabel() + "失败!");
                        ex.printStackTrace();
                    }
                    list.setSelectedIndex(
-1);
                }
            }

            
public void mouseExited(MouseEvent e) {
                list.clearSelection();
            }
        });
        list.addMouseMotionListener(
new MouseMotionListener() {
            
public void mouseDragged(MouseEvent e) {

            }

            
public void mouseMoved(MouseEvent e) {
                
int i = list.locationToIndex(e.getPoint());
                
if (i > -1) {
                    list.setSelectedIndex(i);
                }
            }
        });

列表Item记录的数据和命令(这里只是随便列出几个命令给大家参考,需要其它打开命令的,可以自行添加)
public static List<ListPanelItem> panelItemList = new ArrayList<ListPanelItem>();
    
static{
        ListPanelItem panelItem 
= null;
        
        panelItem 
= new ListPanelItem();
        panelItem.setName(
"我的电脑");
        File[] roots 
= File.listRoots();
        ItemStruct[] itemStructs 
= new ItemStruct[roots.length + 3];
        
for(int i = 0; i < roots.length; i ++){

            String totalSpace 
= calculateSpace(roots[i].getTotalSpace());
            String usableSpace 
= calculateSpace(roots[i].getUsableSpace());
            String name 
= roots[i].toString().replace("\\""");
            itemStructs[i] 
= new ItemStruct( name + "盘 (" + usableSpace +"" + totalSpace,"explorer.exe " + roots[i].toString());
        }
        
//windows命令行代码,需要其它的话,自行搜索下window命令行命令。
        itemStructs[roots.length ] = new ItemStruct("网上邻居","explorer.exe ::{208D2C60-3AEA-1069-A2D7-08002B30309D}");
        itemStructs[roots.length 
+ 1= new ItemStruct("回收站","explorer.exe ::{645FF040-5081-101B-9F08-00AA002F954E}");
        itemStructs[roots.length 
+ 2= new ItemStruct("记事本","notepad");
        panelItem.setItems(itemStructs);        
        panelItemList.add(panelItem);
        
        panelItem 
= new ListPanelItem();
        panelItem.setName(
"关机管理");
        panelItem.setItems(
new ItemStruct[]
                {
                
new ItemStruct("关机","Shutdown.exe -s -t 00"),
                
new ItemStruct("重启","Shutdown.exe -r -f -t 00"),
                
new ItemStruct("取消关机","shutdown -a")
                }
        );        
        panelItemList.add(panelItem);

        panelItem 
= new ListPanelItem();
        panelItem.setName(
"应用程序");
        panelItem.setItems(
new ItemStruct[]
                {
                
//这个是我本机的Tomcat安装位置
                new ItemStruct("启动Tomcat",
                        
"cmd /c E:\\project\\server\\bin\\startup.bat",
                        
"E:\\project\\server\\bin"),
                
                }
        );    
        panelItemList.add(panelItem);
    }

当我不用它时,如果它还在桌面上显示,挡着我工具,这样肯定不行,直接把它关闭,再重新打开也不好(要用时再打开“我的电脑→E盘→project......程序”,这样的事情,我肯定不干),因此给它实现系统托盘,当我不用它时,把它最小化,让它躲在右下角的系统托盘位置。
private void initTi() {
        URL url 
= this.getClass().getClassLoader().getResource(
                
"com/oe/resource/invalid.gif");
        Image image 
= Toolkit.getDefaultToolkit().getImage(url);
        
this.setIconImage(image);
        PopupMenu popupTi 
= new PopupMenu();
        MenuItem showItem 
= new MenuItem("显示");
        showItem.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                setVisible(
true);
                MainWindow.
this.setExtendedState(JFrame.NORMAL);
            }
        });
        popupTi.add(showItem);

        MenuItem exitItem 
= new MenuItem("退出");
        exitItem.addActionListener(
new ActionListener() {
            
public void actionPerformed(ActionEvent e) {
                System.exit(
0);
            }
        });
        popupTi.add(exitItem);
        ti 
= new TrayIcon(image, "TrayIcon", popupTi);
    
        ti.addMouseListener(
new MouseListener() {
            
public void mouseClicked(MouseEvent e) {
                
if (e.getClickCount() == 2) {
                    
if (MainWindow.this.isVisible() == false) {
                        setVisible(
true);
                        MainWindow.
this.setExtendedState(JFrame.NORMAL);
                    }
                }

            }

            
public void mousePressed(MouseEvent e) {
            }

            
public void mouseReleased(MouseEvent e) {
            }

            
public void mouseEntered(MouseEvent e) {
            }

            
public void mouseExited(MouseEvent e) {

            }
        });
        SystemTray tray 
= SystemTray.getSystemTray();
        
try {
            tray.add(ti);
        } 
catch (AWTException e) {
            e.printStackTrace();
        }
    }

看着QQ顶部自动隐藏的效果,觉得这种效果很爽,顺便也给它实现了(自动隐藏这个功能未完善,有兴趣的,可以自己修改下)。
public class DragMouseListener implements MouseListener {
        
public void mouseReleased(java.awt.event.MouseEvent e) {
            isDragged 
= false;
            MainWindow.
this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
            Point p 
= MainWindow.this.getLocation();
            
if(0 <p.getY()  && p.getY() < 10){
                MainWindow.
this.setBounds((int)p.getX(), -2520030);
                MainWindow.
this.setAlwaysOnTop(true);
                hide 
= true;
            }
        }

        
public void mouseClicked(MouseEvent e) {
        }

        
public void mousePressed(java.awt.event.MouseEvent e) {
            tmp 
= new Point(e.getX(), e.getY());
            isDragged 
= true;
            MainWindow.
this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        }

        
public void mouseEntered(MouseEvent e) {
            
if(hide == true){
                MainWindow.
this.setBounds(6280150300);
                MainWindow.
this.setAlwaysOnTop(false);
                hide 
= false;
            }
        }

        
public void mouseExited(MouseEvent e) {
            Point p 
= MainWindow.this.getLocation();
            
if(-1 <p.getY()  && p.getY() < 10){
                String s 
= Double.toString(p.getX());
                
int in = Integer.valueOf("628");
                MainWindow.
this.setBounds(in, -2520030);
                MainWindow.
this.setAlwaysOnTop(true);
                hide 
= true;
            }
        }
    }

    
public class DragMouseMotionListener implements MouseMotionListener{
        
public void mouseDragged(MouseEvent e) {
            
if (isDragged) {
                loc 
= new Point(MainWindow.this.getLocation().x + e.getX()
                        
- tmp.x, MainWindow.this.getLocation().y + e.getY()
                        
- tmp.y);
                MainWindow.
this.setLocation(loc);
            }
        }

        
public void mouseMoved(MouseEvent e) {
        }
    }

主要的代码介绍完,想要更详细的,请看附件的完整代码。
附件:源代码



评论

# re: 一个快捷打开工具的实现  回复  更多评论   

2010-01-27 19:11 by 来如风
像这些功能,我一般用开始菜单来实现!!!!!!

# re: 一个快捷打开工具的实现  回复  更多评论   

2010-01-28 08:28 by rox
Pegtop这个软件非常不错,可以试试:
http://www.pegtop.net/
可惜的是,它只支持启动程序,不支持启动快捷方式,不过,完全够用了。

# re: 一个快捷打开工具的实现  回复  更多评论   

2010-01-28 14:33 by 伦理电影

很实用!

只有注册用户登录后才能发表评论。


网站导航: