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

📄 communication.java

📁 学生信息管理能够查询 修改 编辑 进行各种查询成绩
💻 JAVA
字号:
/*
 * Created on 2008-2-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package no6;

/**
 * @author lenovo
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
/**毛文平完成于2008-2-29 00:25
 * Communication.java
 * 本程序设计了一个通讯录,实现了添加个人信息,以及查询,清空通讯录,退出功能!!
 * serializable类的运用,其就是一个存和取的接口
 */
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class Communication extends JApplet {

	JLabel jl1, jl2, jl3, jl4, jl5, jl6, jl7;

	JButton button1, button2, button3, button4;

	JTextField tx1, tx2, tx3, tx4, tx5, tx6;

	ArrayList v = new ArrayList();

	public Communication() {
		//创建各个标签
		jl1 = new JLabel("communication");
		jl2 = new JLabel("姓名");
		jl3 = new JLabel("邮政编码");
		jl4 = new JLabel("通信地址");
		jl5 = new JLabel("电话");
		jl6 = new JLabel("手机");
		jl7 = new JLabel("email");
		//创建按钮
		button1 = new JButton("添加");
		button2 = new JButton("查找");
		button3 = new JButton("清空");
		button4 = new JButton("退出");
		//创建文档区
		tx1 = new JTextField();
		tx2 = new JTextField();
		tx3 = new JTextField();
		tx4 = new JTextField();
		tx5 = new JTextField();
		tx6 = new JTextField();
	}

	public void init() {
		//清空面板的默认布局BorderLayout;
		Container cp = getContentPane();
		cp.setLayout(null);
		//创建标签communication的画笔
		Font font1 = new Font("Georgia", Font.ITALIC, 30);//斜体,大小为30
		jl1.setBounds(110, 40, 240, 30);//精确确定标签communication的位置
		jl1.setFont(font1);//改变标签字体
		Font font2 = new Font("Black", Font.BOLD, 12);//黑体,大小为12
		jl7.setFont(font2);
		Font font = new Font("Black", Font.BOLD, 10);
		jl2.setBounds(50, 105, 40, 10);
		jl3.setBounds(50, 155, 60, 10);
		jl4.setBounds(50, 205, 60, 10);
		jl5.setBounds(50, 255, 40, 20);
		jl6.setBounds(50, 305, 40, 10);
		jl7.setBounds(50, 354, 50, 10);
		button1.setBounds(50, 400, 60, 20);
		button2.setBounds(130, 400, 60, 20);
		button3.setBounds(210, 400, 60, 20);
		button4.setBounds(290, 400, 60, 20);
		tx1.setBounds(130, 100, 100, 20);
		tx2.setBounds(130, 150, 100, 20);
		tx3.setBounds(130, 200, 200, 20);
		tx4.setBounds(130, 250, 100, 20);
		tx5.setBounds(130, 300, 100, 20);
		tx6.setBounds(130, 350, 200, 20);

		jl2.setFont(font);
		jl3.setFont(font);
		jl4.setFont(font);
		jl5.setFont(font);
		jl6.setFont(font);
		button1.setFont(font);
		button2.setFont(font);
		button3.setFont(font);
		button4.setFont(font);
		//将各个组件加入到面板中
		cp.add(jl1);
		cp.add(jl2);
		cp.add(jl3);
		cp.add(jl4);
		cp.add(jl5);
		cp.add(jl6);
		cp.add(jl7);
		cp.add(button1);
		cp.add(button2);
		cp.add(button3);
		cp.add(button4);
		cp.add(tx1);
		cp.add(tx2);
		cp.add(tx3);
		cp.add(tx4);
		cp.add(tx5);
		cp.add(tx6);
		button1.addActionListener(new ButtonListener1());
		button2.addActionListener(new ButtonListener2());
		button3.addActionListener(new ButtonListener3());
		button4.addActionListener(new ButtonListener4());
	}

	//添加这个按钮的动作事件
	public class ButtonListener1 implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			//无法添加名字为空的记录!!
			if (tx1.getText().equalsIgnoreCase("")) {
				JOptionPane.showMessageDialog(null, "无法添加名字为空的记录", "Message",
						JOptionPane.INFORMATION_MESSAGE);
				//通讯录簿清零!
				tx2.setText("");
				tx3.setText("");
				tx4.setText("");
				tx5.setText("");
				tx6.setText("");
			}
			Note note = new Note();
			note.name = tx1.getText();//将TextField上的值传入到类Note
			note.zip = tx2.getText();
			note.address = tx3.getText();
			note.telephone = tx4.getText();
			note.mobile = tx5.getText();
			note.email = tx6.getText();
			try {
				ObjectInputStream in = new ObjectInputStream(
						new FileInputStream("note.dat"));
				//v.clear();
				//while(in.available()>0)
				v = (ArrayList) in.readObject();//读入链表
				in.close();//文件流关闭
			} catch (Exception e1) {
				System.out.println("error1");
			}
			try {
				ObjectOutputStream out = new ObjectOutputStream(
						new FileOutputStream("note.dat"));
				Note temp = new Note();//创建一个Note类来进行下一步比较
				int i;
				for (i = 0; i < v.size(); i++) {
					temp = (Note) v.get(i);//读取链表值,及某个人的通讯单
					if (temp.name.equalsIgnoreCase(tx1.getText()))//和查找TextField上输入的name值比较
						break;
				}
				//判断当i的值没有为链表的大小时,及上面的for循环中的if语句,一直就没有运行!!
				//这判断链表中存在这个人的通讯单。
				if (v.size() != 0 && i != v.size()) {
					JOptionPane.showMessageDialog(null, "已经存在此记录", "Message",
							JOptionPane.INFORMATION_MESSAGE);
					tx1.setText("");
					tx2.setText("");
					tx3.setText("");
					tx4.setText("");
					tx5.setText("");
					tx6.setText("");

				} else {
					//既然不存再这个人的通讯单,则为新的通讯单,所以加入到链表中去,而后链表再写入到文件中去!!
					v.add(note);
					out.writeObject(v);
				}
				out.close();
			} catch (Exception e2) {
				System.out.println("error2");
			}
			tx1.setText("");
			tx2.setText("");
			tx3.setText("");
			tx4.setText("");
			tx5.setText("");
			tx6.setText("");
		}
	}

	//寻找按钮的动作事件
	public class ButtonListener2 implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			JTextField tx = new JTextField(15);
			//创建一个消息对话框,而且可以输入信息!!
			String val = JOptionPane.showInputDialog("Input messages");
			tx.setText(val);
			//try {

			//ObjectInputStream in=new ObjectInputStream(
			//	new FileInputStream("note.dat"));
			//while(in.available()>0)
			//	v=add((ArrayList)in.readObject());
			//in.close();
			///} catch (Exception e3) {
			///	System.out.println("error3");//出错的地方
			//}
			//判断输入的信息是否为空
			if (tx.getText().equalsIgnoreCase(""))
				JOptionPane.showMessageDialog(null, "查找不能为空", "Message",
						JOptionPane.INFORMATION_MESSAGE);
			else {
				Note temp1 = new Note();
				int i;
				for (i = 0; i < v.size(); i++) {
					//本程序是将name作为索引的
					temp1 = (Note) v.get(i);
					if (temp1.name.equalsIgnoreCase(tx.getText()))
						break;
				}
				if (i != v.size() && v.size() != 0) {
					try {
						ObjectOutputStream out = new ObjectOutputStream(
								new FileOutputStream("find.dat"));
						out.writeObject(temp1);
						out.close();
						//输出!!
						System.out.println("姓名:" + temp1.name + "\n" + "邮政编码:"
								+ temp1.zip + "\n" + "通信地址:" + temp1.address
								+ "\n" + "电话:" + temp1.telephone + "\n" + "手机:"
								+ temp1.mobile + "\n" + "email:" + temp1.email);
						//tx1.setText(temp1.name);
						//tx2.setText(temp1.zip);
						//tx3.setText(temp1.address);
						//tx4.setText(temp1.telephone);
						//tx5.setText(temp1.mobile);
						//tx6.setText(temp1.email);
					} catch (Exception e4) {
						System.out.println("error4");
					}
				} else {
					JOptionPane.showMessageDialog(null, "没有找到!", "Message",
							JOptionPane.INFORMATION_MESSAGE);
					tx1.setText("");
					tx2.setText("");
					tx3.setText("");
					tx4.setText("");
					tx5.setText("");
				}

			}
		}
	}

	//清除按钮的动作事件
	public class ButtonListener3 implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				//ObjectInputStream in = new ObjectInputStream(
				//new FileInputStream("note.dat"));
				//v = (Vector) in.readObject();
				//while(in.available()>0)
				//v.add((ArrayList)in.readObject());
				//in.close();
				v.clear();
				ObjectOutputStream out = new ObjectOutputStream(
						new FileOutputStream("note.dat"));
				out.writeObject(v);
				out.close();
			} catch (Exception e5) {
				System.out.println("error5");//出错的地方!!!
			}
			tx1.setText("");
			tx2.setText("");
			tx3.setText("");
			tx4.setText("");
			tx5.setText("");
		}
	}

	//退出按钮的动作事件
	public class ButtonListener4 implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}

	public class Note implements Serializable {
		public String name;

		public String zip;

		public String address;

		public String telephone;

		public String mobile;

		public String email;
	}

	//框架的大小,名字,显现以及窗口的关闭
	public static void main(String[] args) {
		JFrame jf = new JFrame();
		Communication ct = new Communication();
		jf.getContentPane().add(ct);
		jf.setTitle("通讯录");
		jf.setSize(400, 500);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ct.start();
		ct.init();
		jf.setVisible(true);
	}
}

⌨️ 快捷键说明

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