Archive for 11月 21st, 2012

  1. Hello, SWT(JRuby) World!

    Posted on 11月 21st, 2012 by cx20

    SWT(JRuby)

    SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
    IBM により AWT や Swing を置き換える目的で作成された。
    以下は JRuby による SWT の使用例となっている。

    ソースコード

    require 'java'
    require 'org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar'
     
    import 'org.eclipse.swt.SWT'
    import 'org.eclipse.swt.widgets.Display'
    import 'org.eclipse.swt.widgets.Shell'
    import 'org.eclipse.swt.widgets.Label'
    import 'org.eclipse.swt.layout.FillLayout'
     
    @display = Display.new
    @shell = Shell.new(display)
    @shell.setText("Hello, World")
     
    @layout = FillLayout.new(SWT::VERTICAL)
    @shell.setLayout(@layout)
     
    @label = Label.new(@shell,SWT::BORDER)
    @label.setText("Hello, SWT World!")
     
    @shell.setSize( 640, 480 )
    @shell.open
     
    while (!@shell.isDisposed) do
      if (!@display.readAndDispatch) then
        @display.sleep
      end
    end
     
    @display.dispose

    実行方法

    C:¥> jrubyw Hello.rb

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+