平常想用Java写一些小工具来辅助开发,但是使用Swing是一件非常麻烦的事情,Swing的代码写起来又臭又长。但是又苦于没有现成的框架可以利用,所以自己写了一个工具,实用用XML的配置来实现Swing视图,代码非常小,只有几十K。
需要XML的配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<commonswing>
<frame id="main" ico="ico.jpg">
<title>H2管理器 - 设置</title>
<width>400</width>
<height>200</height>
<defaultCloseOperation>exit</defaultCloseOperation>
<center>true</center>
<content>
<label bounds="10,10,100,20" bgcolor="00FF00">文件路径:</label>
<text bounds="130,10,100,20" id="path"></text>
<button target="path" bounds="230,10,80,20" actionClass="com.tntxia.commonswing.action.ChooseFileAction">选择</button>
<label bounds="10,40,100,20">用户名:</label>
<text bounds="130,40,100,20" id="username"></text>
<label bounds="10,70,100,20">密码:</label>
<text bounds="130,70,100,20" id="password"></text>
<button bounds="160,100,60,20">登陆</button>
</content>
</frame>
</commonswing>
这个XML非常的简单,frame标签表示一个窗体,frame内有title,width,height这几个我们非常用的属性 。
defaultCloseOperation
是点击了窗体关闭按钮后的动作,默认是close,如果设置为exit的话,关闭后,程序结束。
center: 如果为true的话,窗体居中显示。
content:用来放组件的地方,比如按钮,文本,输入框等,
可以使用actionClass给按钮增加动作,所有的动作都必须继承Action类。
非常的简便,易懂。
使用框架的过程有什么疑问,可以在博客留言,我会尽快回复你的。