Archive for the ‘SWT’ Category

  1. Hello, SWT(BeanShell) World!

    Posted on 12月 9th, 2012 by cx20

    SWT(BeanShell)

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

    ソースコード

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.layout.*;
     
    display = new Display();
    shell = new Shell(display);
    shell.setText("Hello, World");
     
    layout = new FillLayout(SWT.VERTICAL);
    shell.setLayout(layout);
     
    label = new Label(shell,SWT.BORDER);
    label.setText("Hello, SWT World!");
     
    shell.setSize( 640, 480 );
    shell.open();
     
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
     
    display.dispose();

    実行方法

    C:¥> SET CLASSPATH=bsh-2.0b4.jar;org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> java bsh.Interpreter hello.bsh

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, SWT(Clojure) World!

    Posted on 12月 3rd, 2012 by cx20

    SWT(Clojure)

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

    ソースコード

    (import 
      (org.eclipse.swt SWT)
      (org.eclipse.swt.widgets Display)
      (org.eclipse.swt.widgets Shell)
      (org.eclipse.swt.widgets Label)
      (org.eclipse.swt.layout FillLayout)
      (org.eclipse.swt.events ShellAdapter))
     
    (def display
      (Display.))
     
    (def shell
      (Shell. display))
     
    (def layout
      (FillLayout. SWT/VERTICAL))
     
    (def label
      (Label. shell SWT/BORDER))
     
    (doto label
      (.setText "Hello, SWT World!"))
     
    (doto shell
      (.setText "Hello, World")
      (.setLayout layout)
      (.setSize 640 480)
      (.addShellListener
        (proxy [ShellAdapter] []
          (shellClosed [evt]
            (System/exit 0))))
      (.open))
     
    (defn while-loop [display shell]
      (loop []
        (if (.isDisposed shell)
          (.dispose display)
          (do
            (if (not (.readAndDispatch display))
              (.sleep display))
            (recur)))))
     
    (while-loop display shell))

    実行方法

    C:¥> SET CLASSPATH=org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> clj Hello.clj

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, SWT(Jython) World!

    Posted on 11月 27th, 2012 by cx20

    SWT(Jython)

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

    ソースコード

    from java.lang import System
    from org.eclipse.swt import SWT
    from org.eclipse.swt.widgets import Display
    from org.eclipse.swt.widgets import Shell
    from org.eclipse.swt.widgets import Label
    from org.eclipse.swt.layout import FillLayout
     
    display = Display()
    shell = Shell(display)
    shell.setText("Hello, World")
     
    layout = FillLayout(SWT.VERTICAL)
    shell.setLayout(layout)
     
    label = Label(shell,SWT.BORDER)
    label.setText("Hello, SWT World!")
     
    shell.setSize( 640, 480 )
    shell.open()
     
    while not shell.isDisposed():
        if not display.readAndDispatch():
            display.sleep()
     
    display.dispose()

    実行方法

    C:¥> SET CLASSPATH=org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
    C:¥> jython Hello.py

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. 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!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. Hello, SWT(Scala) World!

    Posted on 11月 15th, 2012 by cx20

    SWT(Scala)

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

    ソースコード

    import org.eclipse.swt.SWT
    import org.eclipse.swt.widgets._
    import org.eclipse.swt.layout._
     
    object Hello {
        def main (args: Array[String]) {
            var display = new Display()
            var shell = new Shell(display)
            shell.setText("Hello, World")
     
            var layout = new FillLayout(SWT.VERTICAL)
            shell.setLayout(layout)
     
            var label = new Label(shell,SWT.BORDER)
            label.setText("Hello, SWT World!")
     
            shell.setSize( 640, 480 )
            shell.open()
     
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep()
                }
            }
     
            display.dispose()
        }
    }

    コンパイル&実行方法

    C:¥> scalac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.scala
    C:¥> scala -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, SWT(Groovy) World!

    Posted on 11月 9th, 2012 by cx20

    SWT(Groovy)

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

    ソースコード

    import org.eclipse.swt.SWT
    import org.eclipse.swt.widgets.*
    import org.eclipse.swt.layout.*
     
    class Hello {
        static void main (args) {
            def display = new Display()
            def shell = new Shell(display)
            shell.setText("Hello, World")
     
            def layout = new FillLayout(SWT.VERTICAL)
            shell.setLayout(layout)
     
            def label = new Label(shell,SWT.BORDER)
            label.setText("Hello, SWT World!")
     
            shell.setSize( 640, 480 )
            shell.open();
     
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep()
                }
            }
     
            display.dispose()
        }
    }

    実行方法

    C:¥> groovyw -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar Hello.groovy

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, SWT(Java) World!

    Posted on 11月 3rd, 2012 by cx20

    SWT(Java)

    SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
    IBM により AWT や Swing を置き換える目的で作成された。

    ソースコード

    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.layout.*;
     
    public class Hello {
        public static void main (String [] args) {
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setText("Hello, World");
     
            FillLayout layout = new FillLayout(SWT.VERTICAL);
            shell.setLayout(layout);
     
            Label label = new Label(shell,SWT.BORDER);
            label.setText("Hello, SWT World!");
     
            shell.setSize( 640, 480 );
            shell.open();
     
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
     
            display.dispose();
        }
    }

    コンパイル&実行方法

    C:¥> javac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.java
    C:¥> javaw -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, SWT World!                         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  8. Hello, Win32 GUI(SWT) World!

    Posted on 7月 18th, 2012 by cx20

    Win32 GUI(Java)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は Java の GUI ツールキット SWT の非公開 API を使用した Win32 GUI アプリケーション の例となっている。

    ソースコード

    import org.eclipse.swt.internal.Callback;
    import org.eclipse.swt.internal.win32.MSG;
    import org.eclipse.swt.internal.win32.OS;
    import org.eclipse.swt.internal.win32.PAINTSTRUCT;
    import org.eclipse.swt.internal.win32.RECT;
    import org.eclipse.swt.internal.win32.TCHAR;
    import org.eclipse.swt.internal.win32.WNDCLASS;
     
    public class Hello {
        public static int WndProc(int hWnd, int uMsg, int wParam, int lParam) {
            int hdc;
            PAINTSTRUCT lpPaint = new PAINTSTRUCT();
            String strMessage = "Hello, Win32 GUI(SWT) World!";
     
            switch (uMsg) {
            case OS.WM_PAINT:
                RECT rect = new RECT();
                hdc = OS.BeginPaint(hWnd, lpPaint);
                OS.GetClientRect(hWnd, rect);
                OS.DrawTextW(hdc, strMessage.toCharArray(), strMessage.length(), rect, OS.DT_SINGLELINE);
                OS.EndPaint(hWnd, lpPaint);
                return 0;
            case OS.WM_DESTROY:
                System.exit(wParam);
            default:
                return OS.DefWindowProc(hWnd, uMsg, wParam, lParam);
            }
        }
     
        public static void WinMain(String[] args) {
            int hInstance = OS.GetModuleHandle(null);
            TCHAR className  = new TCHAR(0, "helloWindow", true);
            TCHAR windowName = new TCHAR(0, "Hello, World", true);
     
            WNDCLASS wc      = new WNDCLASS();
            wc.style         = OS.CS_HREDRAW | OS.CS_VREDRAW;
            wc.lpfnWndProc   = new Callback(Hello.class, "WndProc", 4).getAddress();
            wc.hInstance     = hInstance;
            wc.hCursor       = OS.LoadCursor(0, OS.IDC_ARROW);
            wc.hbrBackground = OS.GetStockObject(OS.COLOR_WINDOW + 1 );
            wc.lpszClassName = OS.HeapAlloc(OS.GetProcessHeap(), OS.HEAP_ZERO_MEMORY, className.length() * 2);
            OS.MoveMemory(wc.lpszClassName, className, className.length() * 2);
     
            OS.RegisterClass(wc);
     
            int hWnd = OS.CreateWindowEx(
                0,
                className,
                windowName,
                OS.WS_OVERLAPPEDWINDOW,
                OS.CW_USEDEFAULT, 
                OS.CW_USEDEFAULT,
                640,
                480,
                0,
                0,
                hInstance,
                null);
     
            OS.ShowWindow(hWnd, OS.SW_SHOW);
            OS.UpdateWindow(hWnd);
     
            MSG lpMsg = new MSG();
            while (OS.GetMessage(lpMsg, 0, 0, 0)) {
                OS.TranslateMessage(lpMsg);
                OS.DispatchMessage(lpMsg);
            }
        }
     
        public static void main(String[] args) {
            WinMain( args );
        }
     
    }

    コンパイル&実行方法

    C:¥> javac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.java
    C:¥> java -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(SWT) World!              |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+