如何在java

时间:2018-03-30 13:22:25

标签: java xml

<?xml version="1.0" encoding="UTF-8"?>
<properties>
 <config>
   <seperator>
     <entry key ="fileSeprator">|</entry>
        </seperator>
        <columncount>
            <entry key ="colcount">4</entry>
        </columncount>
        <columnName>
            <entry key ="0">ClientID</entry>
            <entry key ="1">ID</entry>
            <entry key ="2">FirstName</entry>
            <entry key ="3">LastName</entry>
        </columnName>
        <exception>
            <entry key ="0">ClientID_Blank_Null</entry>
            <entry key ="1">ID_Blank_Null_incorrect</entry>
            <entry key ="2">FName_IS_Null</entry>
            <entry key ="3">LName_IS_Null</entry>
        </exception>
        </config>
 </properties>

//我有上面的xml属性文件。如何阅读java代码。这里Key = 0是文件中的位置。文件没有列名,但位置是固定的。我只想知道如何在Java代码中读取它。如果value为空,则从属性文件中输入异常。只是帮我在java中读取这个文件。

1 个答案:

答案 0 :(得分:0)

以下是代码,当您单击其他按钮时,它将显示不同的复选框。

import java.awt.BorderLayout;
import java.awt.Checkbox;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DynamicCheckBox extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel north;
    private JPanel center;

    public DynamicCheckBox() {
        north = new JPanel();
        JButton first = new JButton("First");
        JButton second = new JButton("Second");
        north.add(first);
        north.add(second);
        center = new JPanel();
        center.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 20));
        first.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                center.removeAll();
                Checkbox[] box = createCheckBox(new String[]{"1", "2"});
                for (int i = 0; i < box.length; i++) {
                    center.add(box[i]);
                }
                center.repaint();
                DynamicCheckBox.this.revalidate();
            }

        });
        second.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                center.removeAll();
                Checkbox[] box = createCheckBox(new String[]{"3", "4"});
                for (int i = 0; i < box.length; i++) {
                    center.add(box[i]);
                }
                center.repaint();
                DynamicCheckBox.this.revalidate();
            }

        });
        this.add(north, BorderLayout.NORTH);
        this.add(center, BorderLayout.CENTER);
        this.setSize(new Dimension(300, 300));
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new DynamicCheckBox();
    }

    public Checkbox[] createCheckBox(String[] values) {
         Checkbox[] box = new Checkbox[values.length];
         for (int i = 0; i < values.length; i++) {
             box[i] = new Checkbox(values[i]);
         }
         return box;
    }
}

enter image description here enter image description here