First, describe
Develop a MyFrame form application that inherits the JFrame class, the title in the form, the text on the button, and so on can be written in an XML configuration file, even if you want to change all of the properties later, you only need to change the corresponding attribute in the XML configuration file.
This case uses the properties class in Java to load an XML configuration file, read all the attributes in the file (Key-value), and use all the key values that are obtained for the JFrame form property.
Second, the source code
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{//gets each property in the properties object in the constructor method, and use attributes to form properties on the public MyFrame (properties properties) {String title = Properties.getproperty ("title"); String Buttonok = Properties.getproperty ("Buttonok"); String Buttoncancel = Properties.getproperty ("Buttoncancel");//Set the form title Settitle (title); SetLayout (new FlowLayout ()); /Create two buttons, the text above is the text in the XML configuration file 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;//Create a Properties object in the constructor method and call the function loadxmlfile () public that loads the XML file Useofproperties () {properties = new properties (); Loadxmlfile ();} Public Properties GEtproperties () {return properties;} Use the IO stream in Java to load the file, use the efficient BufferedReader stream to load the XML file, use the properties configured in the file to the properties on the form private void Loadxmlfile () {// Use BufferedReader to read the attributes in the XML file BufferedReader BufferedReader = null;try {//The profile is placed in the D-Packing directory, Of course it can also be placed in eclipse at the root of the project BufferedReader = new BufferedReader (New FileReader ("D:\\property.xml"));// Load file Properties.load (BufferedReader) using the Load () method of the Properties object;} catch (FileNotFoundException e) {System.out.println ("File not found!) "); E.printstacktrace ();} catch (IOException e) {System.out.println ("File load failed!") ");} finally{//closes the BufferedReader stream, releasing the resource if (BufferedReader! = null) {try {bufferedreader.close ();} catch (IOException e) { E.printstacktrace ();}}}} public class Setpropertybyxml {/** * @param args */public static void main (string[] args) {//Create Useofproperties object calls the constructor method and The final acquired Properties list is passed into the MyFrame () construction method Useofproperties useofproperties = new Useofproperties (); MyFrame myframe = new MyFrame (Useofproperties.getproperties ());}}
1, property.xml files placed in the D packing directory, here can be placed under the project root directory or other location, corresponding to modify the code in the BufferedReader loading file directory can be
Property.xml file contents (This is multiple key-value pairs):
title= to get window properties from an XML file
buttonok= OK
buttoncancel= Cancel
2. Operation result
Using the properties load XML file in Java to set the form properties of a Java Forms application