Sunday, December 17, 2006

Creating GUI with IronPython, Groovy and JRuby

I was curious what are the differences between the following tools when it comes to creating a GUI desktop application:
  • IronPython (with Windows Forms)
  • Groovy (with Swing)
  • JRuby (with Swing)

I prepared a simple example for each of them. The application shows a form with one button. This button when clicked changes its own text.

IronPython:

tested with 1.1a1 (1.1) on .NET 2.0.50727.42

  • Native windows
  • Possible to pass parameters in the constructor (form = Form(Text="test"))
  • Nice += for event handlers
  • As such a simple example it should work with Mono on Linux, however not sure about more complicated ones.

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms
import Application, Button, Form
from System.Drawing import Size


form = Form(
Text="Hello",
Size=Size(200, 200)
)

button = Button(Text="OK!")

def onClick(event, handler):
button.Text = "Pressed!"

button.Click += onClick
form.Controls.Add(button)

Application.Run(form)


Groovy:
tested with 1.0-RC-01
(Thanks to Marcin Domański for his help on this example)
  • Cross-platform
  • Elegant event handlers api (actionPerformed:click)
  • Possible to pass parameters in the constructor (as in IronPython)


import javax.swing.JFrame
import javax.swing.JButton

def frame = new JFrame(
title:"Hello",
size:[200, 200],
defaultCloseOperation:JFrame.EXIT_ON_CLOSE
)

def click = {
event -> event.source.text = "Pressed!"
}

def button = new JButton(
text:"OK!",
actionPerformed:click
)

frame.add(button)
frame.show()



JRuby
tested with 0.9.2
  • Cross-platform
  • JRuby allows you to call Java code using the more Ruby-like underscore_method_naming and to refer to JavaBean properties as attributes.
  • Event handlers using classes is not nice


require 'java'
include_class "javax.swing.JFrame"
include_class "javax.swing.JButton"
include_class "java.awt.event.ActionListener"

frame = JFrame.new("Hello")
frame.set_size(200,200)
frame.defaultCloseOperation = JFrame::EXIT_ON_CLOSE

button = JButton.new("OK!")

class ClickListener < ActionListener
def actionPerformed(event)
event.source.text = "Pressed!"
end
end

button.add_action_listener(ClickListener.new)

frame.add(button)
frame.show


Update: There is a simpler Groovy version with SwingBuilder.

2 comments:

Aleksandr said...

this post is on behalf of Martin C. Martin his suggested improvemnt over the Groovy version offered would be:
Click here to see original email

import groovy.swing.SwingBuilder

swing = new SwingBuilder()
frame = swing.frame(
title:"Hello",
size:[200, 200],
defaultCloseOperation:JFrame.EXIT_ON_CLOSE) {
button(
text:"OK!",
actionPerformed:{it.source.text = "Pressed!"})
}

frame.show()

Anonymous said...

情趣用品,情趣用品,情趣用品,情趣用品,情趣,情趣,情趣,情趣,按摩棒,震動按摩棒,微調按摩棒,情趣按摩棒,逼真按摩棒,G點,跳蛋,跳蛋,跳蛋,性感內衣,飛機杯,充氣娃娃,情趣娃娃,角色扮演,性感睡衣,SM,潤滑液,威而柔,香水,精油,芳香精油,自慰套,自慰,性感吊帶襪,吊帶襪,情趣用品加盟AIO交友愛情館,情人歡愉用品,美女視訊,情色交友,視訊交友,辣妹視訊,美女交友,嘟嘟成人網,成人網站,A片,A片下載,免費A片,免費A片下載愛情公寓,情色,舊情人,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,一葉情貼圖片區,情色小說,色情,色情遊戲,情色視訊,情色電影,aio交友愛情館,色情a片,一夜情,辣妹視訊,視訊聊天室,免費視訊聊天,免費視訊,視訊,視訊美女,美女視訊,視訊交友,視訊聊天,免費視訊聊天室,情人視訊網,影音視訊聊天室,視訊交友90739,成人影片,成人交友,美女交友,微風成人,嘟嘟成人網,成人貼圖,成人電影,A片,豆豆聊天室,聊天室,UT聊天室,尋夢園聊天室,男同志聊天室,UT男同志聊天室,聊天室尋夢園,080聊天室,080苗栗人聊天室,6K聊天室,女同志聊天室,小高聊天室,上班族聊天室,080中部人聊天室,同志聊天室,聊天室交友,中部人聊天室,成人聊天室,一夜情聊天室,情色聊天室,寄情築園小遊戲情境坊歡愉用品,情趣用品,成人網站,情人節禮物,情人節,AIO交友愛情館,情色,情色貼圖,情色文學,情色交友,色情聊天室,色情小說,七夕情人節,色情,情色電影,色情網站,辣妹視訊,視訊聊天室,情色視訊,免費視訊聊天,美女視訊,視訊美女,美女交友,美女,情色交友,成人交友,自拍,本土自拍,情人視訊網,視訊交友90739,生日禮物,情色論壇,正妹牆,免費A片下載,AV女優,成人影片,色情A片,成人論壇,情趣,免費成人影片,成人電影,成人影城,愛情公寓,成人影片,保險套,舊情人,微風成人,成人,成人遊戲,成人光碟,色情遊戲,跳蛋,按摩棒,一夜情,男同志聊天室,肛交,口交,性交,援交,免費視訊交友,視訊交友,一葉情貼圖片區,性愛,視訊,視訊聊天,A片,A片下載,免費A片,嘟嘟成人網,寄情築園小遊戲,女同志聊天室,免費視訊聊天室,一夜情聊天室,聊天室