看了下,已經差不多三個星期沒有寫過文章了。今天主要介紹下自己之前寫的一個小工具,本來是想用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(), -25, 200, 30);
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(628, 0, 150, 300);
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, -25, 200, 30);
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) {
}
}
主要的代碼介紹完,想要更詳細的,請看附件的完整代碼。
附件:源代碼