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())
}