SWT – グループの例
グループとは
SWTでは、GroupはCompositeクラスのサブクラスです。グループは、アプリケーションの外観を改善し、アプリケーション全体をより整理しやすくします。すべての子ウィジェットの周りに長方形の境界線を描画します。
グループウィジェットは5つのスタイルをサポートしています(実際はそれほど違いはありません)
{空} 1)SWT.SHADOW
IN 2)SWT.SHADOW
OUT 3)SWT.SHADOW
NONE 4)SWT.SHADOW
ETCHED
IN 5)SWT.SHADOW
ETCHED__OUT
グループウィジェットを作成するには?
それをもっと整理するために、Groupは通常別のクラスで作成され、
Composite
クラスを拡張する必要があります。
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
public class SWTGroup extends Composite
{
public SWTGroup(Composite parent)
{
super(parent, SWT.NONE);
this.setSize(300, 300);
Group group = new Group(this, SWT.SHADOW__ETCHED__IN);
group.setLocation(50, 50);
group.setText("Group SHADOW__IN");
Label label = new Label(group, SWT.NONE);
label.setText("Label in Group");
label.setLocation(20,20);
label.pack();
Button button = new Button(group, SWT.PUSH);
button.setText("Push button in Group");
button.setLocation(20,45);
button.pack();
group.pack();
}
}
SWTGroup(Groupクラス)は直接実行できません。そのコンストラクタを呼び出すアプリケーションが必要です。
ここでは、SWTGroupを呼び出すMainクラスを作成し、それをShellに追加して表示します。
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTMain {
public static void main (String[]args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setText("SWT Group Example");
SWTGroup swtGroup = new SWTGroup(shell);
shell.pack();
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}

なぜ、私は別のGroupクラスを作成する必要がありますか?
Main Shell表示クラスにGroupクラスを含めることは間違いありません。 Separate Groupクラスは、SWTアプリケーションをより整理しやすく保守しやすくします。
ここではすべてのクラスを1つのクラスに含める例ですが、ちょっとちょっと面倒です…
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SWTMain {
public static void main (String[]args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setText("SWT Group Example");
Group group = new Group(shell, SWT.SHADOW__IN);
group.setLocation(50, 50);
group.setText("Group SHADOW__IN");
Label label = new Label(group, SWT.NONE);
label.setText("Label in Group");
label.setLocation(20,20);
label.pack();
Button button = new Button(group, SWT.PUSH);
button.setText("Push button in Group");
button.setLocation(20,45);
button.pack();
group.pack();
shell.setSize(500,500);
shell.open();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}