也许你对先前的Groovy程序有点烦了,全是控制台程序,现在我们
来体验一下它的界面程序吧.
package com
import groovy.swing.SwingBuilder
import java.awt.BorderLayout
//声明一个SwingBuilder
def swing = new SwingBuilder()
//计数器
count = 0
//声明一个label
def textlabel
//声明一个frame
def frame =
swing.frame(title:'计数器', size:[300,300])
{
borderLayout()
textlabel = label(text:"您还未点击,Come on!",
constraints: BorderLayout.NORTH)
button(text:'Click Me',
actionPerformed: {count++; textlabel.text = "您已经点击了${count}次."; println "clicked"},
constraints:BorderLayout.SOUTH)
}
//控件自适应窗口大小
frame.pack()
//显示
frame.show()
再来一个,自己运行一下就知道,用groovy写Swing程序是多么简单了.
package com
import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.*
def swing = new SwingBuilder()
//声明一个共用的Panel
def sharedPanel =
{
swing.panel() {
label("共享Panel")
}
}
count = 0
def textlabel
def frame =
swing.frame(title:'共享Panel救命', size:[300,300], defaultCloseOperation:JFrame.EXIT_ON_CLOSE, pack:true,
show:true) {
boxLayout(axis:BoxLayout.Y_AXIS)
textlabel = label(text:"点击了${count}次.",
constraints: BorderLayout.NORTH)
button(text:'点击我',
actionPerformed: {count++; textlabel.text = "点击了${count}次"; println "clicked"},
constraints:BorderLayout.SOUTH)
widget(sharedPanel())
widget(sharedPanel())
}