联系人信息管理小程序

刚学了Myeclipse的VE(直接拖动组件生成代码),就写了一个关于联系人信息管理的小程序,结合了文件操作,其中遇到的问题主要就是

  • 构造传参和构造接参
  • 更新界面的Validate( )必须写上
  • 使用对象流的时候,该对象必须序列化
  • 分层设计思想和面向接口编程思想
  • 模糊查询
  • 打开软件加载数据,增删改查保存数据

程序很小,一共就几个类


效果如图:

下面给出代码,为了方便查找,就按照上面给出的包的图片顺序来放代码了

1.bean
import java.io.Serializable;

public class Linkman implements Serializable{
	private static final long serialVersionUID = 1L;
	private String name;
	private String phone;
	private String address;
	public Linkman(String name, String phone, String address) {
		super();
		this.name = name;
		this.phone = phone;
		this.address = address;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Linkman() {
		super();
	}
	@Override
	public String toString() {
		return name + ", " + phone + ", " + address;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Linkman other = (Linkman) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
}
2.business逻辑层
import java.util.Vector;
import javax.swing.JFrame;
import S07_LinkmanManage.DAO.Dao;
import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.util.DealDate;

/**
 * 逻辑层
 */
public class LmBusiness {
	public static Vector<Linkman> manlist = new Vector<Linkman>();
	static{
		manlist = new Dao().getAll();
	}
	/*测试:
	 * static{//初始化数据--用于测试 manlist.add(new Linkman("老干妈","13530588036","AA"));
	 * manlist.add(new Linkman("老干爹","13530588037","BB")); manlist.add(new
	 * Linkman("蓝胖子","13530588036","CC")); }
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;

	public LmBusiness(JFrame mainFrame) {
		this.mainFrame = mainFrame;
	}

	public LmBusiness() {
		super();
	}

	/**
	 * 添加一个联系人
	 * 
	 * @param man
	 * @return
	 */
	public boolean add(Linkman man) {
		if (manlist.contains(man)) {
			return false;
		}
		int i = DealDate.valiPhone(man.getPhone());
		if (i == DealDate.NULL) {
			man.setPhone("*");
		}
		i = DealDate.valiAddress(man.getAddress());
		if (i == DealDate.NULL) {
			man.setAddress("*");
		}
		manlist.add(man);
		return new Dao().save(manlist);
	}

	public boolean delete(Linkman man) {
		manlist.remove(man);
		return new Dao().save(manlist);
	}

	public boolean update(Linkman man) {
		for (int i = 0; i < manlist.size(); i++) {
			if (manlist.get(i).equals(man)) {
				if (DealDate.NULL == DealDate.valiPhone(man.getPhone())) {
					manlist.get(i).setPhone("*");
				} else {
					manlist.get(i).setPhone(man.getPhone());
				}
				if (DealDate.NULL == DealDate.valiAddress(man.getAddress())) {
					manlist.get(i).setAddress("*");
				} else {
					manlist.get(i).setAddress(man.getAddress());
				}
				return new Dao().save(manlist);
			}
		}
		return false;
	}

	/**
	 * 模糊匹配查找 
	 * 使用卫条件
	 * @param man
	 */
	public Vector<Linkman> findMan(Linkman man) {
		String name = man.getName();
		String phone = man.getPhone();
		String address = man.getAddress();
		Vector<Linkman> list = new Vector<Linkman>();
		Vector<Linkman> allMan = new Dao().getAll();
		for (int i = 0; i < allMan.size(); i++) {
			//1.
			if (name != null && !"".equals(name.trim())) {//是否有可以比较的前提
				if(!allMan.get(i).getName().contains(name)){//是或否符合要求
					continue;
				}
			}
			//2.
			if(phone != null && !"".equals(phone.trim())){
				if(!allMan.get(i).getPhone().contains(phone.trim())){
					continue;
				}
			}
			//3.
			if(address != null && !"".equals(address.trim())){
				if(!allMan.get(i).getAddress().contains(address.trim())){
					continue;
				}
			}
			list.add(allMan.get(i));
		}
		//全部符合条件的联系人收集起来之后,将数据返回到UI
		return list;
	}
}
3.DAO数据层
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.inte.DaoInte;

public class Dao implements DaoInte{
	//private final String FILE_PATH = "E:/test/linkman.bl";
	private final String FILE_PATH = "linkman.bl";

	/**
	 * 从文件获取所有联系人
	 */
	@Override
	public Vector<Linkman> getAll() {
		Vector<Linkman>list = new Vector<Linkman>();
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(FILE_PATH));
			while(true){
				Linkman man = (Linkman) ois.readObject();
				list.add(man);
			}
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		}finally {
			if(ois != null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}

	/**
	 * 将所有联系人保存到文件
	 */
	@Override
	public boolean save(Vector<Linkman> list) {
		ObjectOutputStream out = null;
		try {
			out = new ObjectOutputStream(new FileOutputStream(FILE_PATH));
			for (Linkman linkman : list) {
				out.writeObject(linkman);
			}
		} catch (IOException e) {
			throw new RuntimeException("存储联系人错误");
		} finally{
			if(out != null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}
}
4.接口
import java.util.Vector;

import S07_LinkmanManage.bean.Linkman;
//面向接口编程
public interface DaoInte {
	public abstract boolean save(Vector<Linkman>list);
	public abstract Vector<Linkman> getAll();
}
5.UI--界面,与用户打交道
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.business.LmBusiness;
import S07_LinkmanManage.util.DealDate;

public class AddJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField tfName;
	private JTextField tfPhone;
	private JTextField tfAddress;

	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;

	public AddJPanel(JFrame mainFrame) {
		setBackground(Color.LIGHT_GRAY);
		setForeground(Color.BLACK);
		setLayout(null);
		this.mainFrame = mainFrame;// 构造传参
		JLabel lblTianjia = new JLabel("添加联系人");
		lblTianjia.setBounds(185, 26, 80, 22);
		lblTianjia.setForeground(Color.RED);
		lblTianjia.setFont(new Font("微软雅黑", Font.BOLD, 16));
		add(lblTianjia);

		JButton btnAdd = new JButton("添加");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 收集数据
				String name = tfName.getText();
				String phone = tfPhone.getText();
				String address = tfAddress.getText();
				// 校验数据
				int i = DealDate.valiName(name);
				if (i == DealDate.NULL || i == DealDate.WRONG) {
					JOptionPane.showMessageDialog(null, "姓名为空或者格式错误");
					return;
				}
				
				i = DealDate.valiPhone(phone);
				if (i == DealDate.WRONG) {
					JOptionPane.showMessageDialog(null, "电话号码格式错误");
					return ;
				}

				i = DealDate.valiAddress(address);
				if (i == DealDate.WRONG) {
					JOptionPane.showMessageDialog(null, "地址格式错误");
					return ;
				}

				// 封装数据
				Linkman man = new Linkman(name,phone,address);
				//调用逻辑层
				if(new LmBusiness().add(man)){
					JOptionPane.showMessageDialog(null, "添加成功");
					mainFrame.setContentPane(new MainJPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "添加失败");
				}
			}
		});
		btnAdd.setForeground(new Color(128, 128, 0));
		btnAdd.setFont(new Font("宋体", Font.PLAIN, 14));
		btnAdd.setBounds(83, 212, 103, 39);
		add(btnAdd);

		JLabel lbName = new JLabel("姓名:");
		lbName.setFont(new Font("微软雅黑", Font.BOLD, 15));
		lbName.setForeground(new Color(85, 107, 47));
		lbName.setBounds(106, 80, 80, 22);
		add(lbName);

		JButton btnAddToBack = new JButton("返回");
		btnAddToBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnAddToBack.setForeground(new Color(128, 128, 0));
		btnAddToBack.setFont(new Font("宋体", Font.PLAIN, 14));
		btnAddToBack.setBounds(263, 212, 103, 39);
		add(btnAddToBack);

		tfName = new JTextField();
		tfName.setBounds(199, 80, 127, 23);
		add(tfName);
		tfName.setColumns(10);

		JLabel lbPhone = new JLabel("电话:");
		lbPhone.setForeground(new Color(85, 107, 47));
		lbPhone.setFont(new Font("微软雅黑", Font.BOLD, 15));
		lbPhone.setBounds(106, 122, 80, 22);
		add(lbPhone);

		JLabel lbAddress = new JLabel("地址:");
		lbAddress.setForeground(new Color(85, 107, 47));
		lbAddress.setFont(new Font("微软雅黑", Font.BOLD, 15));
		lbAddress.setBounds(106, 164, 80, 22);
		add(lbAddress);

		tfPhone = new JTextField();
		tfPhone.setColumns(10);
		tfPhone.setBounds(199, 124, 127, 23);
		add(tfPhone);

		tfAddress = new JTextField();
		tfAddress.setColumns(10);
		tfAddress.setBounds(199, 166, 127, 23);
		add(tfAddress);

	}
}
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.business.LmBusiness;

import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DeleteJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField tfName;
	private JTextField tfPhone;
	private JTextField tfAddress;

	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;
	@SuppressWarnings("unused")
	private Linkman man = null;
	public DeleteJPanel(JFrame mainFrame,Linkman man) {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		
		this.mainFrame = mainFrame;//构造接参
		this.man = man;
		
		JLabel label = new JLabel("确认删除联系人");
		label.setBounds(166, 22, 134, 22);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		add(label);
		
		JButton btDelete = new JButton("删除");
		btDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(new LmBusiness().delete(man)){
					JOptionPane.showMessageDialog(null, "删除联系人成功");
				}else {
					JOptionPane.showMessageDialog(null, "删除联系人失败");
				}
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btDelete.setForeground(new Color(128, 128, 0));
		btDelete.setFont(new Font("宋体", Font.PLAIN, 14));
		btDelete.setBounds(90, 206, 103, 39);
		add(btDelete);
		
		JButton btBack = new JButton("返回");
		btBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btBack.setForeground(new Color(128, 128, 0));
		btBack.setFont(new Font("宋体", Font.PLAIN, 14));
		btBack.setBounds(261, 206, 103, 39);
		add(btBack);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setForeground(new Color(85, 107, 47));
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_1.setBounds(125, 68, 80, 22);
		add(label_1);
		
		tfName = new JTextField();
		tfName.setColumns(10);
		tfName.setBounds(218, 68, 127, 23);
		add(tfName);
		
		tfPhone = new JTextField();
		tfPhone.setColumns(10);
		tfPhone.setBounds(218, 112, 127, 23);
		add(tfPhone);
		
		JLabel label_2 = new JLabel("电话:");
		label_2.setForeground(new Color(85, 107, 47));
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_2.setBounds(125, 110, 80, 22);
		add(label_2);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setForeground(new Color(85, 107, 47));
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_3.setBounds(125, 152, 80, 22);
		add(label_3);
		
		tfAddress = new JTextField();
		tfAddress.setColumns(10);
		tfAddress.setBounds(218, 154, 127, 23);
		add(tfAddress);

		//设置参数
		tfName.setText(man.getName());
		tfPhone.setText(man.getPhone());
		tfAddress.setText(man.getAddress());
		//设置不可编辑
		tfName.setEditable(false);
		tfPhone.setEditable(false);
		tfAddress.setEditable(false);
	}

}
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

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

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.business.LmBusiness;
import S07_LinkmanManage.util.DealDate;

public class FindJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField tfName;
	private JTextField tfPhone;
	private JTextField tfAddress;

	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;
	public FindJPanel(JFrame mainFrame) {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		
		this.mainFrame = mainFrame;
		
		JLabel label = new JLabel("查找联系人");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setBounds(189, 10, 80, 22);
		add(label);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setForeground(new Color(85, 107, 47));
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_1.setBounds(110, 64, 80, 22);
		add(label_1);
		
		tfName = new JTextField();
		tfName.setColumns(10);
		tfName.setBounds(203, 64, 127, 23);
		add(tfName);
		
		JLabel label_2 = new JLabel("电话:");
		label_2.setForeground(new Color(85, 107, 47));
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_2.setBounds(110, 106, 80, 22);
		add(label_2);
		
		tfPhone = new JTextField();
		tfPhone.setColumns(10);
		tfPhone.setBounds(203, 108, 127, 23);
		add(tfPhone);
		
		tfAddress = new JTextField();
		tfAddress.setColumns(10);
		tfAddress.setBounds(203, 150, 127, 23);
		add(tfAddress);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setForeground(new Color(85, 107, 47));
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_3.setBounds(110, 148, 80, 22);
		add(label_3);
		
		JButton btFind = new JButton("查找");
		btFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//收集数据
				String name = tfName.getText();
				String phone = tfPhone.getText();
				String address = tfAddress.getText();
				//如果都为空就返回,继续等待输入
				if(DealDate.NULL==DealDate.valiName(name)&&
						DealDate.NULL==DealDate.valiPhone(phone)&&
						DealDate.NULL==DealDate.valiAddress(address)){
					JOptionPane.showMessageDialog(null, "请输入有效查找数据");
					return ;
				}
				//调用逻辑层,返回查找到的人
				Vector<Linkman>findList = new LmBusiness(mainFrame).findMan(new Linkman(name,phone,address));
				//在另一个UI里面显示结果
				mainFrame.setContentPane(new FindList(mainFrame, findList));
				mainFrame.validate();
			}
		});
		btFind.setForeground(new Color(128, 128, 0));
		btFind.setFont(new Font("宋体", Font.PLAIN, 14));
		btFind.setBounds(71, 209, 103, 39);
		add(btFind);
		
		JButton btBack = new JButton("返回");
		btBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btBack.setForeground(new Color(128, 128, 0));
		btBack.setFont(new Font("宋体", Font.PLAIN, 14));
		btBack.setBounds(251, 209, 103, 39);
		add(btBack);
	}
}
import javax.swing.JPanel;
import java.awt.Color;
import javax.swing.JLabel;
import java.awt.Font;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

import S07_LinkmanManage.bean.Linkman;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FindList extends JPanel {
	private static final long serialVersionUID = 1L;
	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;
	@SuppressWarnings("unused")
	private Vector<Linkman> findList;
	public FindList(JFrame mainFrame ,Vector<Linkman> findList) {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		
		this.mainFrame = mainFrame;
		this.findList = findList;
		
		JLabel label = new JLabel("相关联系人");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setBounds(180, 10, 80, 22);
		add(label);
		
		JList<Linkman> list = new JList<Linkman>();
		list.setBackground(UIManager.getColor("Button.light"));
		list.setBounds(48, 54, 352, 137);
		list.setListData(findList);//设置列表的数据
		add(list);
		
		JButton btnToFind = new JButton("继续查找");
		btnToFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new FindJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToFind.setForeground(new Color(128, 128, 0));
		btnToFind.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToFind.setBounds(100, 201, 103, 39);
		add(btnToFind);
		
		JButton btnToMain = new JButton("返回主页");
		btnToMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToMain.setForeground(new Color(128, 128, 0));
		btnToMain.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToMain.setBounds(256, 201, 103, 39);
		add(btnToMain);
	}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.business.LmBusiness;

public class MainJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;
	private JList<Linkman> list = null;
	public MainJPanel(JFrame mainFrame) {
		setForeground(UIManager.getColor("Button.light"));//JFrame mainFrame
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		
		this.mainFrame = mainFrame;
		
		JLabel label = new JLabel("联系人信息管理系统");
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setForeground(Color.RED);
		label.setBounds(157, 10, 157, 30);
		add(label);
		
		JButton btnToAdd = new JButton("添加");
		btnToAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new AddJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToAdd.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToAdd.setForeground(new Color(128, 128, 0));
		btnToAdd.setBounds(312, 61, 103, 39);
		add(btnToAdd);
		
		JButton btnToDelete = new JButton("删除");
		btnToDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(-1==list.getSelectedIndex()){
					return ;
				}
				mainFrame.setContentPane(new DeleteJPanel(mainFrame,list.getSelectedValue()));
				mainFrame.validate();
			}
		});
		btnToDelete.setForeground(new Color(128, 128, 0));
		btnToDelete.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToDelete.setBounds(312, 208, 103, 39);
		add(btnToDelete);
		
		JButton btnToUpdate = new JButton("修改");
		btnToUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(-1==list.getSelectedIndex()){
					return ;
				}
				mainFrame.setContentPane(new UpdateJPanel(mainFrame, list.getSelectedValue()));
				mainFrame.validate();
			}
		});
		btnToUpdate.setForeground(new Color(128, 128, 0));
		btnToUpdate.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToUpdate.setBounds(312, 159, 103, 39);
		add(btnToUpdate);
		
		JButton btnToFind = new JButton("查找");
		btnToFind.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new FindJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnToFind.setForeground(new Color(128, 128, 0));
		btnToFind.setFont(new Font("宋体", Font.PLAIN, 14));
		btnToFind.setBounds(312, 110, 103, 39);
		add(btnToFind);
		
		//将数据添加到JList上面显示
		list = new JList<Linkman>();
		list.setFont(new Font("微软雅黑 Light", Font.PLAIN, 13));
		list.setBackground(new Color(220, 220, 220));
		list.setBounds(25, 50, 259, 197);
		list.setListData(LmBusiness.manlist);
		add(list);
		//new JScrollPane(list);
	}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

import S07_LinkmanManage.bean.Linkman;
import S07_LinkmanManage.business.LmBusiness;
import S07_LinkmanManage.util.DealDate;

public class UpdateJPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTextField tfName;
	private JTextField tfPhone;
	private JTextField tfAddress;

	/**
	 * Create the panel.
	 */
	@SuppressWarnings("unused")
	private JFrame mainFrame = null;
	@SuppressWarnings("unused")
	private Linkman man = null;
	public UpdateJPanel(JFrame mainFrame,Linkman man) {
		setBackground(Color.LIGHT_GRAY);
		setLayout(null);
		
		//构造接参
		this.mainFrame = mainFrame;
		this.man = man;
		
		JLabel label = new JLabel("修改联系人");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 16));
		label.setBounds(187, 10, 80, 22);
		add(label);
		
		JLabel label_1 = new JLabel("姓名:");
		label_1.setForeground(new Color(85, 107, 47));
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_1.setBounds(112, 56, 80, 22);
		add(label_1);
		
		tfName = new JTextField();
		tfName.setColumns(10);
		tfName.setBounds(205, 56, 127, 23);
		add(tfName);
		
		tfPhone = new JTextField();
		tfPhone.setColumns(10);
		tfPhone.setBounds(205, 100, 127, 23);
		add(tfPhone);
		
		JLabel label_2 = new JLabel("电话:");
		label_2.setForeground(new Color(85, 107, 47));
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_2.setBounds(112, 98, 80, 22);
		add(label_2);
		
		JLabel label_3 = new JLabel("地址:");
		label_3.setForeground(new Color(85, 107, 47));
		label_3.setFont(new Font("微软雅黑", Font.BOLD, 15));
		label_3.setBounds(112, 140, 80, 22);
		add(label_3);
		
		tfAddress = new JTextField();
		tfAddress.setColumns(10);
		tfAddress.setBounds(205, 142, 127, 23);
		add(tfAddress);
		
		JButton btBack = new JButton("返回");
		btBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new MainJPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btBack.setForeground(new Color(128, 128, 0));
		btBack.setFont(new Font("宋体", Font.PLAIN, 14));
		btBack.setBounds(246, 194, 103, 39);
		add(btBack);
		
		JButton btUpdate = new JButton("修改");
		btUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//先收集信息
				String phone = tfPhone.getText();
				String address = tfAddress.getText();
				//判断信息
				if(DealDate.WRONG==DealDate.valiPhone(phone)){
					JOptionPane.showMessageDialog(null, "联系人电话号码格式错误");
					return ;
				}
				if(DealDate.WRONG==DealDate.valiAddress(address)){
					JOptionPane.showMessageDialog(null, "联系人地址格式错误");
					return ;
				}
				
				if(new LmBusiness().update(new Linkman(man.getName(),phone,address))){
					JOptionPane.showMessageDialog(null, "修改联系人信息成功");
					mainFrame.setContentPane(new MainJPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "修改联系人信息失败");
					return ;
				}
			}
		});
		btUpdate.setForeground(new Color(128, 128, 0));
		btUpdate.setFont(new Font("宋体", Font.PLAIN, 14));
		btUpdate.setBounds(81, 194, 103, 39);
		add(btUpdate);
		
		//设置编辑框
		tfName.setText(man.getName());
		tfPhone.setText(man.getPhone());
		tfAddress.setText(man.getAddress());
		tfName.setEditable(false);
	}

}
6.main方法
import java.awt.EventQueue;
import javax.swing.JFrame;
import S07_LinkmanManage.ui.MainJPanel;
public class LinkmanManageMain {
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LinkmanManageMain window = new LinkmanManageMain();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public LinkmanManageMain() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame("联系人信息管理系统");
		frame.setBounds(380, 150, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setContentPane(new MainJPanel(frame));//构造传参
	}

}




 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值