⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logindialog.java

📁 Simple SWT for java newbie. good example for you
💻 JAVA
字号:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class LoginDialog extends Dialog {

	private Text passwordText;
	private Text userIdText;
	protected Object result;
	protected Shell shell;
	private final DBUtil dbUtil;
	private Label messageLabel;

	/**
	 * Create the dialog
	 * @param parent
	 * @param dbUtil
	 */
	public LoginDialog(Shell parent, DBUtil dbUtil) {
		super(parent);
		this.dbUtil = dbUtil;
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		final GridLayout gridLayout = new GridLayout();
		shell.setLayout(gridLayout);
		shell.setSize(299, 168);
		shell.setText("Login");

		final Composite composite_1 = new Composite(shell, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_2 = new GridLayout();
		gridLayout_2.numColumns = 2;
		composite_1.setLayout(gridLayout_2);

		messageLabel = new Label(composite_1, SWT.NONE);
		messageLabel.setAlignment(SWT.CENTER);
		final GridData gd_messageLabel = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		messageLabel.setLayoutData(gd_messageLabel);

		final Label idUserLabel = new Label(composite_1, SWT.NONE);
		idUserLabel.setText("ID User");

		userIdText = new Text(composite_1, SWT.BORDER);
		final GridData gd_userIdText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		userIdText.setLayoutData(gd_userIdText);

		final Label passwordLabel = new Label(composite_1, SWT.NONE);
		passwordLabel.setText("Password");

		passwordText = new Text(composite_1, SWT.BORDER);
		passwordText.setEchoChar('*');
		final GridData gd_passwordText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		passwordText.setLayoutData(gd_passwordText);

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		composite.setLayout(gridLayout_1);

		final Button loginButton = new Button(composite, SWT.NONE);
		loginButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				doLogin();
			}
		});
		final GridData gd_loginButton = new GridData(SWT.RIGHT, SWT.CENTER, true, false);
		gd_loginButton.widthHint = 70;
		loginButton.setLayoutData(gd_loginButton);
		loginButton.setText("Login");

		final Button keluarButton = new Button(composite, SWT.NONE);
		keluarButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				doExit();
			}
		});
		final GridData gd_keluarButton = new GridData(SWT.LEFT, SWT.CENTER, true, false);
		gd_keluarButton.widthHint = 70;
		keluarButton.setLayoutData(gd_keluarButton);
		keluarButton.setText("Keluar");
		//
	}

	protected void doLogin() {
		dbUtil.setUsername(userIdText.getText());
		dbUtil.setPassword(passwordText.getText());
		boolean connected = dbUtil.connect();
		if (connected) {
			shell.dispose();
		}
		else {
			messageLabel.setText("Login failed");
		}
	}

	protected void doExit() {
		System.exit(0);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -