posts - 59, comments - 244, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          一個快捷打開工具的實現(xiàn)

          Posted on 2010-01-26 13:01 penngo 閱讀(1891) 評論(3)  編輯  收藏 所屬分類: Java
          看了下,已經(jīng)差不多三個星期沒有寫過文章了。今天主要介紹下自己之前寫的一個小工具,本來是想用QT做的,但做了兩個小時,也沒出什么成果(......學藝未精),改用swing試下,發(fā)現(xiàn)一個小時就能做好大半功能,所以最終用swing實現(xiàn)了它。不過這個工具純屬無聊之作,純屬做出來玩玩的。

          平時工作,經(jīng)常要打開不同的文件夾找東西,打開多了,占滿任務欄的位置,自己也覺得亂。而且每次打開文件夾時,都要經(jīng)歷“我的電腦→E盤→project......”,這個讓我覺得太麻煩。因此自己做個小工具,實現(xiàn)快捷打開文件夾、程序的功能。

          整個程序的主角是Runtime.getRuntime().exec()的使用,不懂Runtime.getRuntime().exec(),請自己Google、Baidu了解下,這樣不作解釋。

          啥也不說,先上兩個圖:


          先介紹點擊列表時執(zhí)行的代碼,主要實現(xiàn)功能:鼠標經(jīng)過時,選項背景色會改變;單擊選項時,執(zhí)行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記錄的數(shù)據(jù)和命令(這里只是隨便列出幾個命令給大家參考,需要其它打開命令的,可以自行添加)
          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("網(wǎng)上鄰居","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......程序”,這樣的事情,我肯定不干),因此給它實現(xiàn)系統(tǒng)托盤,當我不用它時,把它最小化,讓它躲在右下角的系統(tǒng)托盤位置。
          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頂部自動隱藏的效果,覺得這種效果很爽,順便也給它實現(xiàn)了(自動隱藏這個功能未完善,有興趣的,可以自己修改下)。
          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: 一個快捷打開工具的實現(xiàn)  回復  更多評論   

          2010-01-27 19:11 by 來如風
          像這些功能,我一般用開始菜單來實現(xiàn)!!!!!!

          # re: 一個快捷打開工具的實現(xiàn)  回復  更多評論   

          2010-01-28 08:28 by rox
          Pegtop這個軟件非常不錯,可以試試:
          http://www.pegtop.net/
          可惜的是,它只支持啟動程序,不支持啟動快捷方式,不過,完全夠用了。

          # re: 一個快捷打開工具的實現(xiàn)  回復  更多評論   

          2010-01-28 14:33 by 倫理電影

          很實用!
          主站蜘蛛池模板: 左权县| 遂平县| 灵寿县| 韩城市| 赤水市| 土默特右旗| 师宗县| 巴马| 平泉县| 易门县| 鹤峰县| 宜黄县| 扬州市| 永登县| 夏邑县| 吐鲁番市| 邢台市| 临沂市| 特克斯县| 阳信县| 高台县| 盐山县| 增城市| 雷波县| 任丘市| 伊金霍洛旗| 新津县| 客服| 汪清县| 渝中区| 江达县| 河北省| 阿拉善盟| 松江区| 永顺县| 卢氏县| 新野县| 德惠市| 吴桥县| 博白县| 长沙县|