java通讯录

1.实现效果


java通讯录_第1张图片

java通讯录_第2张图片

2.首先写一个文件读写类

package tongxunlu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileRW {
     

	//进行文件读写操作
	private static FileWriter fileWriter;
	private static FileReader fileReader;
    
    //字符读写缓冲流
    private static BufferedReader bf;
    private static BufferedWriter bw;
    
    private static File file = new File("message\\test.txt");
    
    //文件写操作
    public static void fileWrite(String s) {
     
    	try {
     
			fileWriter = new FileWriter(file, true);
			bw = new BufferedWriter(fileWriter);
			bw.write(s);
		} catch (IOException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
     
			try {
     
				bw.close();
			} catch (IOException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    }
    
    //文件读操作
    public static String fileRead(String k) {
     
    	try {
     
			fileReader = new FileReader(file);
			bf = new BufferedReader(fileReader);
			String ss;
			while ( (ss = bf.readLine() )!= null ) {
     
				String[] temp = ss.split(",");
				if(temp[0].equals(k)) {
     
					return ss;
				}
			}
		} catch (FileNotFoundException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
     
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
     
			try {
     
				bf.close();
				fileReader.close();
			} catch (IOException e) {
     
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
    	return null;
    }
}

3.实现界面以及界面和文件交互

package tongxunlu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Main extends JFrame {
     

	String[] texts = new String[5];
	JPanel jp = new JPanel(); 
	JLabel message = new JLabel("个人通讯录", JLabel.CENTER);
	private JTextField jt1 = new JTextField("", 15);
	private JLabel jl1 = new JLabel("姓名");
	private JLabel jl2 = new JLabel("邮政编码");
	private JTextField jt2 = new JTextField("", 15);
	private JLabel jl3 = new JLabel("通信地址");
	private JTextField jt3 = new JTextField("", 45);
	private JLabel jl4 = new JLabel("手机");
	private JTextField jt4 = new JTextField("", 30);
	private JLabel jl5 = new JLabel("email");
	private JTextField jt5 = new JTextField("", 45);
	private JButton add = new JButton("添加");
	private JButton find = new JButton("查找");
	private JButton clear = new JButton("清空");
	private JButton quit = new JButton("退出");
	
	public Main() {
     
		super("通讯录");
		setLayout(null);
		setLocation(400, 200);
		setSize(600, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		setResizable(false);
		message.setForeground(Color.BLUE);
		message.setFont(new Font("黑体",1,30));
		message.setBounds(200, 20, 200, 30);
		this.add(message);
		jl1.setBounds(90, 56, 100, 30);
		jt1.setBounds(150, 56, 100, 30);
		this.add(jl1);
		this.add(jt1);
		jl2.setBounds(90, 106, 100, 30);
		jt2.setBounds(150,106, 200, 30);
		this.add(jl2);
		this.add(jt2);
		jl3.setBounds(90, 156, 100, 30);
		jt3.setBounds(150, 156, 300, 30);
		this.add(jl3);
		this.add(jt3);
		jl4.setBounds(90, 206, 100, 30);
		jt4.setBounds(150, 206, 200, 30);
		this.add(jl4);
		this.add(jt4);
		jl5.setBounds(90, 256, 100, 30);
		jt5.setBounds(150, 256, 300, 30);
		this.add(jl5);
		this.add(jt5);
		add.setBounds(60, 356, 90, 30);
		this.add(add);
		find.setBounds(180, 356, 90, 30);
		this.add(find);
		clear.setBounds(300, 356, 90, 30);
		this.add(clear);
		quit.setBounds(420, 356, 90, 30);
		this.add(quit);
		
		ActionMonitor monitor = new ActionMonitor();
		add.addActionListener(monitor);
		find.addActionListener(monitor);
		clear.addActionListener(monitor);
		quit.addActionListener(monitor);
	}
	
	class ActionMonitor implements ActionListener{
     
		@Override
		public void actionPerformed(ActionEvent e) {
     
			// TODO Auto-generated method stub
			boolean flag = true;
	        StringBuilder s = new StringBuilder();

			if(e.getSource() == add) {
     
				for(int i = 0; i < 5; i++) {
     
					texts[i] = new String();
				}
				texts[0] = jt1.getText();
				texts[1] = jt2.getText();
				texts[2] = jt3.getText();
				texts[3] = jt4.getText();
				texts[4] = jt5.getText();
				for(int i = 0; i < 5; i++) {
     
					if(texts[i].equals("") || texts[i] == null) {
     
						flag = false;
						break;
					}
					if(i == 0) {
     
						s.append(texts[i]);
					}
					else {
     
						s.append(",").append(texts[i]);
					}
				}
				if(flag) {
     
					s.append("\n");
					String ss = s.toString();
					FileRW.fileWrite(ss);
					jt1.setText("");
					jt2.setText("");
					jt3.setText("");
					jt4.setText("");
					jt5.setText("");
					JOptionPane.showMessageDialog(Main.this, "添加联系人成功"); 
				}
				else {
     
					JOptionPane.showMessageDialog(Main.this, "添加失败,请把信息填满"); 
				}
			}
			else if(e.getSource() == find) {
     
				JFrame frame = new JFrame("查询联系人");
				JPanel jPanel = new JPanel();
				JPanel jPanel1 = new JPanel();
				JLabel jLabel = new JLabel("输入查找人的姓名");
				JButton jButton = new JButton("确定");
				JTextField jt = new JTextField("", 15);
				jPanel.add(jLabel);
				jPanel.add(jt);
				jButton.addActionListener(new ActionListener() {
     
					@Override
					public void actionPerformed(ActionEvent e2) {
     
						// TODO Auto-generated method stub
						String text = jt.getText();
						String result = FileRW.fileRead(text);
						if( result == null ) {
     
							JOptionPane.showMessageDialog(Main.this, "查找失败,没有找到此人"); 
						}
						else {
     
							String[] tempresult = result.split(",");
							JOptionPane.showMessageDialog(Main.this, "姓名:" + tempresult[0] + "\n邮政编码:"+ tempresult[1] + "\n通信地址:" + tempresult[2]
									+ "\n手机" + tempresult[3] + "\nemail:" + tempresult[4]); 
						}
							
					}
				});
					jPanel1.add(jButton);
					frame.add(jPanel, BorderLayout.CENTER);
					frame.add(jPanel1, BorderLayout.SOUTH);
					frame.setBounds(500, 300, 400, 300);
					frame.setVisible(true);
			}
			else if(e.getSource() == clear) {
     
				jt1.setText("");
				jt2.setText("");
				jt3.setText("");
				jt4.setText("");
				jt5.setText("");
			}
			else if(e.getSource() == quit) {
     
				System.exit(0);
			}
		}
		
	}
	
	public static void main(String[] args) {
     
		// TODO Auto-generated method stub
		Main a = new Main();
	}
	
}

你可能感兴趣的:(java,java,swing)