熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Groovy高效編程—利用SwingBuilder構造GUI

2013-11-23 18:47:27  來源: Java核心技術 

  最近一直很忙為了放松放松自己就用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 eduecustswing   import groovyswing *   import javaxswing *   import javaawt *   /**   * Groovy Calculator for studying SwingBuilder   *   * @author Daniel Sun(rea)   *   * @since   */   class GroovyCalculator {   def swing = new SwingBuilder()   JFrame frame   def toolkit = ToolkitgetDefaultToolkit()   def screenSize = toolkitgetScreenSize()   def WIDTH =   def HEIGHT =   int X = (screenSizewidth WIDTH) /   int Y = (screenSizeheight HEIGHT) /   boolean flag = false   private 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 = evtsourcetext   swingexprtext <<= name   swingexprtext = swingexprtextreplaceAll( // / )   }   private void clear(EventObject evt = null ) {   swingexprtext =   }   private void eval(EventObject evt = null ) {   String expr = swingexprtext   def 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()   }   }



From:http://tw.wingwit.com/Article/program/Java/hx/201311/25741.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.