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

📄 addressbookui.java

📁 java 开发的电子通讯录
💻 JAVA
字号:
/**
 * 这个是用户主界面
 */
package edu.hqu.JYT.addressBook.ui;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
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.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

import edu.hqu.JYT.addressBook.util.DataTable;
import edu.hqu.JYT.addressBook.util.Robot;

/**
 * @author Jeffrey
 *
 */
public class AddressBookUI implements SelectionListener {
	private final Display display;
	private static Shell mainShell;

	private static Combo relationship;
	private Button ok;
	private static Table tableArea;
	private static DataTable table;

	public AddressBookUI() throws SQLException, IOException {
		display = new Display();
		mainShell = new Shell(display, SWT.MIN);
		mainShell.setText("MINI通讯录");
		mainShell.setSize(380, 500);
		new MenuBar(mainShell);
		mainShell.setLayout(new FormLayout());
		File file = new File("./first");
		boolean firstrun = true;
		if (file.exists()) {
			firstrun = false;
		} else {
			file.createNewFile();
		}
		table = new DataTable("user", "1234567", firstrun);
		if (!firstrun) {
			new Robot(mainShell).happyBirthday();
		}
		buildWorkspace();
	}

	private void buildWorkspace() {
		relationship = new Combo(mainShell, SWT.READ_ONLY);
		relationship.add("朋友");
		relationship.add("家人");
		relationship.add("亲戚");
		relationship.add("同事");
		relationship.add("同学");
		relationship.add("客户");
		relationship.add("其他1");
		relationship.add("其他2");
		relationship.add("其他3");
		FormData data = new FormData();
		data.top = new FormAttachment(0, 10);
		data.left = new FormAttachment(0, 10);
		relationship.setLayoutData(data);

		ok = new Button(mainShell, SWT.PUSH);
		ok.setText("按关系查找!");
		data = new FormData();
		data.top = new FormAttachment(0, 8);
		data.left = new FormAttachment(relationship, 10);
		ok.setLayoutData(data);
		ok.addSelectionListener(this);

		tableArea = new Table(mainShell, SWT.SINGLE | SWT.BORDER);
		tableArea.setFont(new Font(display, "", 20, SWT.NONE));
		data = new FormData();
		data.top = new FormAttachment(relationship, 10);
		data.left = new FormAttachment(0, 10);
		data.bottom = new FormAttachment(100, -10);
		data.right = new FormAttachment(100, -10);
		tableArea.setLayoutData(data);
		tableArea.addSelectionListener(this);
	}

	public void open() {
		mainShell.open();
		while (!mainShell.isDisposed()) {
			if (display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}

	public static int getRelationship() {
		return relationship.getSelectionIndex() + 1;
	}

	public static String getName() {
		if (tableArea.getSelectionCount() < 1) {
			return null;
		}
		return tableArea.getSelection()[0].getText();
	}

	public static void doSearch() throws SQLException {
		tableArea.removeAll();
		int relatinship = relationship.getSelectionIndex() + 1;
		if (relatinship < 1) {
			MessageBox box = new MessageBox(mainShell, SWT.ICON_ERROR);
			box.setText("出错信息");
			box.setMessage("请先选择要查找的关系类型");
			box.open();
			return;
		}
		ResultSet rs = table.searchByRelationships(relatinship);
		while (rs.next()) {
			String str = rs.getString(1);
			if (str == null) {
				return;
			} else {
				TableItem item = new TableItem(tableArea, SWT.NONE);
				item.setText(str);
			}
		}
	}

	public void widgetDefaultSelected(SelectionEvent evnt) {
		if (evnt.getSource().equals(tableArea)) {
			try {
				String name = tableArea.getSelection()[0].getText();
				UserInfoFrame frame = new UserInfoFrame(mainShell);
				frame.open();
				frame.getInfo(name);
			} catch (IOException exp) {

			} catch (SQLException exp) {

			}
		}
	}

	public void widgetSelected(SelectionEvent evnt) {
		if (evnt.getSource().equals(ok)) {
			try {
				doSearch();
			} catch (SQLException exp) {
				System.out.println(exp.getMessage());
				MessageBox box = new MessageBox(mainShell, SWT.ICON_ERROR);
				box.setText("出错信息");
				box.setMessage("读取信息出错.");
				box.open();
			}
		}
	}

	public static void main(String[] args) throws IOException, SQLException {
		new AddressBookUI().open();
	}
}

⌨️ 快捷键说明

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