1.先写一个uimore.xml
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 );
}
}