构造器
1、可以生成XML/HTML/SWING等嵌套树状数据结构
2、在语法级别上实现GOF的Builder设计模式
MarkupBuilder Demo
1、Demo
import groovy.xml.*
//创建生成器
def page = new MarkupBuilder()
//使用伪方法html/head等,参数是闭包
page.html{
head{title 'Hello'}
body{
ul{
for(count in 1..3){
li "clat $count"
}
}
}
}
<html> <head> <title>Hello</title> </head> <body> <ul> <li>clat1</li> <li>clat2</lt> <li>clat3</lt> </ul> </body> </html>
SwingBuilder构造GUI
1、Demo
import groovy.swing.SwingBuilder
import javax.swing.*
//创建生成器
def sb = new SwingBuilder()
//使用伪方法产生界面
def frame = sb.frame(title:'ToyStore' , location:[100,100],
size:[400,300],defaultCloseOperation:
WindowConstants.EXIT_ON_CLOSE){
label(text:"Hello world") // 修改此行重构 ,在frame中建立了一个标签
}
frame.pach()
frame.setVisible(true)
//Swing 的伪方法改为大写,再在前面加上J,就是相应组件了
1、更好的组织代码
def sb = new SwingBuilder()
def mainPanel = {
sb.panel(layout:new GridLayout(2,2,5,5)){
label(text:'Last name:' , horizontalAlignment : JLabel.RIGHT) //右对齐
textField(text:'', columns:10) //输入框
label(text:'First name:' , horizontalAlignment: JLabel.RIGHT)
textField(text:'' , columns:10)
}
}
def frame = sb.frame(title:'ToysStore' ,location :[100,100],
size:[400,300],defaultCloseOperation:WindowConstants.EXIT_ON_CLOSE){
mainPanel() //调用闭包
}
frame.pack()
frame.setVisible(true)
布局与事件处理
1、
def buttonPanel = {
sb.panel(constraints : BorderLayout.SOUTH){
button(text:'OK',actionPerformed:{ println 'OK pressed'})
button(text:'Cancel',actionPerformed:{println 'Cancel pressed'})
}
}
def mainPanel ={
sb.panel(layout : new BorderLayout()){
label(text:'Is this OK?' , horizontalAlignment : JLabel.CENTER,
constraints : BorderLayout.CENTER)
buttonPanel()
}
}
def frame = sb.frame(title:'ToysStore',location:[100,100] ,size:[400,300],
defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE){mainPanel()}
frame.pack(); frame.setVisible(true)
BoxLayout
1、建立4个横向排列的按钮
import groovy.swing.SwingBuidler
import javax.swing.*
import java.awt.*
class FixedButton extends JButton{ // 使按钮大小固定
Dimension getMinimumSize(){return BUTTONSIZE}
Dimension getMaximunSize(){return BUTTONSIZE}
Dimension getPreferredSize(){return BUTTONSIZE}
private static final BUTTONSIZE = new Dimension(80,30)
}
def sb = new SwingBuilder()
def mainPanel = {
sb.panel(layout:new BorderLayout()){
hbox(constraints: BorderLayout.WEST){ // hbox可以换成vbox,变成纵向排列
def buttons = ['One','Two','Three','Four']
buttons.each{but -> sb.widget(new FixedButton(text: but))}
}
}
}
}
def frame = sb.frame(title: 'Example 10' ,location:[100,100],size:[400,300]
,defaultCloseOperation: WindowConstants.EXIT_ON_CLOSE){mainPanel()}
frame.pack() ; frame.setVisible(true)
JList/JScrollPane
1、selection
import groovy.swint.SwingBuilder
import javax.swing.*
import java.awt.*
class ToyListModel extends DefaultListModel{
ToyListModel(list){ super()
list.each{item -> this.addElement(item)} // 列表添加到模型中
}}
def sb = new SwingBuilder()
def listPanel={
sb.panel(constraints: BorderLayout.CENTER){
scrollPane(){ // 加入滚动条
def toyList = ['toy1','toy2','toy3','toy4']
toysList = list(model : new ToyListModel(toylist), //定义列表
selectionMode: ListSelectionModel.SINGLE_SELECTION,
selectedIndex: 0 ,visbleRowCount:4)
}
}
}
2、
def mainPanel = {
sb.panel(layout: new BorderLayout()){
listPanel()
}
}
def fram = sb.frame(title : 'ToyStore' ,location :[100,100],
size:[400,300],defaultCloseOperation:WindowConstants.EXIT_ONT_CLOSE){
mainPanel()
}
JTable/JScrollPane
1、表格
import ....同上
import java.swing.table.*
def sb = new SwingBuilder()
def tablePanel = {
sb.panel(constrains: BorderLayout.CENTER){
scrollPane(){
table(selectionMode: ListSelectionModel.SINGLE_SELECTION){
def toysList = [[toyName:'toy1' , unitPrice: '100'],
[toyName :'toy2', unitPrice:'200']]
tableModel(list:toysList){
closureColumn(header: 'Toy Name' ,read :{row ->return row.toyName}) //指定列
closureColumn(header: 'Unit Price',read:{row -> return row.unitPrice}))
}
}
}
}
}
2、然后可以像上面一样,添加Jframe框架 。