SWT, Standard Widget Toolkit を利用した基本的なサンプルを紹介します。今回は Shell, Label, Button クラスのウィジェットです。
Shell
Shell クラスのインスタンスはトップレベルのウィンドウです。
List: SWTAppShell.java
package shell;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SWTAppShell {
Shell shell;
public SWTAppShell(Display display) {
shell = new Shell(display);
shell.setText("Shell");
shell.setSize(200, 200);
shell.setLocation(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void main(String[] args) {
Display display = new Display();
new SWTAppShell(display);
display.dispose();
}
}
実行例
SWT のアプリケーションでは、まず Display クラスのインスタンスを作成します。 このインスタンスは、SWT のアプリケーションと、下部で動作している OS とを結び付けています。
Shell クラスのインスタンスは、トップレベルのウィンドウを生成しています。
SWT のアプリケーションでは下記のようにイベントループの処理を明確に記述する必要があります。
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
Shell クラスのインスタンス shell が、ウィンドウが閉じられるなどの処理がされるまでループが継続されます。このループは、アプリケーションが終了された時にリソースを開放する display.dispose(); の処理の前に定型句のように記述しておく必要があります。
Label
Label クラスのインスタンスは 文字や画像、あるいはセパレータを表示します。
List: SWTAppLabel.java
package label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class SWTAppLabel {
Shell shell;
public SWTAppLabel(Display display) {
shell = new Shell(display);
shell.setText("Label");
initUI();
shell.setLocation(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
shell.setBackground(new Color(null, 220, 220, 255));
GridLayout gl = new GridLayout(1, true);
gl.marginBottom = 2;
gl.marginTop = 2;
gl.marginLeft = 10;
gl.marginRight = 10;
shell.setLayout(gl);
Label lab = new Label(shell, SWT.CENTER);
lab.setText("こんにちは、世界!");
lab.setBackground(new Color(null, 200, 200, 255));
shell.pack();
}
public static void main(String[] args) {
Display display = new Display();
new SWTAppLabel(display);
display.dispose();
}
}
実行例
Button
Button クラスのインスタンスは プッシュボタン、チェックボタン、ラジオボタンなど複数の種類のボタンとして利用できます。
List: SWTAppButton.java
package button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
public class SWTAppButton {
Shell shell;
public SWTAppButton(Display display) {
shell = new Shell(display);
shell.setText("Button");
initUI();
shell.pack();
shell.setLocation(100, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public void initUI() {
shell.setBackground(new Color(null, 220, 255, 255));
GridLayout gl = new GridLayout(1, true);
gl.marginBottom = 2;
gl.marginTop = 2;
gl.marginLeft = 4;
gl.marginRight = 4;
shell.setLayout(gl);
GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
// プッシュボタン
Button but = new Button(shell, SWT.PUSH);
but.setText("プッシュボタン");
but.setLayoutData(gd);
but.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Button b = (Button) e.widget;
System.out.println(b.getText() + "がクリックされました。");
}
});
// 矢印ボタン
Button arw = new Button(shell, SWT.ARROW | SWT.DOWN);
arw.setLayoutData(gd);
arw.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
System.out.println("矢印ボタンがクリックされました。");
}
});
// チェックボタン
Button chk = new Button(shell, SWT.CHECK);
chk.setText("チェックボタン");
chk.setLayoutData(gd);
chk.addSelectionListener(toggleSelectionAdapter);
// ラジオボタン
Group rbgroup = new Group(shell, SWT.SHADOW_IN);
rbgroup.setText("グループ");
rbgroup.setLayout(new RowLayout(SWT.VERTICAL));
Button rb1 = new Button(rbgroup, SWT.RADIO);
rb1.setText("ラジオボタンA");
rb1.addSelectionListener(radioSelectionAdapter);
Button rb2 = new Button(rbgroup, SWT.RADIO);
rb2.setText("ラジオボタンB");
rb2.addSelectionListener(radioSelectionAdapter);
Button rb3 = new Button(rbgroup, SWT.RADIO);
rb3.setText("ラジオボタンC");
rb3.addSelectionListener(radioSelectionAdapter);
rb1.setSelection(true);
// トグルボタン
Button tgl = new Button(shell, SWT.TOGGLE);
tgl.setText("トグルボタン");
tgl.setLayoutData(gd);
tgl.addSelectionListener(toggleSelectionAdapter);
}
private SelectionAdapter radioSelectionAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Button b = (Button) e.widget;
if (b.getSelection()) {
System.out.println(b.getText() + "が選択されました。");
}
}
};
private SelectionAdapter toggleSelectionAdapter = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Button b = (Button) e.widget;
if (b.getSelection()) {
System.out.println(b.getText() + "がオンになりました。");
} else {
System.out.println(b.getText() + "オフになりました。");
}
}
};
public static void main(String[] args) {
Display display = new Display();
new SWTAppButton(display);
display.dispose();
}
}
実行例
List: コンソール上の出力
プッシュボタンがクリックされました。 矢印ボタンがクリックされました。 チェックボタンがオンになりました。 ラジオボタンBが選択されました。 トグルボタンがオンになりました。





0 件のコメント:
コメントを投稿