基于xml创建Java用户界面(精简版)

来源:互联网 发布:淘宝上黑枸杞是真的吗 编辑:程序博客网 时间:2024/04/29 12:48

1.先写一个uimore.xml
<?xml version="1.0"?>
<ui>

 <panel height="40" width="400">
  <FlowLayout align="1">
  <label caption="Username:"/>
  <textfield defaultText="" cols="20"/>
  </FlowLayout>
 </panel>
 <panel height="40" width="400">
  <label caption="Password:"/>
  <textfield defaultText="" cols="20"/>
 </panel>
 <panel height="40" width="400">
  <FlowLayout align="2">
  <button label="login"/>
  <button label="clear"/>
  </FlowLayout>
 </panel>
 
</ui>

2.写一个增强型的类XMLComponent,完成读数据,加控件的功能。

package common;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.io.FileInputStream;

import javax.swing.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.SAXParser;


public class XMLComponent extends DefaultHandler
{
    private boolean containerActive = false;
   
    private ActionListener listener_ = null;
   
    private JPanel primaryContainer = new JPanel();
   
    private JPanel currentContainer = null;
   
    private SAXParser parser = new SAXParser();

    public XMLComponent()
    {
        super();
    }

    public XMLComponent( ActionListener listener )
    {
        listener_ = listener;
    }
    public JComponent build(String xmlDocument)
    {
        parser.setContentHandler(this);
        try
        {
            parser.parse(new InputSource(new FileInputStream(xmlDocument)));
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
        return primaryContainer;
    }
    public void startElement( String namespaceURL, String name, String qName, Attributes atts )
    {
        if( currentContainer==null )
        {
            currentContainer = primaryContainer;
        }
        if( name.equals("panel") )
        {
            currentContainer = new JPanel();
            Dimension size = new Dimension( Integer.parseInt(atts.getValue("width")),
                    Integer.parseInt(atts.getValue("height")) );
           
        }
       
        if( name.equals("button") )
        {
            JButton aButton = new JButton( atts.getValue("label") );
            aButton.addActionListener( listener_ );
            currentContainer.add( aButton );
        }
        if( name.equals("label") )
            currentContainer.add( new JLabel( atts.getValue("caption") ) );
        if( name.equals("textfield") )
        {
            currentContainer.add( new JTextField( atts.getValue("defaultTxt"),
                      Integer.parseInt(atts.getValue("cols"))) );
        }
        if( name.equals("FlowLayout") )
        {
            currentContainer.setLayout( new FlowLayout( Integer.parseInt(atts.getValue("align"))) );
        }
    }
    public void endElement( String uri, String localName, String qName )
    {
        if( localName.equals("panel") )
        {
            primaryContainer.add( currentContainer );
            currentContainer = primaryContainer;
        }
    }

}

3.写事件处理类ButtonClickAction。

package SwingXML;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ButtonClickAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        System.out.println( "You clicked " + e.getActionCommand() );
    }
}

4.写主程序,整合所有的类。

package SwingMore;
import javax.swing.*;

import SwingXML.ButtonClickAction;
import SwingXML.SimpleDemo;

import java.awt.*;
import java.awt.event.*;
import java.awt.Event;
import java.io.*;

import common.XMLComponent;

public class SwingDemoMore
{
    public SwingDemoMore(JFrame frame)
    {
       
        // TODO Auto-generated constructor stub
        frame.getContentPane().setLayout( new BorderLayout() );
        XMLComponent xmlComponent = new XMLComponent( new ButtonClickAction() );
       
        frame.getContentPane().add( "Center", xmlComponent.build("uimore.xml") );
        frame.setVisible( true );       
    }

    public static void main(String[] args)
    {
        JFrame frame = new JFrame("SwingDemoMore");
        frame.setSize(400, 400);

        frame.addWindowListener( new WindowAdapter()
                  {
                public void windowClosing( WindowEvent ev )
                {
                    System.exit(0);
                }
                  }
            );
        new SwingDemoMore( frame );
    }
}

原创粉丝点击