现在的位置: 首页 > 综合 > 正文

Java制作的快速打开文件夹、程序的小工具

2018年01月30日 ⁄ 综合 ⁄ 共 2609字 ⁄ 字号 评论关闭

平时工作,经常要打开不同的文件夹找东西,打开多了,占满任务栏的位置,自己也觉得乱。而且每次打开文件夹时,都要经历“我的电脑→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.getTotalSpace());
            String usableSpace = calculateSpace(roots.getUsableSpace());
            String name = roots.toString().replace("\\", "");
            itemStructs = new ItemStruct( name + "盘 (" + usableSpace +") " + totalSpace,"explorer.exe " + roots.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);
    }

 源码下载

http://download.csdn.net/detail/ycb1689/4598561

抱歉!评论已关闭.