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) {
/**
* 创建一个Display对象,并使用这个Display创建一个
* 窗口Shell,并设置它的标题和窗口初始尺寸。因为当
* 前线程创建了Display对象,所以它是界面线程
*/
Display display = Display.getDefault();
final Shell shell = new Shell(display);
shell.setText("Hello SWT");
shell.setSize(260, 283);
shell.open();
/**
* 创建一个Text空间并设置其中文字的内容。然后设置了
* 空间在窗口中的位置
* 左上顶点为(10,8),宽度为230,高度为35
*/
text = new Text(shell, SWT.BORDER);
text.setText("SWT是Eclipse平台使用的图形工具箱");
text.setBounds(10, 8, 230, 35);
/**
* List控件可以用来展示一系列的内容。这里创建了4条内容
* 的List控件
*/
list = new List(shell, SWT.BORDER);
list.setItems(new String[] {
"使用操作系统本地控件",
"提供一套平台无关的API",
"GUI程序的运行速度快",
"更多更多......"
});
/**
* Label控件可以在界面上展示不能修改的文字,通常作为
* 标签来使用
*/
benefitOfSwtLabel = new Label(shell, SWT.NONE);
benefitOfSwtLabel.setText("SWT的优点:");
benefitOfSwtLabel.setBounds(10, 49, 90, 15);
/**
* Group对象可以用来把相关的控件组成一组,在这一组控件
* 的外面会显示出一个边框,将它们和其他控件隔离开来。在边框
* 上可以加入文字标题以说明这一组控件的作用。
*/
group = new Group(shell, SWT.NONE);
group.setText("你使用过哪些图形工具箱?");
group.setBounds(10, 159, 230, 47);
/**
* Button类型包含普通按钮,单选按钮,复选按钮等很多形式。
*
*/
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
|