1. 程式人生 > >Java介面 SWT基本元件——Label

Java介面 SWT基本元件——Label

標籤(Label)基本型別與基本案例

SWT 的標籤有兩類,一類是顯示普通文字或圖片的標籤,一類是分割線。如圖所示,為兩種型別的標籤示意圖(文字+分割線)。
程式碼如下:
shell.setSize(200, 200);
shell.setText("SWT.SEP");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));	
		
RowLayout layout = new RowLayout(SWT.VERTICAL);
layout.marginWidth = 10;
shell.setLayout(layout);
		
Group group = new Group(shell, SWT.SHADOW_ETCHED_OUT); 
group.setLayout(new FillLayout(SWT.VERTICAL));
Label labelText = new Label(group, SWT.NONE);
labelText.setText("這是一個有分割線的文字標籤");
Label label = new Label(group, SWT.NONE|SWT.SEPARATOR|SWT.HORIZONTAL);
group.layout();
		
//shell.layout();
//shell.pack();
shell.open();


1. 標籤案例(LabelSample)

內容包含分割線(水平/垂直)、普通文字、佈局寬度設定。
shell.setSize(200, 200);
shell.setText("SWT.SEP&Label");
shell.setLayout(new FillLayout(SWT.VERTICAL));	
// 設定表格佈局
RowLayout layout = new RowLayout(SWT.VERTICAL);
layout.marginWidth = 10;					// 寬度10 ????
shell.setLayout(layout);

// 組·文字標籤
Group group1 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
group1.setLayout(new FillLayout(SWT.VERTICAL));
group1.setText("文字標籤");

Label label1 = new Label(group1, SWT.NONE|SWT.LEFT);
label1.setText("SWT.LEFT");

Label label2 = new Label(group1, SWT.NONE|SWT.CENTER);
label2.setText("SWT.CENTER");

Label label3 = new Label(group1, SWT.NONE|SWT.RIGHT);
label3.setText("SWT.RIGHT");

//group1.layout();
//group1.pack();

// 組·水平分割線
Group group2 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
group2.setLayout(new FillLayout(SWT.VERTICAL));
group2.setText("水平分割線:SWT.HORIZONTAL");

Label label4 = new Label(group2, SWT.NONE|SWT.LEFT);
label4.setText("SWT.SHADOW_IN");

Label sepLabel1 = new Label(group2, SWT.SEPARATOR|SWT.HORIZONTAL|SWT.SHADOW_IN);

Label label5 = new Label(group2, SWT.NONE|SWT.CENTER);
label5.setText("SWT.SHADOW_OUT");

Label sepLabel2 = new Label(group2, SWT.SEPARATOR|SWT.HORIZONTAL|SWT.SHADOW_OUT);

Label label6 = new Label(group2, SWT.NONE|SWT.RIGHT);
label6.setText("SWT.SHADOW_NONE");

Label sepLabel3 = new Label(group2, SWT.SEPARATOR|SWT.HORIZONTAL|SWT.SHADOW_NONE);

//group2.layout();
//group2.pack();

// 豎直分割線:SWT.VERTICAL
Group group3 = new Group(shell, SWT.SHADOW_ETCHED_OUT);
group3.setLayout(new FillLayout(SWT.HORIZONTAL));
group3.setText("豎直分割線:SWT.VERTICAL");

Label sepLabel4 = new Label(group3, SWT.SEPARATOR|SWT.VERTICAL|SWT.SHADOW_IN);

Label sepLabel5 = new Label(group3, SWT.SEPARATOR|SWT.VERTICAL|SWT.SHADOW_OUT);

Label sepLabel6 = new Label(group3, SWT.SEPARATOR|SWT.VERTICAL|SWT.SHADOW_NONE);

//group3.layout();
//group3.pack();

shell.layout();
shell.pack();
shell.open();

2. 自定義標籤(CLabel)

自定義標籤可以實現圖示和文字同時顯示.

shell.setSize(200, 200);
shell.setText("SWT.CLabel");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));	

CLabel cl = new CLabel(shell, SWT.LEFT);
cl.setText("這是一個帶有圖示的自定義標籤");
cl.setImage(new Image(shell.getDisplay(), "f:\\icon\\rswt.png"));

shell.layout();
shell.pack();
shell.open();