动态生成界面

自动从xml页面生成java swing界面的功能,不过比较简单:
xslt可能生成web页面,也就是html或者其它脚本型的界面比较方便。

配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<ConfigDialogTitle>BMC Patrol 采集配置</ConfigDialogTitle>
<ConfigGroup>
<title>基本配置</title>
<configItem name="采集点名称"></configItem>
<configItem name="采集点描述"></configItem>
<configItem name="采集点ID"></configItem>
</ConfigGroup>
<ConfigGroup>
<title>Patrol Agent连接配置</title>
<configItem name="Patrol Agent IP"></configItem>    
<configItem name="Patrol Agent 端口"></configItem>
<configItem name="Patrol Agent 用户"></configItem>
<configItem name="Patrol Agent 密码"></configItem>
</ConfigGroup>
</config>

代码:

Digester digest=new Digester();
        digest.addObjectCreate("config","com.ubi.config.demo.ConfigDialog");
        digest.addCallMethod("config/ConfigDialogTitle","setTitle", 0);
        digest.addObjectCreate("config/ConfigGroup", "com.ubi.config.demo.ConfigTab");
        digest.addCallMethod("config/ConfigGroup/title","setName",0);
        digest.addCallMethod( "config/ConfigGroup/configItem", "addConfigItem", 1 );
        digest.addCallParam( "config/ConfigGroup/configItem", 0,"name" );
        digest.addSetNext( "config/ConfigGroup", "addConfigTab" );

        try {
            BufferedReader r=new BufferedReader(new StringReader(cfgTemplate));
            Object obj=digest.parse(r);
            ConfigDialog dlg=(ConfigDialog)obj;
            dlg.setReaderTypeID(this.readerTypeIntID);
            dlg.setSize(new Dimension(600,400));
            dlg.show();
            System.out.println("parsed object is "+obj);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

相关类:

/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;

import com.ubi.config.demo.listener.CheckBoxTreeCellRenderer;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigDialog extends JDialog implements ActionListener{
    JTabbedPane configTabs;
    int readerTypeID;
    public void setReaderTypeID(int id){
        readerTypeID=id;
    }
    JPanel contentP=new JPanel();
    public ConfigDialog(){
        super();
        contentP.setLayout(new BorderLayout());
        this.setContentPane(contentP);
        this.setSize(new Dimension(200,150));
        configTabs=new JTabbedPane();
        contentP.add(configTabs, BorderLayout.CENTER);
        JPanel btnPane=new JPanel();
        contentP.add(btnPane, BorderLayout.SOUTH);
        btnPane.setLayout(new FlowLayout());
        JButton btnAdd=new JButton("确定");
        JButton btnCacel=new JButton("取消");
        btnPane.add(btnAdd);
        btnPane.add(btnCacel);
        btnAdd.addActionListener(this);
        btnCacel.addActionListener(this);
    }

    public void addConfigTab(ConfigTab newTab){
        configTabs.addTab(newTab.getName(), newTab);
    }
    /* (non-Javadoc)
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getActionCommand().equals("取消")){
            this.hide();
        }
        if (arg0.getActionCommand().equals("确定")){
            buildEventFilterPage();
        }
    }
    void buildEventFilterPage(){
        APMJTree events=new APMJTree("事件过滤");
        events.setCellRenderer(new CheckBoxTreeCellRenderer());
        ResultSet rs=null;
        try {
            rs=ConfigMain.db.executeQuery("select * from coll_pnt_inst_event where coll_pnt_inst_uid="+this.readerTypeID);
            while (rs.next()){
                events.buildPath(rs.getString("coll_pnt_inst_event_id"));
            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                rs.getStatement().close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
        JScrollPane scroll=new JScrollPane(events);
        events.expandRow(0);
        JDialog dlg=new JDialog();
        dlg.setTitle("采集事件过滤");
        dlg.getContentPane().setLayout(new BorderLayout());
        dlg.getContentPane().add(scroll,BorderLayout.CENTER);
        dlg.setSize(400,300);
        dlg.setModal(true);
        dlg.show();
    }
}
/*
 * Created on 2005-3-2
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.ubi.config.demo;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author ibm
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ConfigTab extends JPanel{
    public ConfigTab(){
        super();
        this.setLayout(null);
    }
    int  nextPointX=20;
    int positionY=20;
    int space=50;

    public void addConfigItem(String itemName){
        JLabel lb=new JLabel(itemName);
        lb.setBounds(positionY,nextPointX,150,20);        
        this.add(lb);
        JTextField txt=new JTextField();
        txt.setBounds(positionY+155,nextPointX,150,20);
        nextPointX+=space;
        this.add(txt);           
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QT是一款功能强大的跨平台C++应用程序开发框架,可以用于开发各种类型的图形界面应用程序。而根据配置文件动态生成界面是QT框架中的一项重要特性。 在QT中,我们可以使用Qt Designer来设计界面,也可以通过代码方式来构建界面。Qt Designer是一款可视化工具,可以通过拖拽组件、设置属性等操作来设计界面。而通过代码方式构建界面则是利用Qt的类库和API来动态创建和布局界面。 而根据配置文件动态生成界面是指我们可以通过读取配置文件的参数来决定界面的组件、布局和样式等。这样的设计方式具有很大的灵活性和可扩展性,可以根据不同的需求来生成不同的界面。 在实现过程中,我们可以使用QT提供的文件读写类库来读取配置文件的内容,然后根据读取的参数信息来动态创建和配置界面的组件。我们可以使用QString类来处理字符串、QFile类来读取文件、QTextStream类来读取文件内容等。 例如,我们可以将配置文件中的组件类型、位置和样式等信息以一定的格式写入配置文件,然后在程序运行时读取该配置文件,根据配置文件中的参数来动态创建并设置界面的组件。同时,我们也可以根据不同的配置文件来生成不同的界面,这样就实现了根据配置文件动态生成界面的功能。 总的来说,QT框架通过使用Qt Designer设计界面和代码方式构建界面的方式,以及借助文件读写类库来读取配置文件的内容,可以实现根据配置文件动态生成界面的功能,提供了更加灵活和可定制的界面开发方式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值