构造器
1、可以生成XML/HTML/SWING等嵌套树状数据结构
2、在语法级别上实现GOF的Builder设计模式
NodeBuilder 创建嵌套的节点树 DomBuilder 创建W3C DOM树 SwingBuilder 创建Swing界面 AntBuilder 生成ANT脚本 MarkupBuilder 创建XML或HTML标记
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"
}
}
}
}
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,
size:[400,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,size:[400,
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',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',
size:[400,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框架 。
总结以上是内存溢出为你收集整理的Groovy笔记(10)_Swing界面全部内容,希望文章能够帮你解决Groovy笔记(10)_Swing界面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)