ラベル(Label)は最も基本的なコンポーネントです。ウィンドウ上に文字を表示したい場合に使用します。
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; public class LabelSample1 { public static void main(String[] args){ Display display = new Display (); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1,true)); shell.setText("LabelSample1"); // ラベル Label label1 = new Label(shell,SWT.NULL); label1.setText("Label1"); // 複数行のラベル Label label2 = new Label(shell,SWT.NULL); label2.setText("1行め\n2行め"); // フォントを指定 Label label3 = new Label(shell,SWT.NULL); label3.setText("フォントを指定します"); label3.setFont(new Font(display,"MS 明朝",20,SWT.NORMAL)); shell.pack(); shell.open(); while (!shell.isDisposed ()){ if (!display.readAndDispatch ()){ display.sleep (); } } display.dispose (); } }
ラベルには文字だけでなく、画像を貼り付けることもできます。
// イメージを作成 Image image = new Image(display,"images/icon.gif"); image.setBackground(shell.getBackground()); // イメージを貼り付けたラベルを作成 Label imageLabel = new Label(shell,SWT.NULL); imageLabel.setImage(image);
ラベルはこの他にもオプションでボーダーをつけたり、セパレータとして使用することもできます。
// ボーダーつきのラベルを作成 Label borderLabel = new Label(shell,SWT.BORDER); // セパレータ(水平線)を作成 Label h_separator = new Label(shell,SWT.SEPARATOR|SWT.HORIZONTAL); // セパレータ(縦線)を作成 Label v_separator = new Label(shell,SWT.SEPARATOR|SWT.VERTICAL);
最終更新時間:2004年03月09日 03時32分06秒