前言:java是能夠開發桌面應用程序的,如swing,可是由於界面並不美觀且老是爲控件大小座標操心。不搞事不舒服司機就要改造車。怎麼改進swing巨醜的界面,而且能夠更簡單,用更少的時間。html+js這對搭檔無疑是一個很好的選擇,可是html+js只能在瀏覽器裏使用,那麼就要祭出一個法寶swt的browser控件,這是什麼東東呢,簡單來講就是內嵌瀏覽器。這樣一來其實開發出來的東西就是一款定製版的瀏覽器。html
首先準備一份數據java
設計一個界面node
第三步將這個頁面放進browser裏讓他看起來像一個桌面應用程序web
package min; import java.io.*; import java.net.HttpURLConnection; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import org.eclipse.swt.SWT; import org.eclipse.swt.browser.Browser; import org.eclipse.swt.browser.BrowserFunction; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * This class implements a web browser */ public class client { private Button button; private Combo url; private Browser browser; private static HttpURLConnection conn = null; /** * Runs the application * * the initial location to display */ public void run() { Display display = new Display(); Shell shell = new Shell(display); shell.setText("bgammn v1.0"); createContents(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); } /** * Creates the main window's contents * * @param shell * the main window * the initial location */ public void createContents(Shell shell) { shell.setLayout(new FormLayout()); /* Composite controls = new Composite(shell, SWT.NONE);*/ FormData data = new FormData(); browser = new Browser(shell, SWT.BORDER); data.top = new FormAttachment(0,0); data.left = new FormAttachment(0, 0); data.right = new FormAttachment(100, 0); data.bottom = new FormAttachment(100, 0); browser.setLayoutData(data); /* controls.setLayout(new GridLayout(7, false));*/ /* url = new Combo(controls, SWT.ARROW_DOWN);*/ /* url.setLayoutData(new GridData(496, SWT.DEFAULT)); url.setFocus();*/ browser.setUrl(client.class.getResource("")+"index.html"); } public static void main(String[] args) { new client().run(); } }
運行,一個界面優美的桌面應用就新鮮出爐了shell
but還有一個重要的地方如何交互?好比界面顯示的數據如何從java傳遞給js,用戶作出的動做js如何傳遞給java處理?瀏覽器
新建一個properties數據結構
java讀取到這個值如何傳遞給界面顯示出來app
新建讀取properties工具類eclipse
class toolProperties { private Properties pro; private String path; public toolProperties(String path){ this.path=path; Properties pro=new Properties(); try { InputStream is = new BufferedInputStream(new FileInputStream(path)); BufferedReader in = new BufferedReader(new InputStreamReader(is,"UTF-8")); pro.load(in); this.pro=pro; } catch (IOException e) { e.printStackTrace(); } } public String getNode(String nodeName){ return pro.getProperty(nodeName); } public int saveNode(String nodeName,String nodeVale){ pro.setProperty(nodeName,nodeVale); try { FileOutputStream fre=new FileOutputStream(path); pro.store(fre,"success"); fre.close(); return 1; } catch (IOException e) { return 0; } } public Map<String,String> getAllNodes(){ Map<String,String> nodes=new LinkedHashMap<String, String>(); for(String key:pro.stringPropertyNames()){ nodes.put(key,pro.getProperty(key)); } return nodes; } }
讀取ide
new BrowserFunction(browser, "load"){ @Override public Object function(Object[] arguments) { System.out.println(1); String table=new client().new toolProperties(tablePath).getNode("tableName"); browser.execute("var option=new Array()"); for(String opt:table.split("~")){ browser.execute("option.push('"+opt+"')"); } return super.function(arguments); } };
new BrowserFunction怎麼用呢,第一個參數是browser控件,第二個是暴露給js調用的函數名,browser.execute是執行一段js代碼。這樣在html界面調用load()函數後就會在js裏生成一個option參數,裏面存放的就是讀取的peoperties
能夠
下一節將設計所需的數據結構