java中使用Properties載入XML檔案設定java表單應用程式的表單內容

來源:互聯網
上載者:User

標籤:properties   載入xml檔案   設定jframe表單內容   bufferedreader   java   

一、描述

開發一個MyFrame表單應用程式,該表單繼承JFrame類,表單中的標題、按鈕上的文字等資訊都可以寫在一個xml設定檔中,即使以後想更改所有的屬性,只需要更改xml設定檔中的相應屬性即可。

本案例使用java中的Properties類來載入一個xml設定檔,並讀取檔案中的所有屬性(key-value),並將取得的所有鍵值對應用於JFrame表單內容中。

二、原始碼

package tong.day4_27.systemUse;import java.awt.FlowLayout;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.util.Properties;import javax.swing.JButton;import javax.swing.JFrame;class MyFrame extends JFrame{//在構造方法中擷取Properties對象中的每一個屬性,並把屬性使用到表單內容上public MyFrame(Properties properties) {String title = properties.getProperty("title");String buttonOK = properties.getProperty("buttonOK");String buttonCancel = properties.getProperty("buttonCancel");//設定表單標題setTitle(title);setLayout(new FlowLayout());//建立兩個按鈕,上面的文字就是xml設定檔中的文字JButton btnOK = new JButton(buttonOK);JButton btnCancel = new JButton(buttonCancel);add(btnOK);add(btnCancel);setBounds(200, 100, 300, 100);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}}class UseOfProperties{private Properties properties ;//在構造方法中建立 Properties對象,並調用載入xml檔案的函數loadXMLFile()public UseOfProperties(){properties = new Properties();loadXMLFile();}public Properties getProperties() {return properties;}//使用java中的IO流負載檔案,這裡使用高效的BufferedReader流載入xml檔案,將檔案中配置的屬性使用到表單中的屬性上private void loadXMLFile() {//使用BufferedReader讀取xml檔案中的屬性BufferedReader bufferedReader = null;try {//該設定檔放在d盤根目錄下,當然也可以放在eclipse中該項目的根目錄下bufferedReader = new BufferedReader(new FileReader("d:\\property.xml"));//使用Properties對象的load()方法負載檔案properties.load(bufferedReader);} catch (FileNotFoundException e) {System.out.println("檔案未找到!");e.printStackTrace();}catch (IOException e) {System.out.println("檔案載入失敗!");}finally{//關閉BufferedReader流,釋放資源if (bufferedReader != null) {try {bufferedReader.close();} catch (IOException e) {e.printStackTrace();}}}}}public class SetPropertyByXML {/** * @param args */public static void main(String[] args) {//建立UseOfProperties對象則會調用構造方法,並把最終擷取的Properties屬性列表傳入MyFrame()構造方法中UseOfProperties useOfProperties = new UseOfProperties();MyFrame myFrame = new MyFrame(useOfProperties.getProperties());}}

1、property.xml檔案放在了d盤根目錄下,這裡可以根據需要放在項目根目錄下或者其它位置,對應要修改代碼中BufferedReader中負載檔案的目錄即可
property.xml檔案內容(這是多個鍵值對):
title=通過xml檔案擷取視窗屬性
buttonOK=確定
buttonCancel=取消


2、運行結果


java中使用Properties載入XML檔案設定java表單應用程式的表單內容

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.