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

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 );

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值