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

📄 infobook.java

📁 小小通讯录
💻 JAVA
字号:
import java.util.*;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.*;
import java.awt.Color;
import javax.swing.Box;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.*;
import java.awt.Font;

class InfoEntry implements Serializable{
    public static int totalNumber;
    String strName;
    String strZip;
    String strAddress;
    String strPhone;
    String strMobile;
    String strEmail;
    InfoEntry(String name,String zip,String address,
              String phone,String mobile,String email){
        strName=name;
        strZip=zip;
        strAddress=address;
        strPhone=phone;
        strMobile=mobile;
        strEmail=email;
        totalNumber++;
    }

}

class Entries extends Vector{
    public InfoEntry contains(InfoEntry entry){
        String temp1;
        for(int i=0;i<size();i++){
            temp1=((InfoEntry)elementAt(i)).strName;
           // JOptionPane.showMessageDialog(null,temp1, "OK!",JOptionPane.DEFAULT_OPTION);
            if (temp1.equals(entry.strName)) {
                return entry;
            }
        }
        return null;
    }

}

public class InfoBook extends JFrame {
    //private Entries entries = new Entries();
    private Vector entries=new Vector();

    JPanel contentPane;
    JLabel lbName = new JLabel();
    JLabel lbZip = new JLabel();
    JLabel lbAddress = new JLabel();
    JLabel lbPhone = new JLabel();
    JLabel lbMobile = new JLabel();
    JLabel jbEmail = new JLabel();
    JPanel jpMiddle = new JPanel();
    JTextField tfName = new JTextField();
    GridLayout gridLayout1 = new GridLayout();
    BorderLayout borderLayout1 = new BorderLayout();
    JTextField tfPhone = new JTextField();
    JTextField tfAddress = new JTextField();
    JTextField tfMobile = new JTextField();
    JTextField tfZip = new JTextField();
    JTextField tfMail = new JTextField();
    JPanel jpBottom = new JPanel();
    JPanel jpTop = new JPanel();
    JButton btnAdd = new JButton();
    JButton btnSearch = new JButton();
    JButton btnDel = new JButton();
    JButton btnClear = new JButton();
    JLabel lbTittle = new JLabel();
    JPanel jpRight = new JPanel();
    JPanel jpLeft = new JPanel();
    public InfoBook() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        this.setResizable(false);
        setSize(new Dimension(400, 300));
        setTitle("通讯录");
        this.setVisible(true);
        lbName.setBorder(BorderFactory.createEtchedBorder());
        lbName.setHorizontalTextPosition(SwingConstants.CENTER);
        lbName.setText("姓名");
        lbZip.setBorder(BorderFactory.createEtchedBorder());
        lbZip.setText("邮政编码");
        lbAddress.setBorder(BorderFactory.createEtchedBorder());
        lbAddress.setText("通信地址");
        lbPhone.setBorder(BorderFactory.createEtchedBorder());
        lbPhone.setText("电话");
        lbMobile.setBorder(BorderFactory.createEtchedBorder());
        lbMobile.setText("手机");
        jbEmail.setBorder(BorderFactory.createEtchedBorder());
        jbEmail.setText("Email");
        jpMiddle.setLayout(gridLayout1);
        jpMiddle.setBorder(BorderFactory.createLineBorder(Color.black));
        gridLayout1.setColumns(5);
        gridLayout1.setRows(6);
        gridLayout1.setVgap(5);
        contentPane.setBorder(BorderFactory.createLineBorder(Color.black));
        btnAdd.setText("添加");
        btnAdd.addActionListener(new InfoBook_btnAdd_actionAdapter(this));
        btnSearch.setText("查找");
        btnSearch.addActionListener(new InfoBook_btnSearch_actionAdapter(this));
        btnDel.setText("删除");
        btnDel.addActionListener(new InfoBook_btnDel_actionAdapter(this));
        btnClear.setText("清空");
        btnClear.addActionListener(new InfoBook_btnClear_actionAdapter(this));
        borderLayout1.setHgap(5);
        borderLayout1.setVgap(10);
        lbTittle.setFont(new java.awt.Font("宋体", Font.ITALIC, 20));
        lbTittle.setForeground(Color.blue);
        lbTittle.setText("个人通讯录");
        jpTop.setBorder(null);
        jpBottom.setBorder(null);
        jpMiddle.add(lbName, null);
        jpMiddle.add(tfName, null);
        jpMiddle.add(lbZip, null);
        jpMiddle.add(tfZip);
        jpMiddle.add(lbAddress, null);
        jpMiddle.add(tfAddress);
        jpMiddle.add(lbPhone, null);
        jpMiddle.add(tfPhone);
        jpMiddle.add(lbMobile);
        jpMiddle.add(tfMobile);
        jpMiddle.add(jbEmail, null);
        jpMiddle.add(tfMail);
        jpBottom.add(btnAdd);
        jpBottom.add(btnSearch);
        jpBottom.add(btnDel);
        jpBottom.add(btnClear);
        // this.getContentPane().add(contentPane, java.awt.BorderLayout.CENTER);
        jpTop.add(lbTittle);
        contentPane.add(jpMiddle, java.awt.BorderLayout.CENTER);
        contentPane.add(jpBottom, java.awt.BorderLayout.SOUTH);
        contentPane.add(jpRight, java.awt.BorderLayout.EAST);
        contentPane.add(jpLeft, java.awt.BorderLayout.WEST);
        contentPane.add(jpTop, java.awt.BorderLayout.NORTH);

        try{
      	 ObjectInputStream in =
		      new ObjectInputStream(
		        new FileInputStream("Info.dat"));
		    InfoEntry entry;
		    String s = (String)in.readObject();
		    System.out.println(s);
		   	while(!s.equals("-1")){
		 	   	s = (String)in.readObject();
		 	   	System.out.println(s);
		 	   	if(s.equals("-1"))return;
		 	   	entry = (InfoEntry)in.readObject();
		  	    entries.add(entry);
    			System.out.println(entry.strName);
			}
		}
		catch(IOException ex){}

    }

    private void save(){
		try{
             ObjectOutputStream out =
                  new ObjectOutputStream(
                    new FileOutputStream("Info.dat"));
             out.writeObject("个人备忘录");
             for(int i=entries.size()-1;i>=0;i--){
				 out.writeObject(Integer.toString(i));
                 out.writeObject(entries.elementAt(i));
             }
             out.writeObject("-1");
              out.close(); // Also flushes output
            }catch(IOException ex){}
	}

    private int findInList(String target){
        InfoEntry temp;
        for(int i=0;i<entries.size();i++){
            temp=(InfoEntry)(entries.elementAt(i));
            if(temp.strName.equals(target)){
                return i;
            }
        }
        return -1;
    }
    public void btnAdd_actionPerformed(ActionEvent e) {
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(
                    null,"姓名不能为空!", "错误",JOptionPane.ERROR_MESSAGE);
            return;
        }

        InfoEntry temp=new InfoEntry(tfName.getText(),tfZip.getText(),tfAddress.getText(),tfPhone.getText(),
                     tfMobile.getText(),tfMail.getText());
        //if(entries.contains(temp)==null){
        int index=findInList(tfName.getText());
        if(index==-1){
            entries.addElement((InfoEntry)temp);
            JOptionPane.showMessageDialog(
                   null,"成功添加"+tfName.getText(), "OK!",JOptionPane.DEFAULT_OPTION);
            tfName.setText("");
            tfZip.setText("");
            tfAddress.setText("");
            tfPhone.setText("");
            tfMobile.setText("");
            tfMail.setText("");

			save();

        }
        else{
            JOptionPane.showMessageDialog(
                   null,"已经含有此人", "错误",JOptionPane.DEFAULT_OPTION);

            //tfName.setText("dfj;asdfj;idsjf;");
        }
        //tfName.setText(Integer.toString(InfoEntry.totalNumber));
    }

    public void btnSearch_actionPerformed(ActionEvent e) {
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(
                    null,"姓名不能为空!", "错误",JOptionPane.ERROR_MESSAGE);
            return;
        }

       // InfoEntry temp=new InfoEntry(tfName.getText(),tfZip.getText(),tfAddress.getText(),tfPhone.getText(),
        //             tfMobile.getText(),tfMail.getText());
        InfoEntry temp=new InfoEntry(tfName.getText(),"","","","","");
        InfoEntry info;//=entries.contains(temp);
        int index=findInList(tfName.getText());
        if(index != -1){
            info=(InfoEntry)(entries.elementAt(index));
       // if(info!=null){
            tfName.setText(info.strName);
            tfZip.setText(info.strZip);
            tfAddress.setText(info.strAddress);
            tfPhone.setText(info.strPhone);
            tfMobile.setText(info.strMobile);
            tfMail.setText(info.strEmail);
        }
        else{
            JOptionPane.showMessageDialog(
                    null,"不存在"+tfName.getText(), "错误",JOptionPane.DEFAULT_OPTION);
        }

    }

    public void btnDel_actionPerformed(ActionEvent e) {
        if(tfName.getText().equals("")){
            JOptionPane.showMessageDialog(
                    null,"姓名不能为空!", "错误",JOptionPane.ERROR_MESSAGE);
            return;
        }

        int index=findInList(tfName.getText());
        if(index != -1){
            entries.remove(index);
            JOptionPane.showMessageDialog(
                    null,"成功删除此项", "OK!",JOptionPane.DEFAULT_OPTION);
            tfName.setText("");
            tfZip.setText("");
            tfAddress.setText("");
            tfPhone.setText("");
            tfMobile.setText("");
            tfMail.setText("");
        }
        else{
            JOptionPane.showMessageDialog(
                    null,"不存在"+tfName.getText(), "OK!",JOptionPane.DEFAULT_OPTION);
        }
        save();
        //tfName.setText(Integer.toString(InfoEntry.totalNumber));
    }

    public void btnClear_actionPerformed(ActionEvent e) {
        if(!tfName.getText().equals("0")
           || !tfName.getText().equals("0")
           || !tfName.getText().equals("0")
           || !tfName.getText().equals("0")
           || !tfName.getText().equals("0")
           || !tfName.getText().equals("0")     ){
            JOptionPane.showMessageDialog(
                    null,"6项全部填写为0才能执行清空操作", "错误",JOptionPane.ERROR_MESSAGE);
            return;
        }
        else{
            for(int i=0;i<entries.size();i++){
                entries.remove(i);
            }
            JOptionPane.showMessageDialog(
                    null,"成功清空通讯录", "OK!",JOptionPane.DEFAULT_OPTION);
            tfName.setText("");
            tfZip.setText("");
            tfAddress.setText("");
            tfPhone.setText("");
            tfMobile.setText("");
            tfMail.setText("");
        }
        save();

    }

     public static void main(String agrs[]){
				new InfoBook();
	}

}


class InfoBook_btnClear_actionAdapter implements ActionListener {
    private InfoBook adaptee;
    InfoBook_btnClear_actionAdapter(InfoBook adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnClear_actionPerformed(e);
    }
}


class InfoBook_btnDel_actionAdapter implements ActionListener {
    private InfoBook adaptee;
    InfoBook_btnDel_actionAdapter(InfoBook adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnDel_actionPerformed(e);
    }
}


class InfoBook_btnSearch_actionAdapter implements ActionListener {
    private InfoBook adaptee;
    InfoBook_btnSearch_actionAdapter(InfoBook adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnSearch_actionPerformed(e);
    }
}


class InfoBook_btnAdd_actionAdapter implements ActionListener {
    private InfoBook adaptee;
    InfoBook_btnAdd_actionAdapter(InfoBook adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.btnAdd_actionPerformed(e);
    }
}

⌨️ 快捷键说明

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