import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.List;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class HelloSWT extends Shell{
// 這些都是在下面將要用到的控件
private static Text text;
private static Button swtButton;
private static Button awtButton;
private static Button swingButton;
private static Group group;
private static Button button;
private static Label benefitOfSwtLabel;
private static List list;
public static void main(String[] args) {
/**
* 創(chuàng)建一個Display對象,并使用這個Display創(chuàng)建一個
* 窗口Shell,并設(shè)置它的標(biāo)題和窗口初始尺寸。因?yàn)楫?dāng)
* 前線程創(chuàng)建了Display對象,所以它是界面線程
*/
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("Hello SWT");
shell.setSize(260, 283);
shell.open();
/**
* 創(chuàng)建一個Text空間并設(shè)置其中文字的內(nèi)容。然后設(shè)置了
* 空間在窗口中的位置
* 左上頂點(diǎn)為(10,8),寬度為230,高度為35
*/
text = new Text(shell, SWT.BORDER);
text.setText("SWT是Eclipse平臺使用的圖形工具箱");
text.setBounds(10, 8, 230, 35);
/**
* List控件可以用來展示一系列的內(nèi)容。這里創(chuàng)建了4條內(nèi)容
* 的List控件
*/
list = new List(shell, SWT.BORDER);
list.setItems(new String[] {
"使用操作系統(tǒng)本地控件",
"提供一套平臺無關(guān)的API",
"GUI程序的運(yùn)行速度快",
"更多更多......"
});
/**
* Label控件可以在界面上展示不能修改的文字,通常作為
* 標(biāo)簽來使用
*/
benefitOfSwtLabel = new Label(shell, SWT.NONE);
benefitOfSwtLabel.setText("SWT的優(yōu)點(diǎn):");
benefitOfSwtLabel.setBounds(10, 49, 90, 15);
/**
* Group對象可以用來把相關(guān)的控件組成一組,在這一組控件
* 的外面會顯示出一個邊框,將它們和其他控件隔離開來。在邊框
* 上可以加入文字標(biāo)題以說明這一組控件的作用。
*/
group = new Group(shell, SWT.NONE);
group.setText("你使用過哪些圖形工具箱?");
group.setBounds(10, 159, 230, 47);
/**
* Button類型包含普通按鈕,單選按鈕,復(fù)選按鈕等很多形式。
*
*/
awtButton = new Button(group, SWT.CHECK);
awtButton.setText("AWT");
awtButton.setBounds(10, 20, 54, 18);
swingButton = new Button(group, SWT.CHECK);
swingButton.setText("Swing");
swingButton.setBounds(70, 22, 60, 15);
swtButton = new Button(group, SWT.CHECK);
swtButton.setBounds(136, 22, 62, 15);
swtButton.setText("SWT");
button = new Button(shell, SWT.None);
button.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent arg0) {
MessageBox messageBox =
new MessageBox(shell, SWT.ICON_INFORMATION);
messageBox.setMessage("Hello SWT");
messageBox.open();
}
@Override
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
});
button.setText("按一下按鈕,向SWT說Hello!");
button.setBounds(10, 214, 227, 25);
shell.layout();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
來源:https://www./content-1-599801.html
|