java聊天程序项目
报告
软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载
模板new
《Java高级应用开发》课程
作业(三)
课题名称: 网络聊天程序的设计与实现 专 业: 计算机应用技术 班 级: 计算机应用ZK0901 学 号: 姓 名: 成 绩:
完成时间:二?一? 年 十二月 二十五 日
一、课题设计目的
1、进一步掌握GUI图形用户界面设计的方法,以及事件处理的步骤;
2、进一步掌握线程的创建与调度;
3、进一步熟悉对Java的IO技术的应用;
4、能够利用数据库访问技术完成对数据的添加、删除、修改和查询处理;
5、掌握Socket网络通信技术。
二、课题的描述和要求
本次课题要求运用JAVA语言完成一个基于网络通信的聊天程序,能够实现QQ的简单功能。其主要由以下几个部分构成:
1.登录界面
2、用户注册界面
3、个人信息修改界面
1
4、增加好友分组界面
5、寻找添加好友界面
6、好友列
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
界面
2
7、聊天界面
三、课题设计的步骤
3.1 数据库
分析
定性数据统计分析pdf销售业绩分析模板建筑结构震害分析销售进度分析表京东商城竞争战略分析
与设计
任务一:建立E-R模型
3
根据用户需求,该聊天项目主要涉及用户、好友分组、聊天记录等实体,其实体之间关系模型图如下:
任务二:将E-R图转换为关系模式,并描述表结构
任务三:完成数据库、表及表之间关系的建立
3.2 窗体设计与窗体关系的建立
任务一:确定各个窗体类的类名,以及这些窗体之间的调用关系。经过分析该聊天程序的窗体间关系如下图所示:
UpdateFrame LoginFrame RegFrame 修改信息窗体 登录窗体 注册窗体
UpdatePassword ListFrame 修改密码窗体 主窗体
FoundFriend
查找添加好友窗体
addGroup
添加分组窗体
chatFrame
聊天窗体
思考:当用户登录成功后进入主窗体ListFrame时怎么知道是谁,进入修改信息和密码窗体时怎么知道修改谁的信息,添加好友或者分组的时候是为谁增加,
任务二:完成各个窗体类的定义(特别注意窗体的关闭方式)。
步骤1:根据系统需求,分析出窗体关闭时的操作。其关闭窗体后的操作如下所述:
1、关闭登录窗体:退出应用程序;
2、关闭注册窗体:返回到登录窗体,并且释放注册窗体资源;
3、关闭主窗体:退出应用程序;
4、关闭修改信息、密码窗体,以及添加好友、分组和聊天窗体:都应该是返回
到主窗体,并且将这些窗口资源释放。
步骤2:完成各个窗体类的定义。每个窗体主要代码如下:
1、登录窗体代码
import javax.swing.*;import java.awt.*;
import java.awt.event.*;import java.sql.*;
public class LoginFrame extends JFrame {
Box bv1,bv2,bv,bv3;
JLabel b1,b2,b3,b4;
JTextField userName,server,port;
4
JPasswordField pwd;
JButton login,reg,set,save;
JPanel p1,p2,p3,p4;
LoginFrame(String s){//构造方法
super(s);
this.setBounds(200,200,240,120);
this.setResizable(false);
bv=Box.createVerticalBox();
p2=new JPanel();
bv3=Box.createHorizontalBox();
bv1=Box.createVerticalBox();
b1=new JLabel("用户号:");
bv1.add(b1);
b2=new JLabel("密 码:");
bv1.add(b2);
bv3.add(bv1);
bv2=Box.createVerticalBox();
userName=new JTextField(16);
bv2.add(userName);
pwd=new JPasswordField(16);
pwd.setEchoChar('*');
bv2.add(pwd);
bv3.add(bv2);
p2.add(bv3);
bv.add(p2);
p1=new JPanel();
login=new JButton("登录");
p1.add(login);
reg=new JButton("注册");
p1.add(reg);
set=new JButton("设置");
p1.add(set);
bv.add(p1);
p3=new JPanel();
b3=new JLabel("服务器:");
p3.add(b3);
server=new JTextField(12);
server.setText("127.0.0.1");
p3.add(server);
bv.add(p3);
p4=new JPanel();
b4=new JLabel("端口:");
p4.add(b4);
port=new JTextField(10);
port.setText("5555");
p4.add(port);
5
save=new JButton("保存");
p4.add(save);
bv.add(p4);
this.add(bv);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
2、注册窗体代码如下:
import java.awt.*;import java.awt.event.*; import javax.swing.*;import java.sql.*;
public class RegFrame extends JFrame {
JLabel b1=new JLabel("用户号:"),
b2=new JLabel("密码:"),
b7=new JLabel("确认:"),
b3=new JLabel("昵称:"),
b4=new JLabel("性别:"),
b5=new JLabel("城市:"),
b6=new JLabel("邮箱:");
JTextField userName=new JTextField(10),
nickName=new JTextField(10),
sex=new JTextField(10),
city=new JTextField(10),
email=new JTextField(10);
JPasswordField pwd=new JPasswordField(10),
qrpwd=new JPasswordField(10);
JButton regButton=new JButton("注册"),
exitButton=new JButton("退出");
RegFrame(String s){//构造方法
super(s);
this.setBounds(200,200,180,260);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.add(b1); this.add(userName);
userName.setEnabled(false);//设置不允许对用户名进行编辑
this.add(b2); this.add(pwd);
pwd.setEchoChar('*');//设置密码显示的字符
this.add(b7); this.add(qrpwd);
qrpwd.setEchoChar('*');//设置密码显示的字符
this.add(b3); this.add(nickName);
this.add(b4); this.add(sex);
this.add(b5); this.add(city);
this.add(b6); this.add(email);
this.add(regButton); this.add(exitButton);
this.validate();
6
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
3、主窗体界面的代码(特别注意树创建的步骤)
import java.awt.*;import java.awt.event.*;
import java.sql.*;import javax.swing.*; import javax.swing.event.*;import javax.swing.tree.*;
public class ListFrame extends JFrame {
String userName;//用来保存当前登录的用户账号,通过构造方法传入
JMenuBar mb=new JMenuBar();
JMenu m1=new JMenu("主菜单"),m2=new JMenu("系统设置");
JMenuItem m11=new JMenuItem("寻找好友"),
m12=new JMenuItem("添加分组"),
m21=new JMenuItem("个人资料"),
m22=new JMenuItem("基本设置"),
m14=new JMenuItem("修改密码"),
m15=new JMenuItem("帮助"),
m16=new JMenuItem("退出");
JTree tree;
DefaultTreeModel treeModel=null;
DefaultMutableTreeNode root;
DefaultMutableTreeNode selNode;//获取被选中的节点
JScrollPane sc;
ListFrame(String s,String userName){//构造方法
super(s);
this.userName=userName;//获得当前登录的用户账号
this.setBounds(400,80,200,500);
this.setResizable(false);
m1.add(m11);
m1.add(m12);
m1.addSeparator();
m2.add(m21);
m2.add(m22);
m1.add(m2);
m1.add(m14);
m1.addSeparator();
m1.add(m15);
m1.add(m16);
mb.add(m1);
this.setJMenuBar(mb);
//完成用户列表设计
root=new DefaultMutableTreeNode("好友列表");
treeModel=new DefaultTreeModel(root); //设置好友分组
DefaultMutableTreeNode node1=new DefaultMutableTreeNode("我的好友");
7
treeModel.insertNodeInto(node1,root,root.getChildCount());
//循环添加好友在上面分组中
for(int i=0;i<5;i++){//循环添加好友
DefaultMutableTreeNode node2=new DefaultMutableTreeNode("friendName"+i);
treeModel.insertNodeInto(node2,node1,node1.getChildCount());
}
tree=new JTree(treeModel);//通过树模型创建树对象tree
tree.setRowHeight(20);//设置每行高度
tree.setEditable(true);//设置可以以编辑
JScrollPane sc=new JScrollPane();
sc.setViewportView(tree);//将树放到滚动窗格中
this.add(sc,BorderLayout.CENTER);//将滚动窗格放在窗体的中央
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
4、修改信息窗体的代码
import java.awt.*;import java.awt.event.*; import java.sql.*;import javax.swing.*; public class UpdateFrame extends JFrame{
String userName;
JLabel b1=new JLabel("用户号:"),
b3=new JLabel("昵称:"),
b4=new JLabel("性别:"),
b5=new JLabel("城市:"),
b6=new JLabel("邮箱:");
JTextField userName1=new JTextField(10),
nickName=new JTextField(10),
sex=new JTextField(10),
city=new JTextField(10),
email=new JTextField(10);
JButton regButton=new JButton("修改"),
exitButton=new JButton("退出");
UpdateFrame(String s,String userName){
super(s);
this.userName=userName;
this.setBounds(200,200,180,210);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.add(b1); this.add(userName1);
userName1.setEditable(false);
userName1.setText(userName);//显示当前登录的用户号
this.add(b3); this.add(nickName);
this.add(b4); this.add(sex);
this.add(b5); this.add(city);
8
this.add(b6); this.add(email);
this.add(regButton); this.add(exitButton);
this.validate();//刷新窗体的显示
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
5、修改密码的窗体代码
import java.awt.*;import java.awt.event.*; import java.sql.*;import javax.swing.*; public class UpdatePassword extends JFrame{
String userName;
JLabel b1=new JLabel("用户号:"),
b2=new JLabel("原密码:"),
b3=new JLabel("新密码:"),
b4=new JLabel("确认:");
JTextField userName1=new JTextField(10),
pwd1=new JTextField(10),
pwd=new JTextField(10),
qrpwd=new JTextField(10);
JButton updateButton=new JButton("修改"),
exitButton=new JButton("退出");
UpdatePassword(String s,String userName){//构造方法
super(s);
this.userName=userName;
this.setBounds(200,200,180,210);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.add(b1); this.add(userName1);
userName1.setEditable(false); userName1.setText(userName);
this.add(b2); this.add(pwd1);
this.add(b3); this.add(pwd);
this.add(b4); this.add(qrpwd);
this.add(updateButton);
this.add(exitButton);
this.validate();//刷新窗体的显示
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
6、添加好友窗体代码
import java.awt.*;import java.awt.event.*; import javax.swing.*;import javax.swing.table.*; import java.sql.*;
public class FoundFriend extends JFrame {
String userName;//用来保存当前登录的用户账号,通过构造方法传入
9
JLabel b1=new JLabel("请输入用户号:"),
b2=new JLabel("昵称:"),
b3=new JLabel("选择分组:");
JTextField userNameField=new JTextField(10),
nickNameField=new JTextField(10);
JButton searchButton=new JButton("搜索"),
addButton=new JButton("添加好友"),
exitButton=new JButton("退出");
JPanel p1=new JPanel(),
p2=new JPanel(),
p3=new JPanel();
JComboBox jbx1=new JComboBox();
// 定义表格相关对象
JTable friends;
JScrollPane jscp1;
String columnNames[]={"用户号","昵称","性别","城市","邮箱"};//定义表格的标题
Object[][] rowData={};
DefaultTableModel defaultModel;//定义一个默认表格模型
String[][] data1={{"","","","",""}};//初始一行空白行
String[] data = new String[5];
FoundFriend(String s,String userName){//通过userName作为参数,可以将当前登录用户号传
入进来
super(s);
this.userName=userName;//获得当前登录的用户账号
this.setBounds(100,100,440,400);
this.setResizable(false);
p1.add(b1); p1.add(userNameField);
p1.add(b2); p1.add(nickNameField);
p1.add(searchButton);
this.add(p1,BorderLayout.NORTH);
//创建表格
defaultModel= new DefaultTableModel(data1,columnNames);
friends=new JTable(defaultModel);
friends.setPreferredScrollableViewportSize(new Dimension(400,300));//设置表格的大小
friends.setRowHeight(30);
//friends.setRowHeight(0,20);
friends.setRowHeight(20);//设置相邻两行单元格的距离
friends.setRowSelectionAllowed(true);//设置可否被选择
friends.setSelectionBackground(Color.lightGray);//设置所选择行的背景颜色
friends.setSelectionForeground(Color.red);//设置所选择行的前景色
friends.setGridColor(Color.black);//设置网格线的颜色
friends.setBackground(Color.white);//设置表格背景颜色为亮灰色
jscp1= new JScrollPane (friends);
p2.add(jscp1);
this.add(p2,BorderLayout.CENTER);
10
p3.add(b3); p3.add(jbx1);//添加下拉选择分组
jbx1.addItem("我的好友"); jbx1.addItem("大学同学");
p3.add(addButton);
p3.add(exitButton);
this.add(p3,BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
7、添加分组窗体代码
import java.awt.*;import java.awt.event.*; import java.sql.*;import javax.swing.*; public class AddGroup extends JFrame {
String userName;
JLabel b1=new JLabel("用户号:"),
b2=new JLabel("分组名:");
JTextField userName1=new JTextField(10),
gName=new JTextField(10);
JButton addButton=new JButton("添加"),
exitButton=new JButton("退出");
AddGroup(String s,String userName){//构造方法
super(s);
this.userName=userName;
this.setBounds(200,200,180,120);
this.setResizable(false);
this.setLayout(new FlowLayout());
this.add(b1); this.add(userName1);
userName1.setEnabled(false); userName1.setText(userName);
this.add(b2); this.add(gName);
this.add(addButton);
this.add(exitButton);
this.validate();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
8、聊天窗体代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatFrame extends JFrame {
String userName;
JLabel lb1;
JTextField ip,port;
JButton con,send;
JTextArea note,msg;
11
JScrollPane js1,js2;
JPanel p1=new JPanel();
ChatFrame(String s,String userName){
super(s);
this.userName=userName;
setBounds(100,100,380,410);
setLayout(new FlowLayout());//设置流型布局
lb1=new JLabel("To:");
note=new JTextArea(12,30);
note.setLineWrap(true);//设置文本框自动换行
msg=new JTextArea(4,30);
msg.setLineWrap(true);//设置文本框自动换行
send=new JButton("发送信息");
js1=new JScrollPane(note);//添加滚动条
js2=new JScrollPane(msg);
add(js1);
add(lb1);
add(js2);
p1.add(send);
add(p1);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
任务三:完成窗体之间的访问。要完成事件处理,应首先为类引用按钮事件接口
ActionListener,然后再为事件源添加监视器***.addActionListener(this),最后重写
抽象的方法public void actionPerformed(ActionEvent e)完成窗体之间的调用。 1、完成登录窗体上按钮事件的处理。要求点击“注册”时进入注册窗体RegFrame,
点击击“登录”时进入主窗体ListFrame,点击“设置”或“收回”时完成窗体大小改变。 if((e.getSource()==set)&&(set.Text().equals("设置"))){
set.setText("收回");
this.setSize(240,200);
this.validate();
}
else if((e.getSource()==set)&&(set.getText().equals("收回"))){
set.setText("设置");
this.setSize(240,120);
this.validate();
}else if(e.getSource()==reg){
new RegFrame("注册用户");
}else if(e.getSource()==login){
new ListFrame("我的聊天程序",userName.getText().trim()); }
12
2、点击主窗体的“寻找好友”菜单,进入好友窗体FoundFriend;点击主窗体的“添加分组”菜单,进入分组窗体AddGroup;点击主窗体的“个人资料”菜单,进入修改信息窗体UpdateFrame;点击主窗体的“修改密码”菜单,进入修改密码窗体UpdatePassword。
if(e.getSource()==m11){//添加好友
new FoundFriend("搜索好友",userName);
}
else if(e.getSource()==m12){
new AddGroup("添加用户分组",userName);
}
if(e.getSource()==m14){//修改密码
new UpdatePassword("修改密码",userName);
}
else if(e.getSource()==m21){//修改个人信息
new UpdateFrame("修改个人信息",userName);
}
3.3 用户登录验证的实现
要进行SQL Server2000数据库的访问,首先需要将数据库的驱动资源包
拷贝到JDK的安装路径下的LIB文件夹中;然后打开eclipse选中正在开发的项目,点击鼠标右键,在快捷菜单中选择“构建路径->配置构建路径”进入如下界面
13
接着选择“库”,点击“添加外部JAR”,选中相应位置的三个JAR包;最后确定即可完成驱动程序的加载。下面就可以开始编写代码了。
步骤1:引用接口ActionListener,并且为“登录”按钮加载监视器regButton.addActionListener(this),然后重写抽象方法public void actionPerformed(ActionEvent e){}
步骤2:首先声明数据库访问的变量,然后在抽象方法中完成登录验证(验证方法为:通过输入的用户名和密码在users表中查找是否存在对应的记录,如果存在说明验证成功进入主界面;否则验证失败给出相应提示)。
(1)加载数据库驱动程序
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null,"加载驱动程序失败~");
}
(2)创建数据库连接con
(3)创建带参数的执行对象stmt
(4)获得结果集rst
(5)判断结果集是否为空,不为空则验证成功进入主窗体ListFrame
(6)关闭结果集、执行对象、连接对象。
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.1:1433;DatabaseName
=chatDB","sa","123");
stmt=con.createStatement();
14
rst=stmt.executeQuery("select * from users where userName="+userName.getText()+" and
pwd="+pwd.getText());
if(rst.next()){
new ListFrame("我的聊天程序",userName.getText().trim());
this.dispose();
}else{
JOptionPane.showMessageDialog(null,"没有这个用户或者密码错误~","登录提示",JOptionPane.INFORMATION_MESSAGE);
}
rst.close();
stmt.close();
con.close();
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL语句执行错误~","数据库提示",JOptionPane.ERROR_MESSAGE);
}
3.4 用户注册的实现
首先为RegFrame类引用ActionListener接口,并为“注册”按钮加载监视器,重写抽象方法public void actionPerformed(ActionEvent e);然后通过用户输入登录密码、昵称、性别等基本信息向数据库的users表添加一条记录,即完成用户注册。其操作步骤如下:
public void actionPerformed(ActionEvent e){
if(e.getSource()==regButton){
try {
//1、加载数据库驱动程序
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDiaog(null,"加载驱动程序失败~");
}
try {
//2、创建数据库连接对象con
//3、创建PreparedStatement预处理执行对象pstmt
//4、为每个参数设置值
//5、执行数据插入操作,将返回值赋值给整型变量n
//6、判断n是否大于0,如果大于0提示注册成功并关闭注册窗体,否则提示注册失败
//7、关闭预处理对象pstmt
//8、关闭数据库连接对象con
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
}//end if
}
3.5 主窗体好友列表的实现
当进入主窗体就应该列出该用户的所有用户分组以及每个分组中的好友,因此需要修改构造方法中创建树的部分代码
root=new DefaultMutableTreeNode("好友列表");
15
treeModel=new DefaultTreeModel(root);
//设置好友分组
DefaultMutableTreeNode node1=new DefaultMutableTreeNode("我的好友");
treeModel.insertNodeInto(node1,root,root.getChildCount());
//循环添加好友在上面分组中
for(int i=0;i<5;i++){//循环添加好友
DefaultMutableTreeNode node2=new DefaultMutableTreeNode("friendName"+i);
treeModel.insertNodeInto(node2,node1,node1.getChildCount());
}
设计步骤:
1、首先创建根节点
root=new DefaultMutableTreeNode("好友列表");
treeModel=new DefaultTreeModel(root);
2、搜索出当前登录用户的所有好友分组,将每个分组作为子节点加入root中;然后
为每个分组添加各自的好友。
//(1)从groups表中查询出当前用户所有工作组名称,返回结果集rst
while(rst.next){//(2)循环遍历每条记录,用工作组名称来创建子节点
DefaultMutableTreeNode node1=new DefaultMutableTreeNode(rst.getString("gName"));
treeModel.insertNodeInto(node1,root,root.getChildCount());
//3、为每个分组添加各自的好友
//(1)从好友friends表中查询出当前分组中的好友,并返回结果集rst1
while(rst1.next){//(2)循环遍历每条记录,用好友号friendName来创建子节点
DefaultMutableTreeNode node2=new DefaultMutableTreeNode(rst1.getString
("friendName"));
treeModel.insertNodeInto(node2,node1,node1.getChildCount());
}
}
参考代码:
root=new DefaultMutableTreeNode("好友列表");
treeModel=new DefaultTreeModel(root);
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//1、加载数据库驱动
程序
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null,"加载驱动程序失败~");
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://192.168.0.1:1433;DatabaseName=chat
DB","sa","123");//2、创建数据库连接对象con
pstmt1=con.prepareStatement("select * from groups where userName=?");
pstmt1.setInt(1, Integer.parseInt(this.userName));
rst1=pstmt1.executeQuery();
while(rst1.next()){
16
DefaultMutableTreeNode node1=new DefaultMutableTreeNode(rst1.getString("gName"));
treeModel.insertNodeInto(node1,root,root.getChildCount());
try {
pstmt=con.prepareStatement("select * from friends where userName=?
and gName=?");
pstmt.setInt(1, Integer.parseInt(this.userName));
pstmt.setString(2,rst1.getString("gName"));
rst=pstmt.executeQuery();
while(rst.next()){
DefaultMutableTreeNode node2=new DefaultMutableTreeNode(String.valueOf(rst.getInt("friendName")));
treeModel.insertNodeInto(node2,node1,node1.getChildCount());
}
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
}
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
思考:如果需要将好友列表如下图显示,应该对上面的代码做怎样的修改呢,
3.6 修改个人信息的实现
一般情况下,当我们进入修改信息界面应该能首先看到原来的基本信息,然后进行修改提交。所以,在UpdateFrame的构造方法中应该为基本信息对应的每个文本框设置当前登录用户的基本信息值。
1、完成基本信息显示
修改构造方法中的如下代码
this.add(b1); this.add(userName1);
userName1.setEditable(false);
userName1.setText(userName);//显示当前登录的用户号
this.add(b3); this.add(nickName);
this.add(b4); this.add(sex);
this.add(b5); this.add(city);
this.add(b6); this.add(email);
根据当前登录用户信息的查询并返回结果集rst,然后根据rst获取每个字段的信息并显示在对应文本框中。
try {
//1、加载数据库驱动程序
17
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDiaog(null,"加载驱动程序失败~");
}
try {
//2、创建数据库连接对象con
//3、创建PreparedStatement预处理执行对象pstmt
//4、为每个参数设置值
//5、执行查询并返回结果集对象rst
//6、通过结果集获取字段信息并显示
if(rst.next()){
this.add(b1); this.add(userName1);
userName1.setEditable(false);
userName1.setText(userName); //显示当前登录的用户号
this.add(b3); this.add(nickName);
nickName.setText(rst.getString("nickName"));
this.add(b4); this.add(sex);
sex.setText(rst.getString("sex"));
this.add(b5); this.add(city);
city.setText(rst.getString("city"));
this.add(b6); this.add(email);
mail.setText(rst.getString("mail"));
}
//7、关闭预处理对象pstmt
//8、关闭数据库连接对象con
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
2、完成修改按钮事件处理
首先完成“修改”按钮的监视器加载,然后在actionPerformed方法中完成数据的修
改并提交数据库。
public void actionPerformed(ActionEvent e){ if(e.getSource()==updateButton){
try {
//1、加载数据库驱动程序
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDiaog(null,"加载驱动程序失败~");
}
try {
//2、创建数据库连接对象con
//3、创建PreparedStatement预处理执行对象pstmt
//4、为每个参数设置值
//5、执行数据修改操作,将返回值赋值给整型变量n
//6、判断n是否大于0,如果大于0提示修改成功,否则提示修改失败
//7、关闭预处理对象pstmt
//8、关闭数据库连接对象con
18
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
}//end if
}
3.7 修改登录密码的实现
首先判断两次输入的密码是否一致,如果一致则修改当前用户的密码,否则不能修
改并给出相应提示。(请给出修改按钮事件的代码)
3.8 添加好友的实现
任务一:显示窗体时将好友分组显示在下拉选择框中
在构造方法中完成下拉选择框加载后,添加如下语句:
//获得该用户的分组,逐一追加到下拉选择框中
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null,"加载驱动程序失败~");
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://
192.168.0.1:1433;DatabaseName=chatDB","sa","123");
pstmt=con.prepareStatement("select * from groups where userName=? order by gno asc");
pstmt.setString(1,userName);
rst=pstmt.executeQuery();
while(rst.next()){
jbx1.addItem(rst.getString("gName"));//将分组名添加到下拉选择框中
}
rst.close();
pstmt.close();
con.close();
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"SQL执行错误~");
}
任务二:查询好友,将查询的结果显示在表格中
if(e.getSource()==searchButton){
////// 添加表格之前先删除所有表格///////
for(int i=0;i
0){
JOptionPane.showMessageDialog(null,"好友添加成功~");
}else{
JOptionPane.showMessageDialog(null,"好友添加失败~");
}
rst.close();
pstmt.close();
con.close();
this.dispose();//完成添加后,关闭添加好友窗口
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"好友添加失败~可能你已经添加了该好友。");
}
}
3.9 添加分组的实现
首先判断输入的分组名称是否在当前登录用户的好友分组中,如果在则不能添加;
如果不存在,则向分组表中增加一行。(请在下面给出“添加分组”的代码)
3.10 删除好友的实现
任务一:首先为主窗体的树增加快捷菜单,其中菜单项包括发信息、添加好友、添
加分组、删除好友、删除分组等。
//声明快捷菜单
JPopupMenu pm=new JPopupMenu();
JMenuItem pm1=new JMenuItem("发信息"),
pm2=new JMenuItem("添加好友"),
pm3=new JMenuItem("添加分组"),
pm4=new JMenuItem("删除好友"),
pm5=new JMenuItem("删除分组");
//完成快捷菜单设计
pm.add(pm1); pm1.addActionListener(this);
pm.addSeparator();
pm.add(pm2); pm2.addActionListener(this);
pm.add(pm3); pm3.addActionListener(this);
pm.addSeparator();
pm.add(pm4); pm4.addActionListener(this);
pm.add(pm5); pm5.addActionListener(this); //为好友列表添加鼠标监视器,显示快捷菜单
tree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
21
if(e.getModifiers()==InputEvent.BUTTON3_MASK){
pm.show(tree,e.getX(),e.getY());
}
}
});
任务二:完成删除好友菜单项事件的处理。
执行从好友表friends中删除被选中的好友。
if(e.getSource()==pm4){//删除好友
//加载数据库驱动程序
try{
selNode =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); //获得选中的好友账号
//获得数据库连接con
//从friends表中删除当前登录用户对应朋友记录
pstmt.setString(1,userName);//设置当前用户号
pstmt.setString(2,selNode.toString());//设置被选中的好友号
//执行删除返回整数n
if(n>0){
JOptionPane.showMessageDialog(null,"成功删除好友~");
}else{
JOptionPane.showMessageDialog(null,"删除好友失败~");
}
pstmt.close();
con.close();
}catch(SQLException e1){
JOptionPane.showMessageDialog(null,"删除好友失败~");
}
}
3.11 删除分组的实现
步骤1:首先判断删除的分组是不是默认分组“我的好友”,如果是则不允许删除;
如果不是则允许删除。
步骤2:删除选中的分组。在删除选中分组时,应该先将该分组下的好友转移得到默
认分组“我的好友”中,然后再执行删除。
if(e.getSource()==pm5){//删除分组
selNode =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); //获得选中的分组名称
if(selNode.toString().equals("我的好友")){
JOptionPane.showMessageDialog(null,"你不能删除默认分组~");
}else{//先将该组下所有好友转移到我的好友内,然后删掉该组
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null,"加载驱动程序失败~");
}
try{
22
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=chatD
B","sa","123");//获得数据库连接con
pstmt1=con.prepareStatement("update friends set gName=? where gName=?");
pstmt1.setString(1,"我的好友");
pstmt1.setString(2,selNode.toString());
int m=pstmt1.executeUpdate();
pstmt1.close();
//然后删除选中的分组
pstmt=con.prepareStatement("delete from groups where userName=? and gName=?");
pstmt.setString(1,userName);
pstmt.setString(2,selNode.toString());
int n=pstmt.executeUpdate();
if(n>0){
JOptionPane.showMessageDialog(null,"成功删除好友分组~");
this.shuaxinTree();//刷新好友列表
}
pstmt.close();
con.close();
}
}catch(SQLException e1){
JOptionPane.showMessageDialog(null,"删除好友分组失败~");
}
}
3.12 好友列表的刷新
刷新好友列表,首先移除列表中所有的节点,刷新后再重新通过数据库创建树形列
表(与好友显示思路一致)。
public void shuaxinTree(){
root.removeAllChildren();//先移除所有的节点
treeModel.reload();//刷新
try {//加载数据库驱动
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch (ClassNotFoundException e1) {
JOptionPane.showMessageDialog(null,"加载驱动程序失败~");
}
try {//重新创建树节点
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://
192.168.0.1:1433;DatabaseName=chatDB","sa","123");
pstmt=con.prepareStatement("select * from groups where userName=? order by gno asc");
pstmt.setString(1,userName);
rst=pstmt.executeQuery();
while(rst.next()){
DefaultMutableTreeNode node1=new
DefaultMutableTreeNode(rst.getString("gName"));
treeModel.insertNodeInto(node1,root,root.getChildCount());
23
pstmt1=con.prepareStatement("select * from V_List where gName=? and userName=?
order by online desc");
pstmt1.setString(1,rst.getString("gName"));
pstmt1.setInt(2,Integer.parseInt(userName));
rst1=pstmt1.executeQuery();
while(rst1.next()){
DefaultMutableTreeNode node2=new DefaultMutableTreeNode(rst1.getString("friendName"));
treeModel.insertNodeInto(node2,node1,node1.getChildCount());
}
}
rst1.close();
pstmt1.close();
rst.close();
pstmt.close();
con.close();
} catch (SQLException e1) {
JOptionPane.showMessageDialog(null,"创建好友列表失败~");
}
treeModel.reload();//再一次刷新树
}
那么,每次完成好友添加、好友删除或者是分组删除之后,都可以调用这个方法完成好友列表的刷新。
3.13 系统的拓展
一、将好友列表由原理的“1000”修改为“思源(1000)在线”,并修改删除好友功能的实现(提示:采用字符串处理方法来实现,indexOf和substring)。
举例:String s=”abc-1234-cba”,输出字符串s中的每段字串,分别得到字符串s1=”abc”,s2=”1234”,s3=”cba”。
s1=s.substring(0,s.indexOf(“-“));
s2=s.substring(s.indexOf(“-“)+1,s.lastIndexOf(“-“));
s3=s.substring(s.lastIndexOf(“-“)+1,s.length());
二、修改用户注册,要求实现用户注册成功后显示该用户注册的账号(注意可能存在多个用户同时在注册,如何确定将那个账号返回给当前用户)。
思路:在用户注册时,要求其中某个字段必须是唯一的,比如email要求每个人的都不一样,并且非空字段。注册后可根据当前输入的email来查询返回当前注册的用户账号(也可以在创建用户表时,通过对某个字段建立非空唯一性约束来实现)。
三、将修改用户个人信息窗体中的性别录入修改为下拉选择框。
四、用户登录成功后,将字段online设置为“在线”;用户退出程序时,将字段online设置为“离线”。
24
五、增加类似QQ的好友转移功能。通过选中好友,在鼠标右键中将好友移动到别的分组中。
四、
总结
初级经济法重点总结下载党员个人总结TXt高中句型全总结.doc高中句型全总结.doc理论力学知识点总结pdf
(在这里描述自己在本次课题设计中的收获与不足,提出解决
方案
气瓶 现场处置方案 .pdf气瓶 现场处置方案 .doc见习基地管理方案.doc关于群访事件的化解方案建筑工地扬尘治理专项方案下载
,要求500字以上)
25