Java程序设计之swt教程 下载本文

内容发布更新时间 : 2024/5/14 8:40:26星期一 下面是文章的全部内容请认真阅读。

图4.50 错误信息对话框 ⑥完整的源程序

package edu.ch4;

import org.eclipse.swt.SWT;

import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.eclipse.jface.dialogs.*; public class SWTDesignerTest {

public static void main(String[] args) {

final Display display = Display.getDefault(); final Shell shell = new Shell(); shell.setSize(500, 375); shell.setText(\应用程序\); shell.open();

final Text text = new Text(shell, SWT.BORDER); text.setBounds(249, 50, 146, 27);

final Text text_1 = new Text(shell, SWT.BORDER|SWT.PASSWORD); text_1.setBounds(249, 147, 146, 27);

final Label label = new Label(shell, SWT.NONE); label.setText(\请输入用户名:\); label.setBounds(71, 50, 118, 27);

final Label label_1 = new Label(shell, SWT.NONE); label_1.setBounds(71, 147, 118, 27); label_1.setText(\请输入密码:\);

final Button button = new Button(shell, SWT.NONE); button.addSelectionListener(new SelectionAdapter() {

public void widgetSelected(SelectionEvent e) {

if(text.getText()!=\|text_1.getText()!=\)

//调用JFace的对话框显示登录信息

MessageDialog.openInformation(shell,\登录信息\,\欢迎\+text.getText()+\进入系统!\);

else

//调用JFace的对话框显示出错信息

MessageDialog.openError(shell,\错误\,\用户名或密码为空,请重新输入!\);

});

button.setText(\提交\);

button.setBounds(74, 245, 115, 27);

final Button button_1 = new Button(shell, SWT.NONE); button_1.setBounds(280, 245, 115, 27);

}

}

}

button_1.setText(\重置\); shell.layout();

while (!shell.isDisposed()) { }

if (!display.readAndDispatch())

display.sleep();

4.7 本章小结

与AWT和SWING相比,SWT提供了和本机系统相同的用户界面组件,具有较好的运行效率和稳定的平台表现,因此近年来取得了很快的发展。本章介绍了SWT常用组件、SWT常用容器类、SWT常用的布局方式、SWT事件处理方法等图形化用户界面开发技术。最后介绍了一款SWT可视化界面开发插件包——SWT Designer。通过大量实例,演示了SWT开发桌面应用程序的基本技术和技巧。

4.8 习题

1.试述创建一个典型的SWT应用程序常用的步骤。 2.简述org.eclipse.swt.widgets包的用途。 3.简述GridLayout的常用属性。

4.简述FormAttachment类的作用,它有几个构造方法。构造方法中的参数表示什么含义?

5.完成图4.51所示图形界面的制作。要求“查询结果”用group组件。

图4.51 数据查询界面

6.完成图4.52所示学籍管理主界面的设计与制作。

图4.52 学籍管理系统主界面

7.在图4.52所示界面中,在“用户登录”菜单中添加组件选择事件,当选中“用户登录”时,打开图4.48所示用户登录界面。