Socketclient=clientConnection.get(i);
OutputStreamos=client.getOutputStream();
PrintStreamps=newPrintStream(os);
ps.println(s);
}
}while(flag);
client.close();
}catch(IOExceptione){
System.out.println("有一个客户断开与服务器的连接");
}
}
}
界面:
2.登录
代码:
packagecom.qq.main;
importjava.awt.Color;
importjava.awt.Dimension;
importjava.awt.Toolkit;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JLabel;
importjavax.swing.JOptionPane;
importjavax.swing.JPasswordField;
importjavax.swing.JTextField;
importcom.qq.regist.Regist;
importcom.qq.regist.UserInformation;
/**
*主界面
*/
publicclassMainextendsJFrame{
//组件的内容
privateJLabeluserId;
privateJLabeluserPassword;
privateJTextFieldinputId;
privateJPasswordFieldinputPassword;
privateJButtonbtLogin;
privateJButtonbtRegist;
Main(){
userId=newJLabel("帐号");
userPassword=newJLabel("密码");
inputId=newJTextField(6);
inputPassword=newJPasswordField();
btLogin=newJButton("登陆");
btRegist=newJButton("注册");
//设置窗体属性
Toolkittk=Toolkit.getDefaultToolkit();
DimensionscreenSize=tk.getScreenSize();//得到当前屏幕的长和宽
intx=(int)screenSize.getWidth();
inty=(int)screenSize.getHeight();
this.setBounds((x-240)/2,(y-600)/2,240,600);//窗口显示的大小,位置
this.setResizable(false);//窗口大小不能改变
this.setLayout(null);//默认的格式
this.setBackground(Color.BLACK);//窗口的颜色
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出程序
//设置JLabel属性
userId.setBounds(30,160,40,20);
userPassword.setBounds(30,200,40,20);
//设置文本域属性
inputId.setBounds(90,160,100,20);
inputPassword.setBounds(90,200,100,20);
inputPassword.setEchoChar('*');//用*显示代替你输入的密码
//设置JButton属性
btLogin.setBounds(50,240,60,20);
btRegist.setBounds(120,240,60,20);
//注册“登陆”按钮监听器
btLogin.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
UserInformationuser=newUserInformation();
StringuserName=inputId.getText();
StringuserPassword=newString(inputPassword.getPassword());
if(userName.equals("")){
JOptionPane.showMessageDialog(null,"用户名不能为空");
}elseif("".equals(userPassword)){
JOptionPane.showMessageDialog(null,"密码不能为空");
}elseif(user.isExist(userName)
&&user.userInfomation.getProperty(userName).equals(
userPassword)){
newAllTalkFrame(userName).setVisible(true);//判断成功后new一个群聊窗口
Main.this.dispose();
}else{
JOptionPane.showMessageDialog(null,"此用户名不存在或者密码不正确");
}
}
});
//注册“注册”按钮监听器
btRegist.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newRegist();//注册页面
}
});
this.add(userId);
this.add(userPassword);
this.add(inputId);
this.add(inputPassword);
this.add(btLogin);
this.add(btRegist);
this.setVisible(true);
}
publicstaticvoidmain(String[]args){
newMain();
}
}
界面:
3.注册
代码:
//注册“提交”按钮的监听器
btSubmit.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
StringuserName=inputId.getText();
StringuserPassword=newString(inputPassword.getPassword());
StringuserPasswordConfirm=newString(inputPasswordConfirm
.getPassword());
System.out.println("您点击了提交按钮");
if(userName.equals("")){
JOptionPane.showMessageDialog(null,"用户名不能为空");
}elseif("".equals(userPassword)
||"".equals(userPasswordConfirm)){
JOptionPane.showMessageDialog(null,"密码和密码重复都不能为空");
}elseif(!
userPassword.equals(userPasswordConfirm)){
JOptionPane.showMessageDialog(null,"密码和密码重复不一致");
}else{
UserInformationuser=newUserInformation();
if(user.isExist(userName)){
JOptionPane.showMessageDialog(null,"此用户名已存在");
}else{
JOptionPane.showMessageDialog(null,"注册成功");
user.insert(userName,userPassword);//UserInformation类
Regist.this.dispose();
}
}
}
});
//注册“取消”按钮的监听器
btCancel.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("您点击了取消按钮");
Regist.this.dispose();
}
});
界面:
4.登录和注册判定
代码:
//注册一个用户
publicvoidinsert(StringuserName,StringuserPassword){
try{
userInfomation=newProperties();
InputStreamis;
OutputStreamos;
is=newFileInputStream("c:
/userInfo.properties");
os=newFileOutputStream("c:
/userInfo.properties",true);
userInfomation.load(is);
//将用户名和密码存储到内存中
userInfomation.setProperty(userName,userPassword);
//将用户名和密码保存到文件中
userInfomation.store(os,null);
}catch(FileNotFoundExceptione1){
System.out.println("文件userInfo.properties没有找到");
}catch(IOExceptione){
System.out.println("写userInfo.properties出错");
}
}
//判断此用户名是否存在
publicbooleanisExist(StringuserName){
try{
userInfomation=newProperties();
InputStreamis;
is=newFileInputStream("c:
/userInfo.properties");
userInfomation.load(is);
if(userInfomation.containsKey(userName)){
returntrue;
}
}catch(FileNotFoundExceptione1){
System.out.println("文件userInfo.properties没有找到");
}catch(IOExceptione){
System.out.println("写userInfo.properties出错");
}
returnfalse;
}
5.进入聊天界面
代码:
classshowOldMessageThreadimplementsRunnable{
publicvoidrun(){
booleanflag=true;
while(flag){
try{
//接收群聊服务器端回发过来的消息
StringserverOutput=client.br.readLine()+"\r\n";
if(!
serverOutput.startsWith("私聊")
&&!
serverOutput.startsWith("*")
&&!
(serverOutput.substring(serverOutput
.indexOf(":
")+1).equals("\r\n"))){
Strings1=serverOutput.replace('说','');
Strings=s1.replaceAll("�","\r\n");
oldMessageTextArea.append(s);
}
//添加客户端的用户在线列表
if(!
serverOutput.startsWith("*")
&&!
serverOutput.startsWith("私聊")
&&(serverOutput.indexOf("说")!
=-1)){
StringlistName=serverOutput.substring(0,
serverOutput.indexOf('说'));
//如果JList中有相同名字的用户,则不添加,否则添加
if(!
users.contains(listName)){
System.out.println("用户"+listName+"上线了");
users.add(listName);
userList.setListData(users);
}
}
//判断服务器回发过来的消息是不是以"私聊"开头的,是的话就提取出这两个用户名
if(serverOutput.startsWith("私聊")){
StringsiliaoName1=serverOutput.substring(
serverOutput.indexOf("*")+1,serverOutput
.indexOf("和"));
StringsiliaoName2=serverOutput.substring(
serverOutput.indexOf("和")+1,serverOutput
.indexOf("\r"));
StringsiliaoBenshen="";
StringsiliaoDuixiangName="";
if(siliaoName1.equals(clientName)){
siliaoBenshen=siliaoName1;
siliaoDuixiangName=siliaoName2;
}else{
siliaoBenshen=siliaoName2;
siliaoDuixiangName=siliaoName1;
}
//判断这两个名字中是否有与自己同名的,有的话就弹出个私聊窗口
if(siliaoName1.equals(clientName)
||siliaoName2.equals(clientName)){
newPointToPointTalkFrame(siliaoBenshen+"和"
+siliaoDuixiangName).setVisible(true);
}
}
}catch(IOExceptione1){
System.out.println("读取服务器端消息出错");
}
}
}
}
//注册JList的点击事件,进入私聊界面
userList.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEve