<更新記録>
2007年 12月 12日
作成
2008年 6月 5日
更新

姉妹サイト検索 Web検索


Composite

Composite

Compositeクラスは、controlの入れ物として使うことができます。 ShellクラスもCompositeクラスの子クラスで、controlを作成する際に、 shellを親compositeとして指定することもできます。

GUIアプリケーションを作成する場合には、 すべてのコントロールを最上位ウィンドウにべた書きで配置するのではなく、 それぞれのセクションごとにコンポジットを作成し、そこにコントロールを配置していくという手段をとったほうが、 プログラムの可読性、保守性の面で有利です。

このページでは、Compositeクラスの基本的な使い方と、便利な使い方について説明します。

compositeの基本的な使い方

このサンプルプログラムでは、shellにcompositeを格納し、compositeにlabelを格納しています。

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
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 LabelDemonstrate {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		
		// shellの子としてcomposite作成
		Composite composite = new Composite(shell, SWT.NONE);
		
		// compositeの子としてlabel1を作成
		Label label1 = new Label(composite, SWT.NONE);
		label1.setText("this is label 1");
		label1.setLocation(0, 0);
		label1.pack();
		
		// compositeの子としてlabel2を作成
		Label label2 = new Label(composite, SWT.NONE);
		label2.setText("this is label 2");
		Point l1size = label1.getSize();
		// label2をlabel1とはずらして表示
		label2.setLocation(0, l1size.y);
		label2.pack();
		
		// label1とlabel2を表示するのに最低限必要なサイズにパック
		composite.pack();
		shell.pack();
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		display.dispose();
	}
}

ここで、setLocation(int,int)メソッドで設定した値は、親コンポジットの左上隅からの相対位置になります。 つまりサンプルプログラムで作成したlabel1は、親コンポジットの左上隅から右へ0、左へ0進んだ位置に配置されます。
そしてlabel2は、親コンポジットの左上隅から右へ0、下へlabel1のサイズ分だけ下へ進んだ位置に配置されます。

compositeの便利な使い方

compositeの便利な使い方は、Compositeクラスを継承して、あるまとまった単位を描画するcompositeを、 別のクラスに定義してしまうこと です。 例えば、以下のようなLabelCompositeクラスを定義します。

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class LabelComposite extends Composite {
	public LabelComposite(Composite parent) {
		// shellの子としてこのLabelComposite作成
		super(parent, SWT.NONE);
		
		// LabelCompositeの子としてlabel1を作成
		Label label1 = new Label(this, SWT.NONE);
		label1.setText("this is label 1");
		label1.setLocation(0, 0);
		label1.pack();
		
		// LabelCompositeの子としてlabel2を作成
		Label label2 = new Label(this, SWT.NONE);
		label2.setText("this is label 2");
		Point l1size = label1.getSize();
		label2.setLocation(0, l1size.y);
		label2.pack();
	}
}

ここで定義したLabelCompositeクラスは、次のLabelDemonstrate2で使用することができます。

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class LabelDemonstrate2 {
	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		
		// 中にlabelを2つ格納したcompositeインスタンス作成
		LabelComposite labelComposite = new LabelComposite(shell);
		
		// labelCompositeをパック
		labelComposite.pack();
		shell.pack();
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		
		display.dispose();
	}
}

このLabelDemonstrate2を実行すると、先ほど紹介したLabelDemonstrateと同じ結果になります。
当サイトでは、SWT/JFaceの説明をしていく中で、このように、Compositeクラスを継承したクラスを、大きな1つの部品として定義しておいて、 それを他から使うといったことをしますので、覚えておいてください。


Powered by VeryEasyCMS