Personal tools
Document Actions

HelloButton.java

by fujisaki last modified 2008-12-08 02:17

Click here to get the file

Size 1.6 kB - File type text/x-java

File contents

package exercise.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

public class HelloButton {
    public static void main(String[] args) {
	// フレーム作成
	final Shell frame = new Shell();

	// フレームのサイズ設定
	frame.setSize(200, 200);

	// フレームのレイアウト設定
	frame.setLayout(new GridLayout());

	// フレーム表示
	frame.open();
	
	
	// フレームに追加するボタン作成
	Button button = new Button(frame, SWT.PUSH);

	// ボタンの名前
	button.setText("Hello");

	// 「Hello」ボタンに Selectionリスナ 追加
	button.addSelectionListener(new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent se) {
		    widgetSelected(se);
		}
		
		// 「Hello」ボタンが押されたとき呼び出される
		public void widgetSelected(SelectionEvent se) {
		    // フレームに追加するラベル作成
		    Label label = new Label(frame, SWT.CENTER);

		    // フレームに「Hello! World!!」と表示
		    label.setText("Hello! World!!");

		    // ラベルの初期サイズ設定
		    label.pack();
		}
	    });

	// フレームの初期サイズ設定
	button.pack();

	Display d = frame.getDisplay();
	while (!frame.isDisposed()) {
	    if (!d.readAndDispatch()) {
		d.sleep();
	    }
	}
	d.dispose();
    }   
}