パーソナルツール
文書操作

HelloButton2.java

作成者 fujisaki 最終変更日時 2008年12月08日 02時18分

Click here to get the file

サイズ 1.7 kB - File type text/x-java

ファイルのコンテンツ

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.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

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

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

	// フレーム表示
	frame.open();
	

	// パネル作成
	final Composite compo = new Composite(frame, SWT.EMBEDDED);

	// パネルのレイアウト設定
	compo.setLayout(new GridLayout());
	

	// ボタン作成
	Button button = new Button(compo, SWT.PUSH);

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

	// ボタンに Selection リスナ追加
	button.addSelectionListener(new SelectionListener() {
		public void widgetDefaultSelected(SelectionEvent se) {
		    widgetSelected(se);
		}

		// 「Hello」ボタンが押されたとき呼ばれる
		public void widgetSelected(SelectionEvent se) {
		    // ラベル作成
		    Label label = new Label(compo, SWT.CENTER);

		    // パネルに「Hello! World!!」と表示
		    label.setText("Hello! World!!");

		    // パネルのサイズを再設定
		    compo.pack();
		}
	    });
	
	// パネルの初期サイズ設定
	compo.pack();

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