有,我做过,比如一个动态的表格,通过解析XML数据,读取标志,如果不满足则不显示这列。
要达到这样的动态性,就需要前期设计好,这是代价

解决方案 »

  1.   

    谢谢,诸位仁兄,我列出几个难题
    关键是
    1 依赖关系问题 比如一个textField 它的值域 依赖于其他某一个 控件,
     具体讲有: 
        逻辑关系 A 存在,B才显示, A checkbox 被选中 ,B才可以有效,等等。需要用到关系运算
              (NOT,AND,OR)    数学运算关系     A(设为JTextField) A的值 属于[0, B /2],缺省为 B/3 (假设B为某一控件TextField的值)
    2,界面关系问题,比如原来 panel 上面有3个控件,如果要加入 一个控件,界面如何摆放比较合适
    (我想直接写在xml里面用GridBagLayout)
    还要添加默认的ActionListener。
    这个不同于Eclipse. Eclipse 其实加载的是插件方式。也就是说还必须写不少代码。(我希望之需要
    XML,即便是很复杂)尤其是 依赖关系,还没有什么好的想法
      

  2.   

    用XSL解析XML不行吗?象CSDN这样的
      

  3.   

    有现成的开源代码:
    swixml
    还有很多其它的,专门负责从xml文件动态生成swing页面。
      

  4.   

    我这里还有一段我自己实现的,通过Digester来自动从xml页面生成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);           
        }
        
        
    }
      

  5.   

    回复人: jinxfei(周华健、言兴朋、茅威涛) ( ) 信誉:100  2005-03-10 00:03:00  得分: 0  
     试试,先谢谢了