<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    First they ignore you
    then they ridicule you
    then they fight you
    then you win
        -- Mahatma Gandhi
    Chinese => English     英文 => 中文             
    隨筆-221  評(píng)論-1047  文章-0  trackbacks-0
    最近一直很忙,為了放松放松,自己就用Groovy寫了個(gè)計(jì)算器玩玩,順便也給還不太了解Groovy中SwingBuilder的朋友展示一下SwingBuilder的用法。

    注意:請(qǐng)使用最新版本的Groovy,否則顯示有問題


    運(yùn)行結(jié)果:



    /*
    ?*?Copyright?2007?the?original?author?or?authors.
    ?*?
    ?*?Licensed?under?the?Apache?License,?Version?2.0?(the?"License");
    ?*?you?may?not?use?this?file?except?in?compliance?with?the?License.
    ?*?You?may?obtain?a?copy?of?the?License?at
    ?*?
    ?*??????
    http://www.apache.org/licenses/LICENSE-2.0
    ?*?
    ?*?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 ?edu.ecust.swing

    import ?groovy.swing. *
    import ?javax.swing. *
    import ?java.awt. *

    /**
    ?*?Groovy?Calculator?for?studying?SwingBuilder
    ?*?
    ?*?
    @author ?Daniel?Sun(realbluesun@hotmail.com)
    ?*
    ?*?
    @since ?0.1
    ?
    */

    class ?GroovyCalculator?{
    ????def?swing?
    = ? new ?SwingBuilder()
    ????JFrame?frame
    ????def?toolkit?
    = ?Toolkit.getDefaultToolkit()
    ????def?screenSize?
    = ?toolkit.getScreenSize()
    ????
    ????
    ????def?WIDTH?
    = ? 320
    ????def?HEIGHT?
    = ? 200
    ????
    int ?X? = ?(screenSize.width? - ?WIDTH)? / ? 2
    ????
    int ?Y? = ?(screenSize.height? - ?HEIGHT)? / ? 2
    ????
    ????
    boolean ?flag? = ? false
    ????
    ????
    private ? void ?run()?{
    ????????UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
    ????????frame?
    = ?swing.frame(title:? ' Demo ' ,
    ????????????????????????????size:?[WIDTH,?HEIGHT],
    ????????????????????????????location:?[X,?Y],
    ????????????????????????????defaultCloseOperation:?javax.swing.WindowConstants.DISPOSE_ON_CLOSE)?{
    ????????????

    ????????????
    ????????????panel(layout:?
    new ?BorderLayout())?{
    ????????????????textField(id:?
    " expr " ,?constraints:?BorderLayout.NORTH)
    ????????????????
    ????????????????panel(constraints:?BorderLayout.CENTER)?{
    ????????????????????tableLayout?{
    ????????????????????????tr?{
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 7 " )?{
    ????????????????????????????????????action(getAction(
    " 7 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 8 " )?{
    ????????????????????????????????????action(getAction(
    " 8 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 9 " )?{
    ????????????????????????????????????action(getAction(
    " 9 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " / " )?{
    ????????????????????????????????????action(getAction(
    " / " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????}
    ????????????????????????tr?{
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 4 " )?{
    ????????????????????????????????????action(getAction(
    " 4 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 5 " )?{
    ????????????????????????????????????action(getAction(
    " 5 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 6 " )?{
    ????????????????????????????????????action(getAction(
    " 6 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " * " )?{
    ????????????????????????????????????action(getAction(
    " * " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????}
    ????????????????????????tr?{
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 1 " )?{
    ????????????????????????????????????action(getAction(
    " 1 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 2 " )?{
    ????????????????????????????????????action(getAction(
    " 2 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 3 " )?{
    ????????????????????????????????????action(getAction(
    " 3 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " - " )?{
    ????????????????????????????????????action(getAction(
    " - " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????}
    ????????????????????????tr?{
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " 0 " )?{
    ????????????????????????????????????action(getAction(
    " 0 " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " = " )?{
    ????????????????????????????????????action(name:
    " = " ,?closure:? this . & eval)
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " C " )?{
    ????????????????????????????????????action(name:
    " C " ,?closure:? this . & clear)
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????????td?{
    ????????????????????????????????button(text:
    " + " )?{
    ????????????????????????????????????action(getAction(
    " + " ))
    ????????????????????????????????}
    ????????????????????????????}
    ????????????????????????}
    ????????????????????}
    ????????????????}
    ????????????}
    ????????}
    ????????
    ????????swing.expr.setEditable(
    false )
    ????????swing.expr.setHorizontalAlignment(JTextField.RIGHT)
    ????????swing.expr.setBackground(Color.WHITE)
    ????????
    ????????frame.pack()
    ????????frame.setResizable(
    false )
    ????????frame.setVisible(
    true )
    ????}
    ????
    ????
    private ? void ?append(EventObject?evt? = ? null )?{
    ????????
    if ?(flag)?{
    ????????????swing.expr.text?
    = ? ""
    ????????????flag?
    = ? false
    ????????}
    ????????
    ????????String?name?
    = ?evt.source.text
    ????????swing.expr.text?
    <<= ?name
    ????????swing.expr.text?
    = ?swing.expr.text.replaceAll( " // " ,? " / " )
    ????}
    ????
    ????
    private ? void ?clear(EventObject?evt? = ? null )?{
    ????????swing.expr.text?
    = ? ""
    ????}
    ????
    ????
    private ? void ?eval(EventObject?evt? = ? null )?{
    ????????String?expr?
    = ?swing.expr.text
    ????????
    ????????def?b
    = ? new ?Binding()
    ????????def?conf
    = new ?org.codehaus.groovy.control.CompilerConfiguration()
    ????????conf.setSourceEncoding(
    ' unicode ' )
    ????????def?groovyShell?
    = ? new ?GroovyShell(b,conf)
    ????????
    ????????
    try ?{
    ????????????swing.expr.text?
    = ?groovyShell.evaluate(expr)
    ????????}?
    catch ?(Throwable?t)?{
    ????????????swing.expr.text?
    = ? " invalid?expression! "
    ????????????flag?
    = ? true
    ????????}
    ????????
    ????}
    ????
    ????def?getAction(name)?{
    ????????
    return ?swing.action(name:name,?closure:? this . & append)
    ????}
    ????
    ????
    static ? void ?main(args)?{
    ????????
    new ?GroovyCalculator().run()
    ????}

    }


    附:朝花夕拾——Groovy & Grails
    posted on 2007-08-05 20:52 山風(fēng)小子 閱讀(6154) 評(píng)論(17)  編輯  收藏 所屬分類: Groovy & Grails
    主站蜘蛛池模板: 亚洲国产综合人成综合网站00| 午夜免费不卡毛片完整版| 青柠影视在线观看免费| 丁香花在线观看免费观看图片| 免费无码又爽又黄又刺激网站| 色天使亚洲综合一区二区| 国产成人亚洲综合在线| 全部在线播放免费毛片| a一级毛片免费高清在线| 丰满人妻一区二区三区免费视频| 99久久免费国产特黄| 女同免费毛片在线播放| 亚洲精品免费在线观看| 日本人的色道免费网站| 国产成人yy免费视频| 成**人免费一级毛片| 免费a在线观看播放| 国产偷窥女洗浴在线观看亚洲| 九月丁香婷婷亚洲综合色| 精品亚洲aⅴ在线观看| 激情亚洲一区国产精品| 亚洲国产一区二区三区在线观看| 免费国产黄网站在线看| 中国内地毛片免费高清| 一级毛片在线免费看| 免费国产成人高清在线观看网站| 日韩精品视频免费在线观看| 亚洲国产精品一区二区九九 | 在线视频网址免费播放| 国产一级婬片A视频免费观看| 另类免费视频一区二区在线观看| 亚洲日本在线免费观看| 午夜寂寞在线一级观看免费| 亚洲精品tv久久久久久久久久| 国产日韩亚洲大尺度高清| 亚洲妇女水蜜桃av网网站| 亚洲狠狠色丁香婷婷综合| 中文字幕不卡免费高清视频| 999在线视频精品免费播放观看 | 美女被cao网站免费看在线看| 四虎精品视频在线永久免费观看|