一、课题要求
1. 设一个通信录由以下几项数据信息构成
数据项 类型
姓名 字符串
地址 字符串
邮政编码 字符串
电话号码 字符串
2. 主要功能
(1) 能建立、查询、修改和增删学生通讯录
(2) 能够按多种方式进行查询
(3)界面友好,易于操作
二、设计与实现
【中文摘要】 这个基于java编写的个人通讯录管理系统含有添加、删除、查找等基本功能,采用的是JDBC-ODBC桥实现数据库的连接,ODBC数据源名称chengwz.mbd。本课程设计主要解决通讯录应用的管理问题, 设计开发一个简单的通讯录系统 ,实现新建联系人,不同方式查找信息,删除信息,修改信息,联系人头像,退出系统等功能。在课程设计中,程序通过调试运行,初步实现了设计目标,并且经过适当完善后,将可以应用在通讯录应用的管理中,解决实际问题。用户和密码是123和123,系统所用的图片,自己用Photoshop制作的,运行环境:windows
关键词 :通讯录、java、通讯录应用、个人通讯录管理系统、添加、删除、查找,数据库。
效果图:::::::::::::::::::::::::::::::::::::::::::::::::
三、完整源代码
Main.java
package com.stu.pro;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
// TODO 自动生成的方法存根
JFrame.setDefaultLookAndFeelDecorated(true);
Enter et = new Enter();
}
}
Enter.java
package com.stu.pro;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Enter extends JFrame implements ActionListener {
JLabel lb, user, mima;
JButton btn;
JProgressBar pg = new JProgressBar(1, 100);
Timer tim;
public JTextField user2;
public JPasswordField mima2;
int n = 100;
Enter() {
setSize(550, 420);
// setTitle("系统登录");
pg.setStringPainted(true);
pg.setString("沟通、从这里开始!");
pg.setBackground(Color.white);
pg.setForeground(Color.yellow);
toFront();
tim = new javax.swing.Timer(100, this);
tim.addActionListener(this);
JPanel pn = new JPanel();
setContentPane(pn);
pn.setLayout(null);
user2 = new JTextField(6);
mima2 = new JPasswordField(6);
mima2.setEchoChar('*');
user = new JLabel("用户:");
mima = new JLabel("密码:");
ImageIcon img3 = new ImageIcon("./res/image/23.jpg");
lb = new JLabel(img3);
ImageIcon img1 = new ImageIcon("./res/image/enter.jpg");
btn = new JButton(img1);
user.setBounds(20, 150, 50, 30);
user2.setBounds(55, 150, 100, 30);
mima.setBounds(20, 200, 50, 30);
mima2.setBounds(55, 200, 100, 30);
pg.setBounds(0, 346, 550, 30);
lb.setBounds(0, 0, 550, 358);
btn.setBounds(240, 249, 76, 41);
btn.setEnabled(true);
pn.add(user);
pn.add(user2);
pn.add(mima);
pn.add(mima2);
pn.add(pg);
pn.add(btn);
pn.add(lb);
btn.addActionListener(this);
this.setLocationRelativeTo(null);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn) {
if (user2.getText().trim().equals("admin") && (mima2.getText().trim().equals("123456"))) {
tim.start();
pg.setString("正在进入通讯录系统.......");
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
} else {
JOptionPane.showMessageDialog(null, "你输入的账号和密码不匹配!");
}
}
else if (--n > 0) {
pg.setValue(100 - n);
tim.restart();
}
else {
tim.stop();
dispose();
JFrame.setDefaultLookAndFeelDecorated(true);
MyFrame fr = new MyFrame();
}
}
}
MyFrame.java
package com.stu.pro;
import java.awt.*;
import java.awt.Event.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
import java.util.*;
public class MyFrame extends JFrame implements ActionListener, MouseListener {
JList lt;
private JMenuBar mb;
private JMenu mu1, mu2, mu3, mu4;
private JMenuItem mi1, mi2, mi3, mi4, mi5, mi6, mi7;
DefaultListModel dm = new DefaultListModel();
JLabel lb = new JLabel("业精于勤荒于嬉,行成于思毁于随!");
public int j = 0;
public int m = 0;
public int t = 0;
public int d = 0, f = 0, h = 0, cag = 0, del = 0;
String str1[] = new String[20]; // 姓名
String str2[] = new String[20]; // 电话
String str3[] = new String[20]; // QQ
String str4[] = new String[20]; // 地址
String str5[] = new String[20]; // 邮编
// 中间面板
class mypan extends JPanel implements ActionListener {
JLabel Name, Tel, QQq, Address, youb, lb2, lb3, lb4, lb5, lb6;
JButton btn1, btn2, btn3;
mypan(int index) {
int x = index;
setLayout(null);
setBackground(Color.white);
Name = new JLabel(str1[x] + " 的联系方式");
Tel = new JLabel("电话:");
lb2 = new JLabel(str2[x]);
QQq = new JLabel("QQ:");
lb3 = new JLabel(str3[x]);
Address = new JLabel("地址:");
lb4 = new JLabel(str4[x]);
youb = new JLabel("邮编");
lb5 = new JLabel(str5[x]);
ImageIcon img6 = new ImageIcon("./res/image/2.jpg");
if (x == 1)
img6 = new ImageIcon("./res/image/3.jpg");
if (x == 2)
img6 = new ImageIcon("./res/image/4.jpg");
if (x == 3)
img6 = new ImageIcon("./res/image/5.jpg");
if (x == 4)
img6 = new ImageIcon("./res/image/6.jpg");
if (x == 5)
img6 = new ImageIcon("./res/image/7.jpg");
if (x == 6)
img6 = new ImageIcon("./res/image/8.jpg");
if (x == 7)
img6 = new ImageIcon("./res/image/8.jpg");
if (x == 8)
img6 = new ImageIcon("./res/image/9.jpg");
if (x == 9)
img6 = new ImageIcon("./res/image/10.jpg");
lb6 = new JLabel("", img6, SwingConstants.LEADING);
btn1 = new JButton("删除");
btn2 = new JButton("退出");
btn3 = new JButton("修改");
Name.setBounds(340, 30, 200, 30);
Name.setForeground(Color.blue);
Tel.setBounds(330, 80, 60, 30);
QQq.setBounds(330, 130, 60, 30);
Address.setBounds(330, 180, 60, 30);
youb.setBounds(330, 230, 60, 30);
lb2.setBounds(370, 80, 180, 30);
lb2.setForeground(Color.red);
lb3.setBounds(370, 130, 180, 30);
lb3.setForeground(Color.red);
lb4.setBounds(370, 180, 180, 30);
lb4.setForeground(Color.red);
lb5.setBounds(370, 230, 180, 30);
lb5.setForeground(Color.red);
lb6.setBounds(100, 50, 200, 200);
btn1.setBounds(250, 300, 60, 30);
btn2.setBounds(350, 300, 60, 30);
btn3.setBounds(150, 300, 60, 30);
this.add(btn1);
this.add(btn2);
this.add(btn3);
this.add(Name);
this.add(Tel);
this.add(QQq);
this.add(Address);
this.add(youb);
this.add(lb2);
this.add(lb3);
this.add(lb4);
this.add(lb5);
this.add(lb6);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
}
/*
* public void paint(Graphics g) {
*
*
* super.paint(g);
*
* ImageIcon img = newImageIcon("D:\\1.jpg");
*
* g.drawImage(img.getImage(), 200,100, 800, 300, this);
*
* }
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getSource() == btn1) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载驱动程序
String url = "jdbc:odbc:chengwz";// URL指向要访问的数据库名test
String user = "";// MySQL配置时的用户名
String password = "";// MySQL配置时的密码
ResultSet rs = null;
Connection connection = DriverManager.getConnection(url, user, password);// 连续数据库
if (!connection.isClosed())
System.out.println("Succeededconnecting to the Database!");
Statement statement = connection.createStatement();// statement用来执行SQL语句
PreparedStatement pre = null;
String str2 = str1[f];
String sq2 = "delete * from cwz where name=?";
pre = connection.prepareStatement(sq2); // 要执行的SQL
pre.setString(1, str2);
pre.executeUpdate();
pre.close();
connection.close();
} catch (Exception ee) {
ee.printStackTrace();
System.out.println("============删除时:抛出异常===========");
}
dm.removeElementAt(f);
for (int d = f; d < j; d++) {
str1[d] = str1[d + 1];
str2[d] = str2[d + 1];
str3[d] = str3[d + 1];
str4[d] = str4[d + 1];
str5[d] = str5[d + 1];
}
j--;
f--;
JOptionPane.showMessageDialog(null, "数据删除成功!");
}
else if (e.getSource() == btn2) {
dispose();
System.exit(0);
}
else if (e.getSource() == btn3) {
new Revisefriend(f);
}
}
}
/*-----------------------------------------------------------------------------*/
mypan pn2 = new mypan(f);
mypan pnc = new mypan(f);
JLabel Name, Tel, QQq, Address, youb, lb2, lb3, lb4, lb5, lb6;
JButton btn1, btn2, btn3;
public MyFrame() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载驱动程序
String url = "jdbc:odbc:chengwz";// URL指向要访问的数据库名test
String user = "";// MySQL配置时的用户名
String password = "";// MySQL配置时的密码
Connection connection = DriverManager.getConnection(url, user, password);// 连续数据库
if (!connection.isClosed())
System.out.println("Succeededconnecting to the Database!");
Statement statement = connection.createStatement();// statement用来执行SQL语句
String sql = "select * from cwz";// 要执行的SQL语句
ResultSet rs = statement.executeQuery(sql);// 执行SQL语句并返回结果集
String name = null;
String phone = null;
String QQ = null;
String youbian = null;
String address = null;
while (rs.next()) {
name = rs.getString("name");// 选择sname这列数据
phone = rs.getString("phone");
QQ = rs.getString("QQ");
address = rs.getString("address");
youbian = rs.getString("postcard");
str1[j] = name;
str2[j] = phone;
str3[j] = QQ;
str4[j] = address;
str5[j] = youbian;
j++;
dm.addElement(name + " " + phone + " " + address);
}
rs.close();// 关闭结果集
connection.close();// 关闭连接
} catch (ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
JPanel pn = new JPanel();
JPanel pn3 = new JPanel();
mypan pn2 = new mypan(f);
mb = new JMenuBar();
mu1 = new JMenu("好友操作");
mi1 = new JMenuItem("修改");
mi2 = new JMenuItem("删除");
mu2 = new JMenu("查询");
mi3 = new JMenuItem("按姓名");
mi4 = new JMenuItem("按电话");
mu3 = new JMenu("新增");
mi5 = new JMenuItem("新建联系人");
mu4 = new JMenu("帮助");
mi6 = new JMenuItem("关于");
mi7 = new JMenuItem("退出系统");
setJMenuBar(mb);
mb.add(mu1);
mu1.add(mi1);
mu1.add(mi2);
mb.add(mu2);
mu2.add(mi3);
mu2.add(mi4);
mb.add(mu3);
mu3.add(mi5);
mb.add(mu4);
mu4.add(mi6);
mu4.add(mi7);
lb.setForeground(Color.black);
pn.add(lb);
lt = new JList(dm);
lt.setSelectionForeground(Color.blue); // 选中后的字体颜色
lt.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
JScrollPane sp = new JScrollPane(lt);
ImageIcon icon = new ImageIcon("D:\\1.jpg");
JLabel lbn = new JLabel("8", icon, SwingConstants.LEADING);
pn3.add(lbn);
add(sp, "West");
add(pn, "South");
add(pn2, "Center");
add(pn3, "North");
mi1.addActionListener(this);
mi2.addActionListener(this);
mi3.addActionListener(this);
mi4.addActionListener(this);
mi5.addActionListener(this);
mi6.addActionListener(this);
mi7.addActionListener(this);
lt.addMouseListener(this);
setSize(800, 570);
setTitle("通讯录 1.1");
setVisible(true);
setResizable(false);// 缩放
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JFrame.setDefaultLookAndFeelDecorated(true);
/*
* Font font=newFont("JFrame",Font.PLAIN,14);
*
* Enumeration keys=UIManager.getLookAndFeelDefaults().keys();
*
* while(keys.hasMoreElements()){ Object key=keys.nextElement();
*
* if(UIManager.get(key)instanceofFont)UIManager.put(keys, font);
*
* }
*/
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mi1) {
new ReviseFriendname();
}
else if (e.getSource() == mi2) {
new Deletefriend();
}
else if (e.getSource() == mi3) {
new Searchfriend();
}
else if (e.getSource() == mi4) {
new search2();
}
else if (e.getSource() == mi5) {
new Addfriend();
}
else if (e.getSource() == mi6) {
new about();
}
else if (e.getSource() == mi7) {
this.dispose();
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO 自动生成的方法存根
if (e.getClickCount() == 1)
{
int index = lt.locationToIndex(e.getPoint());
f = index;
pn2.removeAll();
System.out.println("balck");
Name = new JLabel(str1[index] + " 的联系方式");
Tel = new JLabel("电话:");
lb2 = new JLabel(str2[index]);
QQq = new JLabel("QQ:");
lb3 = new JLabel(str3[index]);
Address = new JLabel("地址:");
lb4 = new JLabel(str4[index]);
youb = new JLabel("邮编");
lb5 = new JLabel(str5[index]);
ImageIcon img6 = new ImageIcon("./res/image/2.jpg");
if (index == 1)
img6 = new ImageIcon("./res/image/3.jpg");
if (index == 2)
img6 = new ImageIcon("./res/image/4.jpg");
if (index == 3)
img6 = new ImageIcon("./res/image/5.jpg");
if (index == 4)
img6 = new ImageIcon("./res/image/6.jpg");
if (index == 4)
img6 = new ImageIcon("./res/image/6.jpg");
if (index == 5)
img6 = new ImageIcon("./res/image/7.jpg");
if (index == 6)
img6 = new ImageIcon("./res/image/8.jpg");
if (index == 7)
img6 = new ImageIcon("./res/image/8.jpg");
if (index == 8)
img6 = new ImageIcon("./res/image/9.jpg");
if (index == 9)
img6 = new ImageIcon("./res/image/10.jpg");
lb6 = new JLabel("", img6, SwingConstants.LEADING);
btn1 = new JButton("删除");
btn2 = new JButton("退出");
btn3 = new JButton("修改");
Name.setBounds(340, 30, 200, 30);
Name.setForeground(Color.blue);
Tel.setBounds(330, 80, 60, 30);
QQq.setBounds(330, 130, 60, 30);
Address.setBounds(330, 180, 60, 30);
youb.setBounds(330, 230, 60, 30);
lb2.setBounds(370, 80, 180, 30);
lb2.setForeground(Color.red);
lb3.setBounds(370, 130, 180, 30);
lb3.setForeground(Color.red);
lb4.setBounds(370, 180, 180, 30);
lb4.setForeground(Color.red);
lb5.setBounds(370, 230, 180, 30);
lb5.setForeground(Color.red);
lb6.setBounds(100, 50, 200, 200);
btn1.setBounds(250, 300, 60, 30);
btn2.setBounds(350, 300, 60, 30);
btn3.setBounds(150, 300, 60, 30);
pn2.add(btn1);
pn2.add(btn2);
pn2.add(btn3);
pn2.add(Name);
pn2.add(Tel);
pn2.add(QQq);
pn2.add(Address);
pn2.add(youb);
pn2.add(lb2);
pn2.add(lb3);
pn2.add(lb4);
pn2.add(lb5);
pn2.add(lb6);
add(pn2, "Center");
pn2.updateUI();
}
if (e.getClickCount() == 2)
{
int index = lt.locationToIndex(e.getPoint());
new delete(index);
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
@Override
public void mousePressed(MouseEvent e) {
// TODO 自动生成的方法存根
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO 自动生成的方法存根
}
// 增加好友界面
public class Addfriend extends JFrame implements ActionListener {
JTextField uTxt1, uTxt2, uTxt3, uTxt4, yb;
JLabel Name, Tel, QQ, Address, youbian;
JButton btn1, btn2;
Addfriend() {
this.setSize(500, 390);
this.setTitle("增加好友");
this.setLayout(null);
Name = new JLabel("姓名:");
Tel = new JLabel("电话:");
QQ = new JLabel("QQ:");
Address = new JLabel("地址:");
youbian = new JLabel("邮编");
btn1 = new JButton("新增");
btn2 = new JButton("取消");
uTxt1 = new JTextField(20);
uTxt2 = new JTextField(20);
uTxt3 = new JTextField(20);
uTxt4 = new JTextField(20);
yb = new JTextField(20);
Name.setBounds(130, 30, 60, 30);
Tel.setBounds(130, 80, 60, 30);
QQ.setBounds(130, 130, 60, 30);
Address.setBounds(130, 180, 60, 30);
youbian.setBounds(130, 230, 60, 30);
uTxt1.setBounds(170, 30, 180, 30);
uTxt2.setBounds(170, 80, 180, 30);
uTxt3.setBounds(170, 130, 180, 30);
uTxt4.setBounds(170, 180, 180, 30);
yb.setBounds(170, 230, 180, 30);
btn1.setBounds(150, 300, 60, 30);
btn2.setBounds(300, 300, 60, 30);
this.add(btn1);
this.add(btn2);
this.add(Name);
this.add(Tel);
this.add(QQ);
this.add(Address);
this.add(youbian);
this.add(uTxt1);
this.add(uTxt2);
this.add(uTxt3);
this.add(uTxt4);
this.add(yb);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
if (uTxt1.getText().trim().equals(""))
new kong();
else {
str1[j] = uTxt1.getText();
str2[j] = uTxt2.getText();
str3[j] = uTxt3.getText();
str4[j] = uTxt4.getText();
str5[j] = yb.getText();
j++;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载驱动程序
String url = "jdbc:odbc:chengwz"; // URL指向要访问的数据库名test
String user = ""; // MySQL配置时的用户名
String password = ""; // MySQL配置时的密码
Connection connection = DriverManager.getConnection(url, user, password);// 连续数据库
PreparedStatement pre = null;
if (!connection.isClosed())
System.out.println("Succeededconnecting to the Database!");
Statement statement = connection.createStatement(); // statement用来执行SQL语句
String sql = "insert into cwz(name,phone,QQ,address,postcard)values(?,?,?,?,?)";
pre = connection.prepareStatement(sql); // 要执行的SQL语句
pre.setString(1, uTxt1.getText());
pre.setString(2, uTxt2.getText());
pre.setString(3, uTxt3.getText());
pre.setString(4, uTxt4.getText());
pre.setString(5, yb.getText());
pre.executeUpdate(); // 执行
dm.addElement(uTxt1.getText() + " " + uTxt2.getText() + " " + uTxt4.getText());
pre.close();// 关闭结果集
connection.close();// 关闭连接
} catch (Exception ee) {
System.out.println("==charucuowu");
ee.printStackTrace();
}
this.dispose();
}
}
if (e.getSource() == btn2) {
this.dispose();
}
}
}
// 按姓名查询好友界面
public class Searchfriend extends JFrame implements ActionListener {
JLabel label;
JTextField uTxt;
JButton btn1, btn2;
int f = 0;
Searchfriend() {
this.setSize(400, 300);
this.setTitle("查询好友");
this.setLayout(null);
label = new JLabel("好友名");
uTxt = new JTextField(20);
btn1 = new JButton("查询");
btn2 = new JButton("退出");
label.setBounds(80, 100, 60, 30);
uTxt.setBounds(130, 100, 140, 30);
btn1.setBounds(100, 200, 60, 30);
btn2.setBounds(220, 200, 60, 30);
this.add(label);
this.add(uTxt);
this.add(btn1);
this.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
if (uTxt.getText().trim().equals(""))
JOptionPane.showMessageDialog(null, "请输入姓名 !");
else {
for (f = 0; f <= j; f++)
if (uTxt.getText().equals(str1[f]))
new delete(f);
for (f = 0; (f <= j) && (!uTxt.getText().equals(str1[f])); f++)
if (!uTxt.getText().equals(str1[f]) && f == j)
JOptionPane.showMessageDialog(null, "通讯录中没有此联系人 !");
this.dispose();
}
}
if (e.getSource() == btn2) {
this.dispose();
}
}
}
// 按电话号码查询好友界面
public class search2 extends JFrame implements ActionListener {
JLabel label;
JTextField uTxt;
JButton btn1, btn2;
search2() {
this.setSize(400, 300);
this.setTitle("查询好友");
this.setLayout(null);
label = new JLabel("电话");
uTxt = new JTextField(20);
btn1 = new JButton("查询");
btn2 = new JButton("退出");
label.setBounds(80, 100, 60, 30);
uTxt.setBounds(130, 100, 140, 30);
btn1.setBounds(100, 200, 60, 30);
btn2.setBounds(220, 200, 60, 30);
this.add(label);
this.add(uTxt);
this.add(btn1);
this.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
if (uTxt.getText().trim().equals(""))
JOptionPane.showMessageDialog(null, "请输入联系人号码!");
else {
for (f = 0; f <= j; f++)
if (uTxt.getText().equals(str2[f]))
new delete(f);
for (f = 0; (f <= j) && (!uTxt.getText().equals(str2[f])); f++)
if ((!uTxt.getText().equals(str2[f])) && (f == j))
new none();
this.dispose();
}
}
if (e.getSource() == btn2) {
this.dispose();
}
}
}
// 好友信息修改界面
public class Revisefriend extends JFrame implements ActionListener,
ItemListener {
JTextField uTxt1, uTxt2, uTxt3, uTxt4, yb;
JLabel Name, Tel, QQ, Address, youbian;
JButton btn1, btn2;
Revisefriend(int t) {
this.setSize(500, 380);
this.setTitle("好友信息");
this.setLayout(null);
Name = new JLabel("姓名:");
Tel = new JLabel("电话:");
QQ = new JLabel("QQ:");
Address = new JLabel("地址:");
youbian = new JLabel("邮编");
btn1 = new JButton("修改");
btn2 = new JButton("取消");
uTxt1 = new JTextField(20);
uTxt2 = new JTextField(20);
uTxt3 = new JTextField(20);
uTxt4 = new JTextField(20);
yb = new JTextField(20);
Name.setBounds(130, 30, 60, 30);
Tel.setBounds(130, 80, 60, 30);
QQ.setBounds(130, 130, 60, 30);
Address.setBounds(130, 180, 60, 30);
youbian.setBounds(130, 230, 60, 30);
uTxt1.setBounds(170, 30, 180, 30);
uTxt2.setBounds(170, 80, 180, 30);
uTxt3.setBounds(170, 130, 180, 30);
uTxt4.setBounds(170, 180, 180, 30);
yb.setBounds(170, 230, 180, 30);
btn1.setBounds(150, 290, 60, 30);
btn2.setBounds(320, 290, 60, 30);
uTxt1.setText(str1[t]);
uTxt2.setText(str2[t]);
uTxt3.setText(str3[t]);
uTxt4.setText(str4[t]);
yb.setText(str5[t]);
this.add(btn1);
this.add(btn2);
this.add(Name);
this.add(Tel);
this.add(QQ);
this.add(Address);
this.add(youbian);
this.add(uTxt1);
this.add(uTxt2);
this.add(uTxt3);
this.add(uTxt4);
this.add(yb);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
t = cag;
if (uTxt1.getText().trim().equals(""))
new kong();
else {
str1[t] = uTxt1.getText();
str2[t] = uTxt2.getText();
str3[t] = uTxt3.getText();
str4[t] = uTxt4.getText();
str5[t] = yb.getText();
dm.removeElementAt(t);
dm.add(t, uTxt1.getText() + " " + uTxt2.getText() + " " + uTxt4.getText());
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载驱动程序
String url = "jdbc:odbc:chengwz";
String user = "";
String password = "";
PreparedStatement pre = null;
ResultSet rs;
Connection connection = DriverManager.getConnection(url, user, password);
if (!connection.isClosed())
System.out.println("Succeededconnecting to the Database!");
Statement statement = connection.createStatement(); // statement用来执行SQL语句
String sql = "update cwz set name=?,phone=?,QQ=?,address=?,postcard=? wherename=?";
pre = connection.prepareStatement(sql); // 要执行的SQL
pre.setString(1, uTxt1.getText());
pre.setString(2, uTxt2.getText());
pre.setString(3, uTxt3.getText());
pre.setString(4, uTxt4.getText());
pre.setString(5, yb.getText());
pre.setString(6, uTxt1.getText());
pre.executeUpdate();
pre.close();// 关闭结果集
connection.close();// 关闭连接
} catch (Exception ee) {
System.out.println("==修改时出错==");
ee.printStackTrace();
}
this.dispose();
}
}
else if (e.getSource() == btn2) {
this.dispose();
}
}
public void itemStateChanged(ItemEvent e) {
}
}
// 修改好友提示界面
public class ReviseFriendname extends JFrame implements ActionListener {
JLabel FriendName;
JTextField uTxt;
JButton btn1, btn2;
ReviseFriendname() {
this.setSize(400, 300);
this.setTitle("修改好友");
this.setLayout(null);
FriendName = new JLabel("好友名");
uTxt = new JTextField(20);
btn1 = new JButton("搜索");
btn2 = new JButton("退出");
FriendName.setBounds(80, 100, 60, 30);
uTxt.setBounds(140, 100, 160, 30);
btn1.setBounds(100, 200, 60, 30);
btn2.setBounds(220, 200, 60, 30);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.add(FriendName);
this.add(uTxt);
this.add(btn1);
this.add(btn2);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
if (uTxt.getText().trim().equals(""))
new kong();
else {
for (m = 0; m <= j; m++)
if (uTxt.getText().equals(str1[m])) {
cag = m;
new Revisefriend(m);
}
for (m = 0; m <= j && !(uTxt.getText().equals(str1[m])); m++)
if ((!(uTxt.getText().equals(str1[m]))) && (m == j))
new none();
this.dispose();
}
}
if (e.getSource() == btn2) {
this.dispose();
}
}
}
// 删除好友界面1
public class Deletefriend extends JFrame implements ActionListener {
JLabel FriendName;
JTextField uTxt;
JButton btn1, btn2;
String name;
Deletefriend() {
this.setSize(400, 300);
this.setTitle("搜索你要删除的好友名 ");
this.setLayout(null);
FriendName = new JLabel("好友名");
uTxt = new JTextField(20);
btn1 = new JButton("打开");
btn2 = new JButton("退出");
FriendName.setBounds(80, 60, 100, 30);
uTxt.setBounds(140, 60, 180, 30);
btn1.setBounds(100, 200, 60, 30);
btn2.setBounds(200, 200, 60, 30);
this.add(uTxt);
this.add(FriendName);
this.add(btn1);
this.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn2) {
this.dispose();
}
if (e.getSource() == btn1) {
if (uTxt.getText().trim().equals(""))
new kong();
else {
for (int h = 0; h <= j; h++)
if (uTxt.getText().equals(str1[h])) {
new delete(h);
}
for (int h1 = 0; h1 <= j && (!uTxt.getText().equals(str1[h1])); h1++)
if ((!uTxt.getText().equals(str1[h1]) && h1 == j))
new none();
this.dispose();
}
}
}
}
// 删除好友界面2
public class delete extends JFrame implements ActionListener {
JLabel Name, Tel, QQq, Address, youb, lb2, lb3, lb4, lb5;
JButton btn1, btn2;
delete(int h) {
f = h;
this.setSize(500, 380);
this.setTitle("联系人信息");
this.setLayout(null);
Name = new JLabel(str1[f] + " 的联系方式");
Tel = new JLabel("电话:");
lb2 = new JLabel(str2[f]);
QQq = new JLabel("QQ:");
lb3 = new JLabel(str3[f]);
Address = new JLabel("地址:");
lb4 = new JLabel(str4[f]);
youb = new JLabel("邮编");
lb5 = new JLabel(str5[f]);
btn1 = new JButton("删除");
btn2 = new JButton("返回");
Name.setBounds(190, 30, 200, 30);
Name.setForeground(Color.blue);
Tel.setBounds(180, 80, 60, 30);
QQq.setBounds(180, 130, 60, 30);
Address.setBounds(180, 180, 60, 30);
youb.setBounds(180, 230, 60, 30);
lb2.setBounds(220, 80, 180, 30);
lb2.setForeground(Color.red);
lb3.setBounds(220, 130, 180, 30);
lb3.setForeground(Color.red);
lb4.setBounds(220, 180, 180, 30);
lb4.setForeground(Color.red);
lb5.setBounds(220, 230, 180, 30);
lb5.setForeground(Color.red);
btn1.setBounds(150, 270, 60, 30);
btn2.setBounds(300, 270, 60, 30);
this.add(btn1);
this.add(btn2);
this.add(Name);
this.add(Tel);
this.add(QQq);
this.add(Address);
this.add(youb);
this.add(lb2);
this.add(lb3);
this.add(lb4);
this.add(lb5);
btn1.addActionListener(this);
btn2.addActionListener(this);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn1) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");// 加载驱动程序
String url = "jdbc:odbc:chengwz";// URL指向要访问的数据库名test
String user = "";// MySQL配置时的用户名
String password = "";// MySQL配置时的密码
ResultSet rs = null;
Connection connection = DriverManager.getConnection(url, user, password);// 连续数据库
if (!connection.isClosed())
System.out.println("Succeededconnecting to the Database!");
Statement statement = connection.createStatement();// statement用来执行SQL语句
PreparedStatement pre = null;
String str2 = str1[f];
String sq2 = "delete * from cwz where name=?";
pre = connection.prepareStatement(sq2); // 要执行的SQL
pre.setString(1, str2);
pre.executeUpdate();
pre.close();
connection.close();
} catch (Exception ee) {
ee.printStackTrace();
System.out.println("============删除时:抛出异常===========");
}
dm.removeElementAt(f);
for (int d = f; d < j; d++) {
str1[d] = str1[d + 1];
str2[d] = str2[d + 1];
str3[d] = str3[d + 1];
str4[d] = str4[d + 1];
str5[d] = str5[d + 1];
}
j--;
f--;
this.dispose();
}
else if (e.getSource() == btn2) {
this.dispose();
}
}
}
// 关于
public class about extends JFrame {
JLabel lb1, lb2, lb3, lb4, lb5;
about() {
this.setSize(300, 200);
this.setTitle("关于");
this.setLayout(null);
lb1 = new JLabel(" java 课程设计 通讯录 1.1");
lb2 = new JLabel("海南师范大学信息学院 2012级计本(1)班 ");
lb3 = new JLabel(" 指导老师:文 斌 ");
lb4 = new JLabel(" 2013.12");
lb5 = new JLabel("陈万洲 201224010203");
lb1.setBounds(20, 0, 400, 50);
lb2.setBounds(20, 20, 400, 50);
lb5.setBounds(20, 40, 400, 50);
lb3.setBounds(20, 60, 400, 100);
lb4.setBounds(20, 80, 400, 100);
lb1.setForeground(Color.blue);
this.add(lb1);
this.add(lb2);
this.add(lb3);
this.add(lb4);
this.add(lb5);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
}
// 查无此人
public class none extends JFrame {
JLabel lb1;
none() {
this.setSize(300, 200);
this.setTitle("提示");
this.setLayout(null);
lb1 = new JLabel("查无此联系人!");
lb1.setBounds(20, 0, 400, 100);
lb1.setForeground(Color.RED);
this.add(lb1);
this.setLocation(650, 340);
this.dispose();
this.setVisible(true);
}
}
// 没有输入名字提示界面
public class kong extends JFrame implements ActionListener {
JLabel lb1;
JButton bt = new JButton("OK");
kong() {
this.setSize(300, 200);
this.setTitle("提示");
this.setLayout(null);
lb1 = new JLabel("请输入好友姓名 !");
lb1.setBounds(20, 0, 400, 100);
bt.setBounds(120, 100, 60, 30);
lb1.setForeground(Color.blue);
bt.setForeground(Color.blue);
this.add(lb1);
this.add(bt);
bt.addActionListener(this);
this.setLocation(1019, 414);
this.dispose();
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
dispose();
}
}
}
四、 下载资源
通讯录下载:http://pan.baidu.com/s/1mgDiqb2
源代码下载:https://download.csdn.net/download/meimeieee/13453366
五、 运行详细说明
1. 环境说明
开发工具:eclipse
JDK版本:建议JDK1.7及以下版本
数据库:Access
如果万一需要在 JDK1.8 中运行,需要手动导入 jdbc.jar 包,操作如下图:
下载 “JdbcOdbc.dll”:https://download.csdn.net/download/meimeieee/13454797
1.将 jdbc.jar 复制到 JDK目录下的“lib/ext”目录下 (如果有手动导入jdbc.jar这步可以省略)
2.将 JdbcOdbc.dll 复制到 JDK目录下的“bin”目录下
2.配置ODBC数据源(数据库配置)
第一步:打开 “控制面板”,搜索 “管理工具”
第二步:根据自己电脑系统位数,选择ODBC数据源,点击 “添加”
第三步:输入数据源名‘chengwz’,找到自己项目工程目录中的 ‘cwz.accdb’ 文件并选中,最后一路点确认就创建好了。
更多java中采用ODBC方式连接Access数据库可以参考这篇博文:https://www.iteye.com/blog/1017401036-2260786
3.运行项目
鼠标右键项目,选择 ”Run as” ,再选 “Java Application” 方式。
用户:admin
密码:123456
六、exe安装包制作
这个不做详细介绍,感兴趣的同学可以给我留言交流。