1、继承Dialog类。
//JFace包和SWT包都有Dialog类,这里继承的是JFace的Dialog类
public class LoginDialog extends Dialog {
public LoginDialog(Shell parentShell) {
super(parentShell);
setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
}
2、 添加界面组件。
//在createDialogArea方法中构建Dialog的界面内容
@Override
protected Control createDialogArea(Composite parent)` {
Composite container = new Composite(parent, SWT.NONE);
GridLayout gLayout = new GridLayout(2, false);
// 应用GridLayout布局
container.setLayout(gLayout);
// 添加文本标签
Label label_1 = new Label(container, SWT.NONE);
label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
label_1.setText("用户:");
// 添加文本框
textUser = new Text(container, SWT.BORDER);
textUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textUser.setText("请输入用户名");
Label lblNewLabel = new Label(container, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblNewLabel.setText("密码:");
textPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
textPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textUser.setFocus();
return container;
}
3、 设置对话框按钮样式。
// 改写父类创建按钮的方法使其失效。
//parent:取得放置按钮的面板;id:定义按钮的ID值;label:按钮文字;参数defaultButton:是否为Dialog的默认按钮。
protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
return null;
}
// 改写父类的initializeBounds方法,并调用父类的createButton方法建立按钮,其中RESET_ID为自定义按钮的ID值。
protected void initializeBounds() {
// 取得按钮面板
Composite comp = (Composite) getButtonBar();
super.createButton(comp, IDialogConstants.OK_ID, "确定", false);
super.createButton(comp, IDialogConstants.CANCEL_ID, "取消", false);
super.createButton(comp, RESET_ID, "重置", true);
super.initializeBounds();
}
4、 添加按钮点击处理方法。
//单击对话框中的按钮后执行此方法,参数buttonId是被单击按钮的ID值。
@Override
protected void buttonPressed(int buttonId) {
if(buttonId == IDialogConstants.OK_ID) {
String username = textUser.getText();
String password = textPassword.getText();
if (username.isEmpty() || password.isEmpty()) {
MessageDialog msgDlg = new MessageDialog(this.getShell(), "登录失败", null, "登录错误,请重新输入用户名、密码、验证码!",
MessageDialog.ERROR, new String[] { IDialogConstants.OK_LABEL }, 0);
msgDlg.open();
return;
}
}else if(buttonId == RESET_ID){
this.close();
this.open();
}
super.buttonPressed(buttonId);
}
5、 自定义对话框的大小。
//设置自定义对话框的大小
@Override
protected Point getInitialSize() {
return new Point(330, 180);
}
6、 运行截图如下:
LoginDialog loginDlg = new LoginDialog(shell);
loginDlg.open();
代码原文如下:
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
//JFace包和SWT包都有Dialog类,这里继承的是JFace的Dialog类
public class LoginDialog extends Dialog {
private Text textUser;
private Text textPassword;
public static final int RESET_ID = 101;
public LoginDialog(Shell parentShell) {
super(parentShell);
setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
}
// 改写父类创建按钮的方法使其失效。
//parent:取得放置按钮的面板;id:定义按钮的ID值;label:按钮文字;参数defaultButton:是否为Dialog的默认按钮。
protected Button createButton(Composite parent, int id, String label, boolean defaultButton) {
return null;
}
// 改写父类的initializeBounds方法,并调用父类的createButton方法建立按钮,其中RESET_ID为自定义按钮的ID值。
protected void initializeBounds() {
// 取得按钮面板
Composite comp = (Composite) getButtonBar();
super.createButton(comp, IDialogConstants.OK_ID, "确定", false);
super.createButton(comp, IDialogConstants.CANCEL_ID, "取消", false);
super.createButton(comp, RESET_ID, "重置", true);
super.initializeBounds();
}
//在createDialogArea方法中构建Dialog的界面内容
@Override
protected Control createDialogArea(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
GridLayout gLayout = new GridLayout(2, false);
// 应用GridLayout布局
container.setLayout(gLayout);
// 添加文本标签
Label label_1 = new Label(container, SWT.NONE);
label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
label_1.setText("用户:");
// 添加文本框
textUser = new Text(container, SWT.BORDER);
textUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textUser.setText("请输入用户名/手机号/邮箱地址");
Label lblNewLabel = new Label(container, SWT.NONE);
lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblNewLabel.setText("密码:");
textPassword = new Text(container, SWT.BORDER | SWT.PASSWORD);
textPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
textUser.setFocus();
return container;
}
//单击对话框中的按钮后执行此方法,参数buttonId是被单击按钮的ID值。
@Override
protected void buttonPressed(int buttonId) {
if(buttonId == IDialogConstants.OK_ID) {
String username = textUser.getText();
String password = textPassword.getText();
if (username.isEmpty() || password.isEmpty()) {
MessageDialog msgDlg = new MessageDialog(this.getShell(), "登录失败", null, "登录错误,请重新输入用户名、密码、验证码!",
MessageDialog.ERROR, new String[] { IDialogConstants.OK_LABEL }, 0);
msgDlg.open();
return;
}
}else if(buttonId == RESET_ID){
this.close();
this.open();
}
super.buttonPressed(buttonId);
}
//设置自定义对话框的大小
@Override
protected Point getInitialSize() {
return new Point(330, 180);
}
}