最近一直很忙为了放松放松自己就用Groovy写了个计算器玩玩顺便也给还不太了解Groovy中SwingBuilder的朋友展示一下SwingBuilder的用法 注意请使用最新版本的Groovy否则显示有问题 运行结果 /** Copyright the original author or authors** Licensed under the Apache License Version (the License);* you may not use this file except in compliance with the License* You may obtain a copy of the License at**** Unless required by applicable law or agreed to in writing software* distributed under the License is distributed on an AS IS BASIS* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND either express or implied* See the License for the specific language governing permissions and* limitations under the License*/package eduecustswingimport groovyswing *import javaxswing *import javaawt */*** Groovy Calculator for studying SwingBuilder** @author Daniel Sun(rea)** @since */class GroovyCalculator {def swing = new SwingBuilder()JFrame framedef toolkit = ToolkitgetDefaultToolkit()def screenSize = toolkitgetScreenSize()def WIDTH = def HEIGHT = int X = (screenSizewidth WIDTH) / int Y = (screenSizeheight HEIGHT) / boolean flag = falseprivate void run() {UIManagersetLookAndFeel(UIManagergetSystemLookAndFeelClassName())frame = swingframe(title: Demo size: [WIDTH HEIGHT]location: [X Y]defaultCloseOperation: javaxswingWindowConstantsDISPOSE_ON_CLOSE) {panel(layout: new BorderLayout()) {textField(id: expr constraints: BorderLayoutNORTH)panel(constraints: BorderLayoutCENTER) {tableLayout {tr {td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: / ) {action(getAction( / ))}}}tr {td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: * ) {action(getAction( * ))}}}tr {td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}td {button(text: ) {action(getAction( ))}}}tr {td {button(text: ) {action(getAction( ))}}td {button(text: = ) {action(name: = closure: this & eval)}}td {button(text: C ) {action(name: C closure: this & clear)}}td {button(text: + ) {action(getAction( + ))}}}}}}}swingexprsetEditable( false )swingexprsetHorizontalAlignment(JTextFieldRIGHT)swingexprsetBackground(ColorWHITE)framepack()framesetResizable( false )framesetVisible( true )}private void append(EventObject evt = null ) {if (flag) {swingexprtext = flag = false}String name = evtsourcetextswingexprtext <<= nameswingexprtext = swingexprtextreplaceAll( // / )}private void clear(EventObject evt = null ) {swingexprtext = }private void eval(EventObject evt = null ) {String expr = swingexprtextdef b = new Binding()def conf = new ntrolCompilerConfiguration()confsetSourceEncoding( unicode )def groovyShell = new GroovyShell(bconf)try {swingexprtext = groovyShellevaluate(expr)} catch (Throwable t) {swingexprtext = invalid expression! flag = true}}def getAction(name) {return swingaction(name:name closure: this & append)}static void main(args) {new GroovyCalculator()run()}} |