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

📄 addframe.java

📁 java 开发的电子通讯录
💻 JAVA
字号:
/**
 * 这个程序有点BUG
 * 1.日期的每个月分都会有31号
 * 2.现在还不能判定输入TEXT的数据是否合格
 */
package edu.hqu.JYT.addressBook.ui;

import java.io.IOException;
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.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
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.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

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

public class AddFrame implements SelectionListener {
	private final Shell shell;
	private final Font font;
	private Text nameText, phoneNumberText, telephoneNumberText,
			officePhoneNumberText, emailAddressText, QQNumberText, addressText,
			postNumberText;

	private Label nameLabel, sexLabel, birthdayLabel, monthLabel, dateLabel,
			phoneNumberLabel, telephoneNumberLabel, officePhoneNumberLabel,
			emailAddressLabel, QQNumberLabel, addressLabel, postNumberLabel,
			relationshipLabel;

	private Combo relationshipCombo, monthCombo, dateCombo;

	private Button femaleButton, maleButton, ok, cancel, help;

	private final DataTable table;

	public AddFrame(Shell parent) throws SQLException, IOException {
		shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.CLOSE);
		shell.setText("添加记录");
		shell.setLayout(new FormLayout());
		shell.setSize(380, 500);
		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent evnt) {
				evnt.doit = false;
				shell.setVisible(false);
			}
		});
		font = new Font(shell.getDisplay(), "", 16, SWT.NONE);
		table = new DataTable("user", "1234567");
		buildFrame();
	}

	public void open() {
		shell.open();
	}

	/**
	 * 创建界面
	 */
	private void buildFrame() {
		nameLabel = new Label(shell, SWT.NONE); // 姓名
		nameLabel.setFont(font);
		nameLabel.setText("姓名:");
		FormData data = new FormData();
		data.top = new FormAttachment(0, 14);
		data.left = new FormAttachment(0, 10);
		nameLabel.setLayoutData(data);

		nameText = new Text(shell, SWT.BORDER);
		nameText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(0, 10);
		data.left = new FormAttachment(nameLabel, 10);
		data.right = new FormAttachment(100, -10);
		nameText.setLayoutData(data);

		sexLabel = new Label(shell, SWT.NONE);// 性别
		sexLabel.setFont(font);
		sexLabel.setText("性别:");
		data = new FormData();
		data.top = new FormAttachment(nameLabel, 12);
		data.left = new FormAttachment(0, 10);
		sexLabel.setLayoutData(data);

		femaleButton = new Button(shell, SWT.RADIO);
		femaleButton.setFont(font);
		femaleButton.setText("女");
		femaleButton.setSelection(true);
		data = new FormData();
		data.top = new FormAttachment(nameText, 10);
		data.left = new FormAttachment(sexLabel, 10);
		femaleButton.setLayoutData(data);

		maleButton = new Button(shell, SWT.RADIO);
		maleButton.setFont(font);
		maleButton.setText("男");
		data = new FormData();
		data.top = new FormAttachment(nameText, 10);
		data.left = new FormAttachment(femaleButton, 10);
		maleButton.setLayoutData(data);

		birthdayLabel = new Label(shell, SWT.NONE);
		birthdayLabel.setFont(font);
		birthdayLabel.setText("生日:");
		data = new FormData();
		data.left = new FormAttachment(0, 10);
		data.top = new FormAttachment(sexLabel, 10);
		birthdayLabel.setLayoutData(data);

		monthCombo = new Combo(shell, SWT.READ_ONLY);
		for (int i = 1; i <= 12; i++) {
			monthCombo.add(String.valueOf(i));
		}
		data = new FormData();
		data.top = new FormAttachment(femaleButton, 10);
		data.left = new FormAttachment(birthdayLabel, 10);
		monthCombo.setLayoutData(data);

		monthLabel = new Label(shell, SWT.NONE);
		monthLabel.setFont(font);
		monthLabel.setText("月");
		data = new FormData();
		data.top = new FormAttachment(maleButton, 10);
		data.left = new FormAttachment(monthCombo, 10);
		monthLabel.setLayoutData(data);

		dateCombo = new Combo(shell, SWT.READ_ONLY);
		for (int i = 1; i <= 31; i++) {
			dateCombo.add(String.valueOf(i));
		}
		data = new FormData();
		data.top = new FormAttachment(femaleButton, 10);
		data.left = new FormAttachment(monthLabel, 5);
		dateCombo.setLayoutData(data);

		dateLabel = new Label(shell, SWT.NONE);
		dateLabel.setFont(font);
		dateLabel.setText("日");
		data = new FormData();
		data.top = new FormAttachment(femaleButton, 10);
		data.left = new FormAttachment(dateCombo, 5);
		dateLabel.setLayoutData(data);

		phoneNumberLabel = new Label(shell, SWT.NONE);
		phoneNumberLabel.setFont(font);
		phoneNumberLabel.setText("座机号码:");
		data = new FormData();
		data.top = new FormAttachment(birthdayLabel, 10);
		data.left = new FormAttachment(0, 10);
		phoneNumberLabel.setLayoutData(data);

		phoneNumberText = new Text(shell, SWT.BORDER);
		phoneNumberText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(monthCombo, 10);
		data.left = new FormAttachment(phoneNumberLabel, 10);
		data.right = new FormAttachment(100, -10);
		phoneNumberText.setLayoutData(data);

		telephoneNumberLabel = new Label(shell, SWT.NONE);
		telephoneNumberLabel.setFont(font);
		telephoneNumberLabel.setText("手机号码:");
		data = new FormData();
		data.top = new FormAttachment(phoneNumberLabel, 15);
		data.left = new FormAttachment(0, 10);
		telephoneNumberLabel.setLayoutData(data);

		telephoneNumberText = new Text(shell, SWT.BORDER);
		telephoneNumberText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(phoneNumberText, 10);
		data.left = new FormAttachment(telephoneNumberLabel, 10);
		data.right = new FormAttachment(100, -10);
		telephoneNumberText.setLayoutData(data);

		officePhoneNumberLabel = new Label(shell, SWT.NONE);
		officePhoneNumberLabel.setFont(font);
		officePhoneNumberLabel.setText("办公室号码:");
		data = new FormData();
		data.top = new FormAttachment(telephoneNumberLabel, 17);
		data.left = new FormAttachment(0, 10);
		officePhoneNumberLabel.setLayoutData(data);

		officePhoneNumberText = new Text(shell, SWT.BORDER);
		officePhoneNumberText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(telephoneNumberText, 10);
		data.left = new FormAttachment(officePhoneNumberLabel, 10);
		data.right = new FormAttachment(100, -10);
		officePhoneNumberText.setLayoutData(data);

		emailAddressLabel = new Label(shell, SWT.NONE);
		emailAddressLabel.setFont(font);
		emailAddressLabel.setText("E-MAIL:");
		data = new FormData();
		data.top = new FormAttachment(officePhoneNumberLabel, 14);
		data.left = new FormAttachment(0, 10);
		emailAddressLabel.setLayoutData(data);

		emailAddressText = new Text(shell, SWT.BORDER);
		emailAddressText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(officePhoneNumberText, 10);
		data.left = new FormAttachment(emailAddressLabel, 10);
		data.right = new FormAttachment(100, -10);
		emailAddressText.setLayoutData(data);

		QQNumberLabel = new Label(shell, SWT.NONE);
		QQNumberLabel.setFont(font);
		QQNumberLabel.setText("QQ号码:");
		data = new FormData();
		data.top = new FormAttachment(emailAddressLabel, 18);
		data.left = new FormAttachment(0, 10);
		QQNumberLabel.setLayoutData(data);

		QQNumberText = new Text(shell, SWT.BORDER);
		QQNumberText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(emailAddressText, 10);
		data.left = new FormAttachment(QQNumberLabel, 10);
		data.right = new FormAttachment(100, -10);
		QQNumberText.setLayoutData(data);

		addressLabel = new Label(shell, SWT.NONE);
		addressLabel.setFont(font);
		addressLabel.setText("地址:");
		data = new FormData();
		data.top = new FormAttachment(QQNumberLabel, 14);
		data.left = new FormAttachment(0, 10);
		addressLabel.setLayoutData(data);

		addressText = new Text(shell, SWT.BORDER);
		addressText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(QQNumberText, 10);
		data.left = new FormAttachment(addressLabel, 10);
		data.right = new FormAttachment(100, -10);
		addressText.setLayoutData(data);

		postNumberLabel = new Label(shell, SWT.NONE);
		postNumberLabel.setFont(font);
		postNumberLabel.setText("邮编:");
		data = new FormData();
		data.top = new FormAttachment(addressLabel, 16);
		data.left = new FormAttachment(0, 10);
		postNumberLabel.setLayoutData(data);

		postNumberText = new Text(shell, SWT.BORDER);
		postNumberText.setFont(font);
		data = new FormData();
		data.top = new FormAttachment(addressText, 10);
		data.left = new FormAttachment(postNumberLabel, 10);
		data.right = new FormAttachment(100, -10);
		postNumberText.setLayoutData(data);

		relationshipLabel = new Label(shell, SWT.NONE);
		relationshipLabel.setFont(font);
		relationshipLabel.setText("与你的关系:");
		data = new FormData();
		data.top = new FormAttachment(postNumberLabel, 20);
		data.left = new FormAttachment(0, 10);
		relationshipLabel.setLayoutData(data);

		relationshipCombo = new Combo(shell, SWT.SINGLE);
		relationshipCombo.add("朋友");
		relationshipCombo.add("家人");
		relationshipCombo.add("亲戚");
		relationshipCombo.add("同事");
		relationshipCombo.add("同学");
		relationshipCombo.add("客户");
		relationshipCombo.add("其他1");
		relationshipCombo.add("其他2");
		relationshipCombo.add("其他3");
		data = new FormData();
		data.top = new FormAttachment(postNumberText, 14);
		data.left = new FormAttachment(relationshipLabel, 10);
		relationshipCombo.select(0);
		relationshipCombo.setLayoutData(data);

		ok = new Button(shell, SWT.PUSH);
		ok.setFont(font);
		ok.setText("确定");
		data = new FormData();
		data.top = new FormAttachment(relationshipLabel, 10);
		data.left = new FormAttachment(10, 30);
		ok.setLayoutData(data);
		ok.addSelectionListener(this);

		cancel = new Button(shell, SWT.PUSH);
		cancel.setFont(font);
		cancel.setText("取消");
		data = new FormData();
		data.top = new FormAttachment(relationshipLabel, 10);
		data.left = new FormAttachment(ok, 30);
		cancel.setLayoutData(data);
		cancel.addSelectionListener(this);

		help = new Button(shell, SWT.PUSH);
		help.setFont(font);
		help.setText("帮助");
		data = new FormData();
		data.top = new FormAttachment(relationshipCombo, 10);
		data.left = new FormAttachment(cancel, 30);
		help.setLayoutData(data);
		help.addSelectionListener(this);
	}

	private void addInfo() throws SQLException {
		String name = nameText.getText();
		boolean isFemale = femaleButton.getSelection();
		int month = monthCombo.getSelectionIndex() + 1;
		int date = dateCombo.getSelectionIndex() + 1;
		String phoneNumber = phoneNumberText.getText();
		String telephoneNumber = telephoneNumberText.getText();
		String officePhoneNumber = officePhoneNumberText.getText();
		String emailAddress = emailAddressText.getText();
		String QQNumber = QQNumberText.getText();
		String address = addressText.getText();
		String postNumber = postNumberText.getText();
		int relationship = relationshipCombo.getSelectionIndex() + 1;

		if (name.equals("") || month < 1 || date < 1) {
			MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
			box.setText("出错信息");
			box.setMessage("请准确输入信息.");
			box.open();
			return;
		}
		table.addInfo(name, relationship);
		table.addInfo(name, isFemale, month, date, telephoneNumber,
				phoneNumber, officePhoneNumber, QQNumber, emailAddress,
				address, postNumber);
	}

	public void widgetSelected(SelectionEvent evnt) {
		Object obj = evnt.getSource();
		if (obj.equals(ok)) {
			try {
				addInfo();
			} catch (SQLException exp) {
				MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
				box.setText("出错信息");
				box.setMessage("添加信息出错.");
				box.open();
			}
			shell.close();
		} else if (obj.equals(cancel)) {
			shell.close();
		} else if (obj.equals(help)) {
			MessageBox box = new MessageBox(shell, SWT.ICON_INFORMATION);
			box.setText("帮助信息");
			box.setMessage("输入相应的信息,点确定即可以添加信息了");
			box.open();
		}
	}

	public void widgetDefaultSelected(SelectionEvent evnt) {
	}

}

⌨️ 快捷键说明

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