SWT - グループの例

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

グループウィジェットを作成する方法

より整理するために、グループは通常、別のクラスで作成され、extend the 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クラスを作成し、表示用にシェルに追加します。

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 ();
}
}

image

別のグループクラスを作成する必要があるのはなぜですか?

メインシェルの表示クラスにグループクラスを含めることは、何の問題もありません。 別のグループクラスにより、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 ();
}
}