本文共 5318 字,大约阅读时间需要 17 分钟。
1,swing工具启动时读取配置文件
-
-
-
-
- private void readConfig() throws IOException{
- configFile=new File(configFilePath);
- if(configFile.exists()){
- InputStream inStream=new FileInputStream(configFile);
- props.load(inStream);
- inStream.close();
- }
- String shareUrl=getPropValue(PROP_KEY_SHARE_URL);
- String picUrls=getPropValue(PROP_KEY_PIC_URLS);
-
-
-
- String qrComboboxStr=getPropValue(PROP_KEY_QR_CODE_COMBOBOX);
- if(!ValueWidget.isNullOrEmpty(shareUrl)){
- urlTextField.setText(shareUrl);
- textField_1.setText(shareUrl);
- }
- if(!ValueWidget.isNullOrEmpty(picUrls)){
- ComponentUtil.fillComboBox(picPathComboBox, picUrls);
-
- }
-
- setSwingInput(sharedPicTextField, PROP_KEY_PIC_URL);
-
- setSwingInput(redTF, PROP_KEY_COLOR_RED);
- setSwingInput(greenTF, PROP_KEY_COLOR_GREEN);
- setSwingInput(blueTF, PROP_KEY_COLOR_BLUE);
-
- setSwingInput(queryStringPanel.getQueryStringTField(), PROP_KEY_QUERY_STRING2JSON);
- setSwingInput(queryStringPanel.getToUrlEncodedTField_1(), PROP_KEY_HAVE_URL_DECODED);
- setSwingInput(queryStringPanel.getURLEncodedTF(), PROP_KEY_HAVE_URL_ENCODED);
-
-
-
-
- ComponentUtil.fillComboBox(qrCodePanel.getQrComboBox(), qrComboboxStr);
-
- setSwingInput(qrCodePanel.getInputQRTextArea(), PROP_KEY_QR_CODE_TEXT);
-
- setSwingInput(customEncryptPanel.getSourceTextField(), PROP_KEY_ENCRYPTED_SRCFILE);
- }
-
该方法是在什么时候调用呢?
在界面渲染的最后面
该方法的功能:
读取硬盘上的配置文件,初始化到界面表单元素
2,关闭时保存界面信息到配置文件
-
-
-
-
- public void saveConfig() throws IOException{
- if(!configFile.exists()){
- try {
- SystemHWUtil.createEmptyFile(configFile);
- } catch (IOException e) {
- e.printStackTrace();
- GUIUtil23.errorDialog(e);
- }
- }
- CMDUtil.show(configFilePath);
- if(ValueWidget.isNullOrEmpty(props)){
- props= new Properties();
- }
-
- String url2=urlTextField.getText();
- if(!ValueWidget.isNullOrEmpty(url2)){
- props.setProperty(PROP_KEY_SHARE_URL, url2);
- }
- String picUrl2=sharedPicTextField.getText();
- if(!ValueWidget.isNullOrEmpty(picUrl2)){
- props.setProperty(PROP_KEY_PIC_URL, picUrl2);
- }
-
- String redColor2=redTF.getText();
- if(!ValueWidget.isNullOrEmpty(redColor2)){
- props.setProperty(PROP_KEY_COLOR_RED, redColor2);
- }
- String greenColor2=greenTF.getText();
- if(!ValueWidget.isNullOrEmpty(greenColor2)){
- props.setProperty(PROP_KEY_COLOR_GREEN, greenColor2);
- }
- String blueColor2=blueTF.getText();
- if(!ValueWidget.isNullOrEmpty(blueColor2)){
- props.setProperty(PROP_KEY_COLOR_BLUE, blueColor2);
- }
-
- String queryString2=queryStringPanel.getQueryStringTField().getText();
- if(!ValueWidget.isNullOrEmpty(queryString2)){
- props.setProperty(PROP_KEY_QUERY_STRING2JSON, queryString2);
- }
-
- String toUrlEncoded2=queryStringPanel.getToUrlEncodedTField_1().getText();
- if(!ValueWidget.isNullOrEmpty(toUrlEncoded2)){
- props.setProperty(PROP_KEY_HAVE_URL_DECODED, toUrlEncoded2);
- }
-
- String URLEncoded2=queryStringPanel.getURLEncodedTF().getText();
- if(!ValueWidget.isNullOrEmpty(URLEncoded2)){
- props.setProperty(PROP_KEY_HAVE_URL_ENCODED, URLEncoded2);
- }
-
- setCombox(PROP_KEY_PIC_URLS, sharedPicTextField, picPathComboBox);
-
- setCombox(PROP_KEY_QR_CODE_COMBOBOX, qrCodePanel.getInputQRTextArea(), qrCodePanel.getQrComboBox());
-
- String inputQRTextAreaText=qrCodePanel.getInputQRTextArea().getText();
- if(!ValueWidget.isNullOrEmpty(inputQRTextAreaText)){
- props.setProperty(PROP_KEY_QR_CODE_TEXT, inputQRTextAreaText);
- }
-
- String sourceTextFieldText=customEncryptPanel.getSourceTextField().getText();
- if(!ValueWidget.isNullOrEmpty(sourceTextFieldText)){
- props.setProperty(PROP_KEY_ENCRYPTED_SRCFILE, sourceTextFieldText);
- }
- OutputStream out=new FileOutputStream(configFile);
- props.store(out, TimeHWUtil.formatDateTimeZH(null));
- out.close();
- CMDUtil.hide(configFilePath);
- }
该方法在什么时候调用呢?
在关闭swing界面时调用
3,相关变量的声明
- protected Properties props= new Properties();
-
-
-
- protected File configFile;
- public static final String configFilePath=System.getProperty("user.home")+File.separator+ ".share.properties";
- public static final String PROP_KEY_SHARE_URL="share_url";
-
-
-
- public static final String PROP_KEY_PIC_URLS="pic_urls";
-
-
-
- public static final String PROP_KEY_PIC_URL="pic_url";
-
-
-
- public static final String PROP_KEY_COLOR_RED="color_red";
-
-
-
- public static final String PROP_KEY_COLOR_GREEN ="color_green";
-
-
-
- public static final String PROP_KEY_COLOR_BLUE="color_blue";
-
-
-
- public static final String PROP_KEY_QR_CODE_TEXT="qr_code_text";
-
-
-
- public static final String PROP_KEY_ENCRYPTED_SRCFILE="encrypted_srcfile";
-
-
-
- public static final String PROP_KEY_QR_CODE_COMBOBOX="qr_code_combobox";
-
-
-
-
- public static final String PROP_KEY_QUERY_STRING2JSON="json_querystring";
-
-
-
- public static final String PROP_KEY_HAVE_URL_ENCODED="url_encoded";
-
-
-
- public static final String PROP_KEY_HAVE_URL_DECODED="url_decoded";
-
4,读取时把配置文件读取到Properties中
- InputStream inStream=new FileInputStream(configFile);
- props.load(inStream);
- inStream.close();
5,持久化时,把Properties持久化到文件中
- OutputStream out=new FileOutputStream(configFile);
- props.store(out, TimeHWUtil.formatDateTimeZH(null));
- out.close();
转载地址:http://xgeno.baihongyu.com/