天天看點

swing Ctrl+S 儲存配置

swing Ctrl+S 儲存配置
swing Ctrl+S 儲存配置

我現在不細說功能,而是說明如何儲存使用痕迹,比如上圖,我在"old path"輸入了"com\common\jn\img\path.png",我想下次打開時"old path"自動儲存上一次的記錄.

如何實作呢?

(1)可以把文本框的内容儲存到properties配置檔案中;

(2)關閉視窗或者按下ctrl+s 時觸發儲存事件

(3)下次啟動視窗時,從配置檔案中讀取使用痕迹,并恢複到界面上.

具體實作如下

配置檔案為

swing Ctrl+S 儲存配置

public static final string configfilepath="c:\\.path_config.properties";  

(a)擷取界面上文本框的内容,并設定到properties中,然後寫入磁盤

swing Ctrl+S 儲存配置

/*** 

     * 儲存到配置檔案中 

     * @throws ioexception  

     */  

    protected void saveconfig() throws ioexception{  

        file configfile=new file(configfilepath);  

        if(!configfile.exists()){  

            try {  

                systemhwutil.createemptyfile(configfile);  

            } catch (ioexception e) {  

                e.printstacktrace();  

                guiutil23.errordialog(e);  

            }  

        }  

        cmdutil.show(configfilepath);//因為隐藏檔案是隻讀的  

//        fileutils.writetofile(configfilepath, content);  

        if(valuewidget.isnullorempty(props)){  

            props= new properties();  

        string old_path=oldpathtf.gettext();  

        if(!valuewidget.isnullorempty(old_path)){  

            props.setproperty(prop_key_old_path, old_path);  

        string folder1=this.comparefolderpanel.getfolderonetextfield().gettext();  

        if(!valuewidget.isnullorempty(folder1)){  

            props.setproperty(prop_key_compared_folderone, folder1);  

        string folder2=this.comparefolderpanel.getfolder2textfield().gettext();  

        if(!valuewidget.isnullorempty(folder2)){  

            props.setproperty(prop_key_compared_folder2, folder2);  

        string sourcefile=this.checksamepanel.getsourcefiletf().gettext();  

        if(!valuewidget.isnullorempty(sourcefile)){  

            props.setproperty(prop_key_source_file, sourcefile);  

        string targetfile=this.checksamepanel.gettargetfiletf().gettext();  

        if(!valuewidget.isnullorempty(targetfile)){  

            props.setproperty(prop_key_target_file, targetfile);  

        /*** 

         * 增量包中,複制class時是否彈出确認提示框 

         */  

        boolean issure=createfolderbypackage.getsurecheckbox().isselected();  

        props.setproperty(prop_key_copy_java_sure, string.valueof(issure));  

        boolean iscopypath=copycheckbox.isselected();  

        props.setproperty(prop_key_is_copy_path, string.valueof(iscopypath));  

        setcombox(prop_key_root_paths, this.createfolderbypackage.getrootfoldertextfield(),this.createfolderbypackage.getrootpathcombobox());  

        setcombox(prop_key_java_file_paths, this.createfolderbypackage.getclasstextfield(),this.createfolderbypackage.getjavapathcombobox());  

        outputstream out=new fileoutputstream(configfile);  

        props.store(out, timehwutil.formatdatetimezh(null));  

        out.close();//及時關閉資源  

        cmdutil.hide(configfilepath);//隐藏檔案:attrib ".mqtt_client.properties" +h  

        system.out.println("save complete.");  

    }  

   /*** 

     * 以;;分隔 

     * @param prop_key 

     * @param tf 

    private void setcombox(string prop_key,jtextfield tf,jcombobox<string>combobox){  

        string rootpath=tf.gettext();  

        if(valuewidget.isnullorempty(rootpath)){  

            return;  

        string roots=props.getproperty(prop_key);  

        if(valuewidget.isnullorempty(roots)){  

            roots=rootpath;  

        }else{  

            string roots_old[]=roots.split(sharedpicdivision);  

            if(!systemhwutil.iscontains(roots_old, rootpath)){  

                roots=roots+sharedpicdivision+rootpath;  

                combobox.additem(rootpath);  

            string urls_old2[]=roots.split(sharedpicdivision);  

            string urls_new[]=systemhwutil.unique(urls_old2);  

            roots=stringutils.join(urls_new, sharedpicdivision);  

        props.setproperty(prop_key, roots);  

 注意:cmdutil.show 是去掉配置檔案的隐藏屬性,因為隐藏檔案不可寫.

(b)視窗關閉時觸發儲存事件

swing Ctrl+S 儲存配置

this.addwindowlistener(new windowadapter() {  

            @override  

            public void windowclosing(windowevent e) {  

                system.out.println("closing....");  

                try {  

                    saveconfig();  

                } catch (ioexception e1) {  

                    e1.printstacktrace();  

                    guiutil23.errordialog(e1);  

                }  

                super.windowclosing(e);  

            public void windowclosed(windowevent e) {  

                /*system.out.println("closed"); 

                try { 

                    saveconfig(); 

                } catch (ioexception e1) { 

                    e1.printstacktrace(); 

                }*/  

                super.windowclosed(e);  

        });  

 注意:視窗關閉時隻會執行windowclosing(),而不會執行windowclosed()

swing Ctrl+S 儲存配置

(c)增加全局快捷鍵

swing Ctrl+S 儲存配置

         * 增加全局快捷鍵.<br> 

         * ctrl+s,導緻"比例"文本框聚焦 

        protected void setglobalshortcuts() {  

            // add global shortcuts  

            toolkit toolkit = toolkit.getdefaulttoolkit();  

            // 注冊應用程式全局鍵盤事件, 所有的鍵盤事件都會被此事件監聽器處理.  

            toolkit.addawteventlistener(new java.awt.event.awteventlistener() {  

                public void eventdispatched(awtevent event) {  

                    if (event.getclass() == keyevent.class) {  

                        keyevent ke = ((keyevent) event);  

                        // 處理按鍵事件 ctrl+s  

                        if (ke.getkeycode() == keyevent.vk_s  

                                && ke.iscontroldown()&&!ke.isaltdown()  

                                && ke.getid() == keyevent.key_pressed) {  

                            try {  

                                saveconfig();  

                            } catch (ioexception e) {  

                                e.printstacktrace();  

                                guiutil23.errordialog(e);  

                            }  

                        }   

                    }  

            }, java.awt.awtevent.key_event_mask);  

 注意:為什麼要加上條件 ke.getid() == keyevent.key_pressed,根本原因是為了防止觸發兩次

(d)啟動程式時,先讀取配置檔案

swing Ctrl+S 儲存配置

     * 讀取配置檔案 

     * @throws ioexception 

    private void readconfig() throws ioexception{  

        if(configfile.exists()){  

            inputstream instream=new fileinputstream(configfile);  

            props.load(instream);  

            instream.close();//及時關閉資源  

        string old_path=getpropvalue(prop_key_old_path);  

        //"比較檔案夾"  

        string compared_folderone=getpropvalue(prop_key_compared_folderone);  

        string compared_folder2=getpropvalue(prop_key_compared_folder2);  

        //增量包  

        string root_path=getpropvalue(prop_key_root_paths);  

        string java_path=getpropvalue(prop_key_java_file_paths);  

        //比較二進制檔案  

        string sourcefile=getpropvalue(prop_key_source_file);  

        string targetfile=getpropvalue(prop_key_target_file);  

        string issurestr=getpropvalue(prop_key_copy_java_sure);  

        string iscopypath=getpropvalue(prop_key_is_copy_path);  

            oldpathtf.settext(old_path);  

        if(!valuewidget.isnullorempty(root_path)){  

            string roots[]=root_path.split(sharedpicdivision);  

            componentutil.fillcombobox(createfolderbypackage.getrootpathcombobox(), roots);  

        if(!valuewidget.isnullorempty(java_path)){  

            string java_paths[]=java_path.split(sharedpicdivision);  

            componentutil.fillcombobox(createfolderbypackage.getjavapathcombobox(), java_paths);  

        if(!valuewidget.isnullorempty(compared_folderone)){  

            comparefolderpanel.getfolderonetextfield().settext(compared_folderone);;  

        if(!valuewidget.isnullorempty(compared_folder2)){  

            comparefolderpanel.getfolder2textfield().settext(compared_folder2);;  

            checksamepanel.getsourcefiletf().settext(sourcefile);  

            checksamepanel.gettargetfiletf().settext(targetfile);  

        if(!valuewidget.isnullorempty(issurestr)){  

            boolean  issure2=boolean.parseboolean(issurestr);  

            createfolderbypackage.getsurecheckbox().setselected(issure2);  

        if(!valuewidget.isnullorempty(iscopypath)){  

            boolean  iscopypath2=boolean.parseboolean(iscopypath);  

            copycheckbox.setselected(iscopypath2);  

上述代碼中的一些變量聲明:

swing Ctrl+S 儲存配置

    private properties props=new properties();  

    /*** 

     * path tools 

    public static final string prop_key_old_path="old_path";  

     * 比較檔案夾 

    public static final string prop_key_compared_folderone="compared_folderone";  

    public static final string prop_key_compared_folder2="compared_folder2";  

     * 增量包 

    public static final string prop_key_root_paths="root_paths";  

    /** 

     * 增量包的"class檔案" 

    public static final string prop_key_java_file_paths="java_paths";  

     * 比較二進制檔案 

    public static final string prop_key_source_file="sourcefile";  

    public static final string prop_key_target_file="targetfile";  

     * 增量包中複制class時是否彈出确認提示框 

    public static final string prop_key_copy_java_sure="issure";  

     * 是否複制路徑 

    public static final string prop_key_is_copy_path="iscopypath";  

    public static final string sharedpicdivision=";;";  

 注意:

(1)window中隐藏檔案是隻讀的,不可寫

swing Ctrl+S 儲存配置

(2)對于下拉框,多個路徑之間使用兩個分号進行分割

配置檔案範例:

swing Ctrl+S 儲存配置