请重新输入!");
由于用到了 JFace的对话框,在程序的前面要引入相应的包:
import org.eclipse.jface.dialogs.*;
输入密码时要在密码框显示“*”,需 要添加SWT.PASSWORD选项:
final Text text_1=new Text (shell,SWT.BORDER|SWT.PASSWORD);
SWT Designer简介(4)
时间:2011-04-23 哪热
2.5运行程序
在Eclipse包资源 管理器中,右单击文件名,在弹出的菜单中选择【运行方式】→【SWT应用程序】,运行 结果如图6所示。输入用户名和密码,点击【提交】,则出现用户登录对话框,如图7所示。 如果用户名或密码为空,则出现错误提示对话框,如图8所示。
图6 用户 登录窗体
图7 用户 登录对话框
图8 错误信息对话框
SWT Designer简介(5)
时间:2011-04-23 哪热
2.6 程序源程序
源程序
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 login {
private Text text_1;
private Text text;
protected Shell shell;
/** *//**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
login window = new login();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/** *//**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (! shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/** *//**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(411, 359);
shell.setText("SWT Application");
text = new Text(shell, SWT.BORDER);
text.setBounds(197, 109, 142, 25);
text_1 = new Text (shell, SWT.BORDER|SWT.PASSWORD);
text_1.setBounds(197, 173, 142, 25);
final Button button = new Button(shell, SWT.ABORT);
button.setText("提交");
button.setBounds(85, 263, 48, 22);
final Button button_1 = new Button(shell, SWT.ABORT);
button_1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(final SelectionEvent e)
{
}
});
button_1.setText(" 取消");
button_1.setBounds(247, 263, 48, 22);
final Label label = new Label(shell, SWT.NONE);
label.setText ("用户名");
label.setBounds(106, 112, 48, 22);
final Label label_1 = new Label(shell, SWT.NONE);
label_1.setText("密 码");
label_1.setBounds(106, 176, 48, 25);
button.addSelectionListener(new SelectionAdapter() {
publi
|