两个Java工程       1.Client             2.Server


Client   下 4个JAVA文件   一个db.properties(用来存储用户账号密码),调用集合,实现验证用户登录


1.File.java   
 package pkg;

 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.util.Properties;

 public class File {
    
//根据KEY 得到 值
public static String getValue(String key)
{//创建集合类
Properties p=new Properties();

try{ //读取文件 并将该文件写入IO流
InputStream readDB=File.class.getClassLoader().getResourceAsStream("db.properties");
p.load(readDB);       //将读取的文件赋给Properties 自动获取key-value功能
return p.getProperty(key,null);
}catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public static boolean checkLogin(String uname,String pwd)
{
String username=File.getValue("username");
String password=File.getValue("pwd");
if(username.equals(uname) && password.equals(pwd))
{
return true;
}
return false;
}
 }

2.登录界面  DengLu.java

package pkg;


 import java.awt.Dimension;
 import java.awt.FlowLayout;
 import java.awt.Image;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;


 import javax.swing.Box;
 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;
 import javax.swing.SwingConstants;




 public class DengLu extends JFrame{
public JButton btDengLu;
public JButton btZhuCe;

public JTextField tfZhangHao;
public JTextField tfMiMa;

public JLabel jb1;
public JLabel jb2;

public JPanel tPanel;
public JPanel idPanel; 
public JPanel pwdPanel;
public JPanel ButtonPanel;
public Box center=Box.createVerticalBox();




public static void main(String[] args)
{
DengLu client=new DengLu();
}
public  DengLu(){
super("三毛聊天");
btDengLu=new JButton("登录");
btZhuCe=new JButton("注册");
jb1=new JLabel("账号:");
jb2=new JLabel("密码:");



tfZhangHao=new JTextField(10);
tfMiMa=new JTextField(10);

idPanel=new JPanel();
pwdPanel=new JPanel();
ButtonPanel=new JPanel();
tPanel=new JPanel();

idPanel.add(jb1);
idPanel.add(tfZhangHao);

pwdPanel.add(jb2);
pwdPanel.add(tfMiMa);

ButtonPanel.add(btDengLu);
ButtonPanel.add(btZhuCe);


center.add(tPanel);
center.add(idPanel);
center.add(pwdPanel);

btDengLu.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent arg0) {

String username=tfZhangHao.getText();
String pwd=tfMiMa.getText();
System.out.println(username+pwd);
boolean b=File.checkLogin(username,pwd);
System.out.println();
if(b==true)
{
new ChatClientUI(username); //显示聊天窗口
DengLu.this.dispose();//关闭登录窗口
}
else
{
JOptionPane.showMessageDialog(null, "账号或密码错误!");
}
   

}

});
btZhuCe.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}

});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
int a=JOptionPane.showConfirmDialog(null, "确定关闭吗?","温馨的提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
System.exit(0);
}
}
});
         
center.add(ButtonPanel);
this.add(center);
this.pack();
this.showFrame();
}
public void showFrame()
{
//获取屏幕大小
int Width=(int)this.getToolkit().getScreenSize().getWidth();
int Height=(int)this.getToolkit().getScreenSize().getHeight();
int x=(Width-300)/2;
int y=(Height-300)/2;

this.setSize(300,300);
this.setLocation(x,y);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}}


3.ChatClientUI     聊天界面


package pkg;


 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.JScrollPane;
 import javax.swing.JTextArea;
 // 1  构造
 // 2  初始化
 // 3  拼装组件
 // 4 监听
 // 5 显示
 public class ChatClientUI extends JFrame {
private int height=600,width=560; //主窗口的高度和宽度
private ImageIcon showUserImage=null;  //User
private JPanel showMsgPanel=null,inputMsgPanel=null;
private JTextArea showMsgField=null,inputMsgField=null;
private JButton sendMsgBtn=null;
private JLabel showUserImageLabel=null,showUsernameLabel=null;
private Client client;
private String username=null;
public ChatClientUI(String username)
{
this.username=username;
initUI();
addComponment();
addListener();
showUI();
client=new Client(showMsgField);
}
public void initUI()
{ 
//初始化多项文本框
showMsgField=new JTextArea(30,32); //显示消息文本框
showMsgField.setEditable(false); //设置多文本框不能编辑
inputMsgField=new JTextArea(5,32); //显示输入消息文本框
//对多行文本框设置换行
showMsgField.setLineWrap(true); //激活自动换行功能
showMsgField.setWrapStyleWord(true); //断行不断字
showMsgField.setAutoscrolls(true);

inputMsgField.setLineWrap(true); //激活自动换行功能
inputMsgField.setWrapStyleWord(true); //断行不断字
inputMsgField.setAutoscrolls(true);

sendMsgBtn=new JButton("发送消息");
   //设置User  加载资源
  showUserImage=new ImageIcon("images/zhou.png");
  //初始化Label
  showUserImageLabel=new JLabel(showUserImage,JLabel.CENTER);
//showUsernameLabel
  showUsernameLabel=new JLabel("欢迎你"+this.username,JLabel.CENTER);
  //JPanel
  showMsgPanel=new JPanel();
  inputMsgPanel=new JPanel();
}
public void addComponment()
{
this.setLayout(null); //默认布局为空
showMsgPanel.add(new JScrollPane(showMsgField));
this.add(showMsgPanel);
showMsgPanel.setBounds(5,3,width-200,height-250);  //设置左顶点坐标(5,3)  和组件 width,height

inputMsgPanel.add(new JScrollPane(inputMsgField));

this.add(inputMsgPanel);
inputMsgPanel.setBounds(5,height-230,width-200,100);
//发送按钮
this.add(sendMsgBtn);
sendMsgBtn.setBounds(width-300, height-120, 100, 30);
//将图片放入label
showUserImageLabel.setIcon(showUserImage);
this.add(showUserImageLabel);
showUserImageLabel.setBounds(width-180,10,width-400,266);

//showUsernameLabel

this.add(showUsernameLabel);
showUsernameLabel.setBounds(width-180,400,200,30);
}
public void addListener()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//点击发送按钮,获取输入文本框内 的 信息 ,发送到网络
sendMsgBtn.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0){
String msg=inputMsgField.getText();
if(msg==null&&msg.trim().length()<1)
{JOptionPane.showMessageDialog(null, "消息不能为空");}
else
{client.sendMsgToClient(inputMsgField.getText());
inputMsgField.setText("");
}

}});

}
public void showUI()
{
this.setTitle("客户端:"+this.username);
//设置logo
this.setIconImage(new ImageIcon("images/QQ.png").getImage());
//获取屏幕大小
int Width=(int)this.getToolkit().getScreenSize().getWidth();
int Height=(int)this.getToolkit().getScreenSize().getHeight();
int x=(Width-this.width)/2;
int y=(Height-this.height)/2;
this.setLocation(x,y);
this.setResizable(false);//不可重新定义大小

this.setSize(width,height);
this.setVisible(true);
}

 }

4.Client .java    客户端文件

package pkg;


 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.net.Socket;
 import java.net.UnknownHostException;


 import javax.swing.JTextArea;


 public class Client {


BufferedReader br=null;
PrintStream ps=null;
JTextArea showMsgField=null;
public Client(JTextArea showMsgField)
{
this.showMsgField=showMsgField;
initClient();
getMsg();
}
public void initClient()
{
try {
//客户端创建Socket
Socket socket=new Socket("127.0.0.1",1025);
OutputStream os=socket.getOutputStream(); //输出流(二进制)
ps=new PrintStream(os);   



InputStream is=socket.getInputStream();
//转成文本流
br=new BufferedReader(new InputStreamReader(is));
} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

}
public void sendMsgToClient(String msg)
{
ps.println(msg);
//我发送的消息
showMsgField.append("张永超:"+msg+"\n");
}
//开启新线程   读取信号
public void getMsg()
{
new Thread(new Runnable(){



public void run() {

while(true)
{
try {
String msg=br.readLine();
//把获得的字符串追加大显示文本框中
showMsgField.append("三毛:"+msg+"\n");
} catch (IOException e) {
e.printStackTrace();
} 

}
}

}).start();
}
 }


服务器  文件

ChatServerUI.java
package pkg;


 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.JScrollPane;
 import javax.swing.JTextArea;
 // 1  构造
 // 2  初始化
 // 3  拼装组件
 // 4 监听
 // 5 显示
 public class ChatServerUI extends JFrame {
private int height=600,width=560; //主窗口的高度和宽度
private ImageIcon showUserImage=null;  //User
private JPanel showMsgPanel=null,inputMsgPanel=null;
private JTextArea showMsgField=null,inputMsgField=null;
private JButton sendMsgBtn=null;
private JLabel showUserImageLabel=null,showUsernameLabel=null;
private Server server;
private String username=null;
public ChatServerUI(String username)
{
this.username=username;
initUI();
addComponment();
addListener();
showUI();

//创建完界面  开始建立网络
server=new Server(showMsgField);
}
public void initUI()
{ 
//初始化多项文本框
showMsgField=new JTextArea(30,32); //显示消息文本框
showMsgField.setEditable(false); //设置多文本框不能编辑
inputMsgField=new JTextArea(5,32); //显示输入消息文本框
//对多行文本框设置换行
showMsgField.setLineWrap(true); //激活自动换行功能
showMsgField.setWrapStyleWord(true); //断行不断字
showMsgField.setAutoscrolls(true);

inputMsgField.setLineWrap(true); //激活自动换行功能
inputMsgField.setWrapStyleWord(true); //断行不断字
inputMsgField.setAutoscrolls(true);

sendMsgBtn=new JButton("发送消息");
   //设置User  加载资源
  showUserImage=new ImageIcon("images/wu.png");
  //初始化Label
  showUserImageLabel=new JLabel(showUserImage,JLabel.CENTER);
//showUsernameLabel
  showUsernameLabel=new JLabel("欢迎你"+this.username,JLabel.CENTER);
  //JPanel
  showMsgPanel=new JPanel();
  inputMsgPanel=new JPanel();
}
public void addComponment()
{
this.setLayout(null); //默认布局为空
showMsgPanel.add(new JScrollPane(showMsgField));
this.add(showMsgPanel);
showMsgPanel.setBounds(5,3,width-200,height-250);  //设置左顶点坐标(5,3)  和组件 width,height

inputMsgPanel.add(new JScrollPane(inputMsgField));

this.add(inputMsgPanel);
inputMsgPanel.setBounds(5,height-230,width-200,100);
//发送按钮
this.add(sendMsgBtn);
sendMsgBtn.setBounds(width-300, height-120, 100, 30);
//将图片放入label
showUserImageLabel.setIcon(showUserImage);
this.add(showUserImageLabel);
showUserImageLabel.setBounds(width-180,10,width-400,266);

//showUsernameLabel

this.add(showUsernameLabel);
showUsernameLabel.setBounds(width-180,400,200,30);
}
public void addListener()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//点击发送按钮,获取输入文本框内 的 信息 ,发送到网络
sendMsgBtn.addActionListener(new ActionListener(){


public void actionPerformed(ActionEvent arg0){
String msg=inputMsgField.getText();
if(msg==null&&msg.trim().length()<1)
{JOptionPane.showMessageDialog(null, "消息不能为空");}
else
{server.sendMsgToClient(inputMsgField.getText());
inputMsgField.setText("");
}

}});
}
public void showUI()
{
this.setTitle("服务器管理员:"+this.username);
//设置logo
this.setIconImage(new ImageIcon("images/QQ.png").getImage());
//获取屏幕大小
int Width=(int)this.getToolkit().getScreenSize().getWidth();
int Height=(int)this.getToolkit().getScreenSize().getHeight();
int x=(Width-this.width)/2;
int y=(Height-this.height)/2;
this.setLocation(x,y);
this.setResizable(false);//不可重新定义大小

this.setSize(width,height);
this.setVisible(true);
}
public static void main(String[] args)
{
new ChatServerUI("三毛");
}
 }Server.java
package pkg;


 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.net.ServerSocket;
 import java.net.Socket;


 import javax.swing.JOptionPane;
 import javax.swing.JTextArea;


 public class Server {


BufferedReader br=null;
PrintStream ps=null;
JTextArea showMsgField=null;
public Server(JTextArea showMsgField)
{
this.showMsgField=showMsgField;
initServer();//监听端口 
getMsg();   //开心线程   接收信息
}
public void initServer()
{
try {
//服务器监听端口
ServerSocket serverSocket=new ServerSocket(1025);
//服务器监听用户进入,否者等待       返回socket类型
Socket socket=serverSocket.accept();
//接收进来的二进制数据  
InputStream is=socket.getInputStream();
//转成文本流
br=new BufferedReader(new InputStreamReader(is));
   
//向网络上发送消息
ps=new PrintStream(socket.getOutputStream());
   } catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "端口被占用");
}
}
//向网络上发送消息

public void sendMsgToClient(String msg)
{
ps.println(msg);
showMsgField.append("三毛:"+msg+"\n");
}
//开启新线程   读取信号
public void getMsg()
{
new Thread(new Runnable(){



public void run() {

while(true)
{
try {
String msg=br.readLine();
//把获得的字符串追加大显示文本框中
showMsgField.append("张永超:"+msg+"\n");
} catch (IOException e) {
e.printStackTrace();
} 

}
}

}).start();
}
 }