使用java的browser+html+js编写简易版excel桌面应用程序(一)

前言:java是可以开发桌面应用程序的,如swing,但是因为界面并不美观且总是为控件大小坐标操心。不搞事不舒服司机就要改造车。怎么改进swing巨丑的界面,并且可以更简单,用更少的时间。html+js这对搭档无疑是一个很好的选择,但是html+js只能在浏览器里使用,那么就要祭出一个法宝swt的browser控件,这是什么东东呢,简单来说就是内嵌浏览器。这样一来其实开发出来的东西就是一款定制版的浏览器。

首先准备一份数据

设计一个界面

7c117879c5b76d0cf6af7a198f9abcfbfca.jpg

第三步将这个页面放进browser里让他看起来像一个桌面应用程序

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();

    }
}


6c2f4b3c3f106bd1853d6e36b0af589f5b3.jpg

运行,一个界面优美的桌面应用就新鲜出炉了

but还有一个重要的地方如何交互?比如界面显示的数据如何从java传递给js,用户做出的动作js如何传递给java处理?

新建一个properties

bb78107d3e7f2319208e66a9eb26ee0f03e.jpg

java读取到这个值如何传递给界面显示出来

新建读取properties工具类

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;
        }
    }

读取

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

bfc2759ef269ec3e449e0c897c94aae4967.jpg

可以

下一节将设计所需的数据结构

转载于:https://my.oschina.net/u/3188070/blog/2874787

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值