【Java】调用XML文件生成GUI界面

解析XML文件,读出其中关于GUI界面的相关配置,生成GUI界面。
XML文件如下(ui.xml):

<?xml version="1.0"?>
<frame name="XMLUI" center="true" visible="true"
 resizable="false" alwaysOnTop="true" main="true" 
 left="0" top="0" width="300" height="200">
   <panel visible="true" left="20" top="10" width="80" height="20"/>
   <button enable="true" name="Wintel" left="20" top="100"
    width="80" height="20" actionCommand="btn_is"/>
   <button enable="false" name="Zhao" left="100" top="100"
    width="80" height="20" actionCommand="bin_great"/>
</frame>

XMLGUI类,解析XML文件,生成GUI界面:

package xml;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Color;

import java.io.File;
import java.io.IOException;

import java.util.List;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class XMLUI{
    
    private String actionCommand = null;
    
    public static void main(String args[]) {
        new XMLUI("ui.xml");
    }

    public XMLUI(String fileName) {
        this.createFrame(fileName);
    }
    
    public void createFrame(String fileName) {
        Document docJDOM = new Document();
        SAXBuilder bSAX = new SAXBuilder(false);
        try {
            docJDOM = bSAX.build(new File(fileName));
        }catch (JDOMException e) {
            e.printStackTrace();
            return;
        }catch (IOException ioe) {
            ioe.printStackTrace();
        }
        
        Element root = docJDOM.getRootElement();
        JFrame jFrame = createJFrame(Boolean.parseBoolean
        (root.getAttribute("center").getValue().trim()),
                Boolean.parseBoolean(root.getAttribute("visible").getValue().trim()),
                Boolean.parseBoolean(root.getAttribute("resizable").getValue().trim()),
                Boolean.parseBoolean(root.getAttribute("alwaysOnTop").getValue().trim()),
                Boolean.parseBoolean(root.getAttribute("main").getValue().trim()),
                Integer.parseInt(root.getAttribute("left").getValue().trim()),
                Integer.parseInt(root.getAttribute("top").getValue().trim()),
                Integer.parseInt(root.getAttribute("width").getValue().trim()),
                Integer.parseInt(root.getAttribute("height").getValue().trim()),
                root.getAttribute("name").getValue());
        
        List panelList = root.getChildren("panel");
        for (int i=0; i < panelList.size() ; i++) {
            Element element = (Element)panelList.get(i);
            JPanel jPanel = createJPanel(Boolean.parseBoolean
            (element.getAttribute("visible").getValue().trim()),
                    Integer.parseInt(element.getAttribute("left").getValue().trim()),
                    Integer.parseInt(element.getAttribute("top").getValue().trim()),
                    Integer.parseInt(element.getAttribute("width").getValue().trim()),
                    Integer.parseInt(element.getAttribute("height").getValue().trim()));
            jFrame.getContentPane().add(jPanel, null);
        }
        
        List buttonList = root.getChildren("button");
        for (int i=0; i < buttonList.size() ; i++) {
            Element element = (Element)buttonList.get(i);
            JButton jButton = createJButton(Boolean.parseBoolean
            (element.getAttribute("enable").getValue().trim()),
                    element.getAttribute("name").getValue(),
                    Integer.parseInt(element.getAttribute("left").getValue().trim()),
                    Integer.parseInt(element.getAttribute("top").getValue().trim()),
                    Integer.parseInt(element.getAttribute("width").getValue().trim()),
                    Integer.parseInt(element.getAttribute("height").getValue().trim()),
                    element.getAttribute("actionCommand").getValue());
            jFrame.getContentPane().add(jButton, null);
        }
        
        jFrame.repaint();
    }
    
    public JFrame createJFrame(boolean center, boolean visible, boolean resizable, boolean alwaysOnTop, boolean main, int left, int top, int width, int height, String name) {
        
        JFrame jFrame = new JFrame(name);
        JPanel jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jFrame.setContentPane(jContentPane);
        
        if(center) {
            Toolkit theKit = jFrame.getToolkit();
            Dimension wndSize = theKit.getScreenSize();
            jFrame.setBounds(wndSize.width/2-width/2,wndSize.height/2-height/2,width,height);//窗口在屏幕中心显示
        }
        else {
            jFrame.setBounds(left,top,width,height);
        }
        jFrame.setVisible(visible);
        jFrame.setResizable(resizable);
        jFrame.setAlwaysOnTop(alwaysOnTop);
        
        if(main) {
            jFrame.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent ev) {
                    System.exit(0);
                }
            });
        }
        
        return jFrame;
    }
    public JPanel createJPanel(boolean visible, int left, int top, int width, int height){
        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setVisible(visible);
        jPanel.setBounds(left,top,width,height);
        jPanel.setBackground(Color.BLUE);
        return jPanel;
    }
    public JButton createJButton(boolean enable, String name, int left, int top, int width,
     int height, String actionCommand) {
     
        JButton jButton = new JButton(name);
        jButton.setBounds(left,top,width,height);
        jButton.setEnabled(enable);
        setActionCommand(actionCommand);
        jButton.addActionListener(new java.awt.event.ActionListener(){ 
        public void actionPerformed(java.awt.event.ActionEvent e) {    
        action(getActionCommand());
        }
                                });
        return jButton;
    }
    
    public void action(String actionCommand) {
        System.out.println(actionCommand);
    }
    
    public void setActionCommand(String actionCommand) {
        this.actionCommand = actionCommand;
    }
    public String getActionCommand() {
        return this.actionCommand;
    }
}

测试程序,其中覆盖了父类的action方法,可以重写监听器。

package xml;

public class TestXMLUI extends XMLUI {

    public TestXMLUI(){
        super("ui.xml");
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new TestXMLUI();
    }
    
    public void action(String actionCommand) {
        System.out.println("actionCommand: " + actionCommand);
    }

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值