Posted on 2010-03-04 15:08
penngo 閱讀(3139)
評論(0) 編輯 收藏 所屬分類:
Java
使用javascript和CSS,可以把web的頁面設計搞得很漂亮;但如果你想把CS的桌面程序搞得漂亮點,這個還真不容易。雖然像swing
可以更換外觀http://www.open-open.com/61.htm讓界面漂亮點,但這些也只是讓swing控件漂亮點。對于程序的整體布局等,相對
javascript和CSS做出來的頁面效果,實在顯得有點差。
本文是先通過tree.html設計好程序界面,再調用jdic中的瀏覽器控件org.jdesktop.jdic.browser.WebBrowser顯示tree.html
程序的功能主要是實現上一篇介紹的http://m.tkk7.com/pengo/archive/2010/01/26/310849.html
tree.html代碼
通過<a>標記傳遞參數,當點擊C盤時,URL將會變為....../tree.html#explorer.exe c:
<span style="top: 213px; height: 211px; visibility: visible;" class="link"><div class="title">我的電腦</div>
<div style="visibility: hidden;" class="text">
<a href="#explorer.exe c:" class="alink">C盤</a> <br/>
<a href="#explorer.exe d:" class="alink">D盤</a> <br/>
<a href="#explorer.exe e:" class="alink">E盤</a> <br/>
<a href="#explorer.exe ::{208D2C60-3AEA-1069-A2D7-08002B30309D}" class="alink" >網上鄰居</a> <br/>
<a href="#explorer.exe ::{645FF040-5081-101B-9F08-00AA002F954E}" class="alink">回收站</a> <br/>
<a href="#notepad" class="alink">記事本</a> <br/>
</div>
</span>
<span style="top: 231px; height: 211px; visibility: visible;" class="link"><div class="title">關機管理</div>
<div style="visibility: hidden;" class="text">
<a href="#Shutdown.exe -s -t 00" class="alink">關機</a> <br/>
<a href="#Shutdown.exe -r -f -t 00" class="alink">重啟</a> <br/>
</div>
</span>
MenuTest.java代碼:
在webBrowser的WebBrowserListener事件中實現downloadCompleted方法,先獲取當前頁的URL,通過分析URL后邊的參數,獲得“#”符號后操作命令。
public void downloadCompleted(WebBrowserEvent event) {
URL currentUrl = webBrowser.getURL();
if(currentUrl != null){
String cmd = currentUrl.toString();
if(cmd.indexOf("#") != -1){
cmd = cmd.substring(cmd.indexOf("#") + 1);
if(cmd.equals("exit")){
System.exit(0);
}
try {
Runtime.getRuntime().exec(cmd);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
效果圖:
原代碼:
HtmlFrame.src.rar
運行程序:
HtmlFrame.jar.rar