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

📄 logindailog.java

📁 一个使用Java实现的类似与QQ的聊天程序。使用了Hibernate组建。可用于学习Java网络编程和Hiberante数据库组件的学习
💻 JAVA
字号:
package com.jim.client;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TypedEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;

public class LoginDailog extends Dialog {

	protected Object result;

	protected Shell shell;
	private Text portT;
	private Text serverT;
	private Text pwdT;
	private Text numberT;
	private static MainWin mainWin;
	private boolean connected = false;
	private boolean loggedin = false;
	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public LoginDailog(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public LoginDailog(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public boolean open() {
		createContents();
		Rectangle smallRect = shell.getBounds();
		Rectangle displayRect = getParent().getDisplay().getBounds();
		shell.setLocation((displayRect.width - smallRect.width) / 2, (displayRect.height - smallRect.height) / 2);
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return loggedin;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		//shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell = new Shell(getParent(), SWT.CLOSE | SWT.MIN);
		shell.setText("JIM聊天工具 - 登录");
		shell.setSize(348, 251);
		shell.setLayout(new FillLayout());
		shell.setImage(SWTResourceManager.getImage(LoginDailog.class, "resources/1.bmp"));
		shell.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent de) {
				closeWin(de);
			}
		});

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new FormLayout());

		final Group group = new Group(composite, SWT.NONE);
		final FormData formData = new FormData();
		formData.bottom = new FormAttachment(0, 155);
		formData.right = new FormAttachment(100, -5);
		formData.top = new FormAttachment(0, 5);
		formData.left = new FormAttachment(0, 5);
		group.setLayoutData(formData);
		group.setLayout(new FormLayout());

		final Label label = new Label(group, SWT.NONE);
		label.setAlignment(SWT.CENTER);
		final FormData formData_1 = new FormData();
		formData_1.left = new FormAttachment(0, 25);
		formData_1.right = new FormAttachment(0, 90);
		formData_1.bottom = new FormAttachment(0, 33);
		formData_1.top = new FormAttachment(0, 15);
		label.setLayoutData(formData_1);
		label.setText("JIM号码");

		numberT = new Text(group, SWT.BORDER);
		final FormData formData_2 = new FormData();
		formData_2.right = new FormAttachment(100, -6);
		formData_2.left = new FormAttachment(label, 5, SWT.RIGHT);
		formData_2.top = new FormAttachment(0, 10);
		numberT.setLayoutData(formData_2);

		final Label label_1 = new Label(group, SWT.NONE);
		final FormData formData_1_1 = new FormData();
		formData_1_1.bottom = new FormAttachment(0, 73);
		formData_1_1.top = new FormAttachment(0, 55);
		formData_1_1.right = new FormAttachment(label, 65, SWT.LEFT);
		formData_1_1.left = new FormAttachment(label, 0, SWT.LEFT);
		label_1.setLayoutData(formData_1_1);
		label_1.setAlignment(SWT.CENTER);
		label_1.setText("JIM密码");

		pwdT = new Text(group, SWT.PASSWORD | SWT.BORDER);
		pwdT.setTextLimit(20);
		pwdT.setTabs(0);
		pwdT.addKeyListener(new KeyAdapter() {
	            public void keyPressed(KeyEvent e) {
	                if(e.character == SWT.CR) {
	                	login(e);
	                }
	            }
	        });
		final FormData formData_2_1 = new FormData();
		formData_2_1.right = new FormAttachment(100, -6);
		formData_2_1.left = new FormAttachment(label_1, 5, SWT.RIGHT);
		formData_2_1.bottom = new FormAttachment(label_1, 13, SWT.DEFAULT);
		formData_2_1.top = new FormAttachment(label_1, -5, SWT.TOP);
		pwdT.setLayoutData(formData_2_1);

		final Label label_2 = new Label(group, SWT.NONE);
		final FormData formData_3 = new FormData();
		formData_3.top = new FormAttachment(0, 95);
		formData_3.right = new FormAttachment(label_1, 60, SWT.LEFT);
		formData_3.left = new FormAttachment(label_1, 0, SWT.LEFT);
		label_2.setLayoutData(formData_3);
		label_2.setText("服务器地址");

		serverT = new Text(group, SWT.BORDER);
		serverT.setText("localhost");
		final FormData formData_4 = new FormData();
		formData_4.bottom = new FormAttachment(0, 108);
		formData_4.top = new FormAttachment(0, 90);
		formData_4.left = new FormAttachment(label_1, 5, SWT.RIGHT);
		serverT.setLayoutData(formData_4);

		Label label_3;
		label_3 = new Label(group, SWT.NONE);
		formData_4.right = new FormAttachment(label_3, -10, SWT.LEFT);
		label_3.setAlignment(SWT.CENTER);
		final FormData formData_3_1 = new FormData();
		formData_3_1.left = new FormAttachment(100, -116);
		formData_3_1.bottom = new FormAttachment(0, 107);
		formData_3_1.top = new FormAttachment(0, 95);
		label_3.setLayoutData(formData_3_1);
		label_3.setText("端口号");

		portT = new Text(group, SWT.BORDER);
		portT.setText("9001");
		formData_3_1.right = new FormAttachment(portT, -5, SWT.LEFT);
		final FormData formData_4_1 = new FormData();
		formData_4_1.left = new FormAttachment(100, -71);
		formData_4_1.right = new FormAttachment(100, -6);
		formData_4_1.bottom = new FormAttachment(0, 108);
		formData_4_1.top = new FormAttachment(0, 90);
		portT.setLayoutData(formData_4_1);

		final Button registerB = new Button(composite, SWT.NONE);
		registerB.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent se) {
				register(se);
			}
		});
		final FormData formData_5 = new FormData();
		formData_5.top = new FormAttachment(0, 173);
		formData_5.bottom = new FormAttachment(0, 195);
		formData_5.right = new FormAttachment(0, 282);
		formData_5.left = new FormAttachment(0, 199);
		registerB.setLayoutData(formData_5);
		registerB.setText("注册新用户");

		final Button loginB = new Button(composite, SWT.NONE);
		loginB.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				login(event);
			}
		});
		final FormData formData_5_1 = new FormData();
		formData_5_1.bottom = new FormAttachment(registerB, 22, SWT.TOP);
		formData_5_1.top = new FormAttachment(registerB, 0, SWT.TOP);
		formData_5_1.right = new FormAttachment(0, 141);
		formData_5_1.left = new FormAttachment(0, 58);
		loginB.setLayoutData(formData_5_1);
		loginB.setText("登录");
		//
	}

	protected void login(TypedEvent event) {
		String status = null;
		if (!connected) {
			status = MainWin.client.connectServer(serverT.getText(), Integer
					.parseInt(portT.getText()));
			if (!status.equals("OK")) {
				serverT.setText("");
				MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
				mb.setMessage(status);
				mb.open();
				return;
			} else {
				connected = true;
			}
		}
		status = MainWin.client.login(Integer.parseInt(numberT.getText()), pwdT
				.getText());
		if (!status.equals("OK")) {
			MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
			mb.setMessage(status);
			mb.open();
			return;
		}

		loggedin = true;
		MainWin.loggedIn = true;
		shell.close();
	}

	protected void register(SelectionEvent se) {
//		 TODO Auto-generated method stub
		String status = null;
		if (!connected) {
			status = MainWin.client.connectServer(serverT.getText(), Integer
					.parseInt(portT.getText()));
			if (!status.equals("OK")) {
				serverT.setText("");
				MessageBox mb = new MessageBox(shell, SWT.ICON_ERROR | SWT.OK);
				mb.setMessage(status);
				mb.open();
				return;
			} else {
				connected = true;
			}
		}
		InforCardDialog  icd = new InforCardDialog(shell,InforCardDialog.REGISTER,true);
		icd.open();
	}

	protected void closeWin(DisposeEvent de) {
		if(!loggedin && connected){
			MainWin.client.logout();
		}
		shell.dispose();		
	}

	
	public static void main(String[] args){
		Shell shell = new Shell(new Display());
		LoginDailog login = new  LoginDailog(shell);
		if(login.open()){
			System.out.println("logged in");
			try {
				mainWin = new MainWin();
				mainWin.open();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}

⌨️ 快捷键说明

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