Breezy简介 -- 快速开发Swing程序! 博客分类: BlogJava Swing.netPHPBBSHibernate
Breezy简介 -- 快速开发Swing程序!
Breezy:
sf 项目主页: http://sourceforge.net/projects/breezy/
下载: http://sourceforge.net/project/showfiles.php?group_id=158540&package_id=177653&release_id=412125
用户讨论区: http://www.hexiao.cn/bbs/thread.php?fid=9
大家都知道开发Swing程序有三大难点:
布局设置,事件处理,和数据绑定. 使用netbean中的新的布局管理器已经解决了第一个问题, 那么后面两个呢?
Breezy 就是用来简化后面两个问题而推出的.
下面我们先来看看一个简单的例子:
package test;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import net.sf.breezy.event.EventInstaller;
import net.sf.breezy.event.annotations.ActionPerformed;
import net.sf.breezy.event.annotations.DefAction;
@SuppressWarnings("serial")
public class TestBreezy extends JFrame {
private JButton button = new JButton("Test Breezy!");
public TestBreezy() {
this.setLayout(new BorderLayout());
this.add(button,BorderLayout.CENTER);
// 用来处理所有的Breezy的Annotations
EventInstaller.processEventAnnotation(this);
this.setSize(400, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EventInstaller.setAction(button, "action");
}
// button的事件响应方法
@ActionPerformed("button")
public void myButtonAction() {
System.out.println("God, this is terrific!");//设置另外一个事件
}
// 使用id注册另外一个事件响应方法
@DefAction(id="action", name="Button")
void action(ActionEvent e) {
System.out.println(e + ":" + e);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new TestBreezy();
frame.setVisible(true);
}
}
怎么样, 看起来不错吧.
现在Breezy几乎支持所有的Swing EventListeners. 详细情况请查看类net.sf.breezy.event.annotations.*
当然了, 作为一个优秀的框架,还要有可扩展性,在Breezy中开发者还可以自定义自己的事件响应Annotation 来实现自己的功能. 详细情况请查看API.
数据绑定问题主要 就是 JTable 和JTree的问题:
使用Breezy来处理该问题,有点类似于Hibernate中的xml映射数据库属性字段差不多. 可以很方便的映射.
请看下面的例子: 和Breezy文档:
package test;
import java.util.Date;
public class Name {
private String name = "Starwhisperer";
private Date time = new Date();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
User.java
package test;
import java.util.Date;
import net.sf.breezy.datatable.TableField;
public class User {
@TableField(head = "名称", editable = false)
private String name;
@TableField(head = "年龄", editable = false, type = "number")
private int age;
@TableField(head = "阵营", type = "boolean", editable = false)
private boolean light;
@TableField(head = "时间", type = "time", pattern = "yyyy/MM/dd")
private Date date = new Date();
@TableField(head = "名称", property = "name", type="string")
private Name nameBean = new Name();
public Name getNameBean() {
return nameBean;
}
public void setNameBean(Name nameBean) {
this.nameBean = nameBean;
}
public boolean isLight() {
return light;
}
public void setLight(boolean light) {
this.light = light;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
DataTableTest .java
package test;
import java.awt.BorderLayout;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import net.sf.breezy.datatable.DataTable;
import net.sf.breezy.datatable.DataTableScrollPane;
public class DataTableTest {
private JFrame mainFrm = new JFrame();
protected void setUp() {
mainFrm.setSize(100, 600);
mainFrm.setLocationRelativeTo(null);
mainFrm.setLayout(new BorderLayout());
mainFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
DataTableTest test = new DataTableTest();
test.setUp();
test.testTable();
}
public void testTable() {
java.util.List list = new LinkedList();
for(int i = 0; i < 120; i ++) {
User u = new User();
u.setAge(33);
u.setName("Cain " + i);
list.add(u);
}
DataTableScrollPane<User> pane = DataTable.getEntityTablePane(list, User.class);
mainFrm.add(pane, BorderLayout.CENTER);
mainFrm.setVisible(true);
}
}
映射文件:datatable-mappings.xml
<?xml version="1.0"?>
<class-mapping>
<class name="test.User">
<property name="age" head="年龄" />
<property name="light" head="光芒" type="boolean" editable="true"/>
<property name="nameBean" property="time" head="姓名" pattern="yyyy/mm/dd" type="time"/>
<property name="date" head="Date"/>
<property name="name" head="Name"/>
</class>
</class-mapping>
也是很简单的吧. 关于JTree的应用也是很简单的哦. 详细情况请查看用户文档:
更多信息请关注 用户交流论坛: http://www.hexiao.cn/bbs/thread.php?fid=9