?? communication.java
字號(hào):
/*
* 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
* 本程序設(shè)計(jì)了一個(gè)通訊錄,實(shí)現(xiàn)了添加個(gè)人信息,以及查詢,清空通訊錄,退出功能!!
* serializable類的運(yùn)用,其就是一個(gè)存和取的接口
*/
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() {
//創(chuàng)建各個(gè)標(biāo)簽
jl1 = new JLabel("communication");
jl2 = new JLabel("姓名");
jl3 = new JLabel("郵政編碼");
jl4 = new JLabel("通信地址");
jl5 = new JLabel("電話");
jl6 = new JLabel("手機(jī)");
jl7 = new JLabel("email");
//創(chuàng)建按鈕
button1 = new JButton("添加");
button2 = new JButton("查找");
button3 = new JButton("清空");
button4 = new JButton("退出");
//創(chuàng)建文檔區(qū)
tx1 = new JTextField();
tx2 = new JTextField();
tx3 = new JTextField();
tx4 = new JTextField();
tx5 = new JTextField();
tx6 = new JTextField();
}
public void init() {
//清空面板的默認(rèn)布局BorderLayout;
Container cp = getContentPane();
cp.setLayout(null);
//創(chuàng)建標(biāo)簽communication的畫筆
Font font1 = new Font("Georgia", Font.ITALIC, 30);//斜體,大小為30
jl1.setBounds(110, 40, 240, 30);//精確確定標(biāo)簽communication的位置
jl1.setFont(font1);//改變標(biāo)簽字體
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);
//將各個(gè)組件加入到面板中
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());
}
//添加這個(gè)按鈕的動(dòng)作事件
public class ButtonListener1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
//無(wú)法添加名字為空的記錄!!
if (tx1.getText().equalsIgnoreCase("")) {
JOptionPane.showMessageDialog(null, "無(wú)法添加名字為空的記錄", "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();//文件流關(guān)閉
} catch (Exception e1) {
System.out.println("error1");
}
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("note.dat"));
Note temp = new Note();//創(chuàng)建一個(gè)Note類來(lái)進(jìn)行下一步比較
int i;
for (i = 0; i < v.size(); i++) {
temp = (Note) v.get(i);//讀取鏈表值,及某個(gè)人的通訊單
if (temp.name.equalsIgnoreCase(tx1.getText()))//和查找TextField上輸入的name值比較
break;
}
//判斷當(dāng)i的值沒(méi)有為鏈表的大小時(shí),及上面的for循環(huán)中的if語(yǔ)句,一直就沒(méi)有運(yùn)行!!
//這判斷鏈表中存在這個(gè)人的通訊單。
if (v.size() != 0 && i != v.size()) {
JOptionPane.showMessageDialog(null, "已經(jīng)存在此記錄", "Message",
JOptionPane.INFORMATION_MESSAGE);
tx1.setText("");
tx2.setText("");
tx3.setText("");
tx4.setText("");
tx5.setText("");
tx6.setText("");
} else {
//既然不存再這個(gè)人的通訊單,則為新的通訊單,所以加入到鏈表中去,而后鏈表再寫入到文件中去!!
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("");
}
}
//尋找按鈕的動(dòng)作事件
public class ButtonListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
JTextField tx = new JTextField(15);
//創(chuàng)建一個(gè)消息對(duì)話框,而且可以輸入信息!!
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");//出錯(cuò)的地方
//}
//判斷輸入的信息是否為空
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" + "手機(jī):"
+ 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, "沒(méi)有找到!", "Message",
JOptionPane.INFORMATION_MESSAGE);
tx1.setText("");
tx2.setText("");
tx3.setText("");
tx4.setText("");
tx5.setText("");
}
}
}
}
//清除按鈕的動(dòng)作事件
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");//出錯(cuò)的地方!!!
}
tx1.setText("");
tx2.setText("");
tx3.setText("");
tx4.setText("");
tx5.setText("");
}
}
//退出按鈕的動(dòng)作事件
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;
}
//框架的大小,名字,顯現(xiàn)以及窗口的關(guān)閉
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);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -