客戶端 UI
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;
import com.angelfate.net.ChatClientNet;
/**
*
* @Project: chatSever /ChatSeverUI.java/com.angelfate.ui
* @Description:完成聊天界面
* @author 10406
* @date 2018年4月14日 下午4:43:49
* @company AngelFate
* @return ChatSeverUI
*/
public class ChatClientUI extends JFrame{
private ImageIcon chatLogo = null;
private int width =850,height = 840;
// 定义两个文本框,一个用于显示信息,一个用于用户输入内容
private JTextArea showMsgField = null,inputMsgField = null;
private JButton sendMsgBtn = null;
private ImageIcon showUserImage = null;
private JLabel showUserImageLabel = null,showUserInfoLabel = null;
private String username = null;
private JPanel inputMsgPanel = null,showMsgPanel = null;
private ChatClientNet clientNet = null;
public ChatClientUI(String username) {
this.username = username;
initUI();
addComponent();
addLIstener();
showUI();
//添加网络
clientNet = new ChatClientNet(showMsgField);
}
public void initUI() {
// 显示消息的文本框
showMsgField = new JTextArea(30,32);
showMsgField.setEditable(false); // 设置该文本框不可编辑
showMsgField.setBackground(Color.LIGHT_GRAY);
showMsgField.setWrapStyleWord(true);
// 供用户输入消息的文本框
inputMsgField = new JTextArea(5,32);
inputMsgField.setLineWrap(true);// 激活自动换行功能
inputMsgField.setWrapStyleWord(true); // 激活断行不断字功能
inputMsgField.setAutoscrolls(true); // 滚动条
inputMsgPanel = new JPanel();
showMsgPanel = new JPanel();
sendMsgBtn = new JButton("发送消息");
showUserImage = new ImageIcon(this.getClass().getResource("/images/chatlogo.gif"));
showUserImageLabel = new JLabel(showUserImage,JLabel.CENTER);
showUserInfoLabel = new JLabel("欢迎你:"+this.username,JLabel.CENTER);
showUserInfoLabel.setFont(new Font("宋体",Font.BOLD,16));
}
/*添加组件*/
public void addComponent() {
showMsgPanel.add(new JScrollPane(showMsgField));
this.add(showMsgPanel); // showMsgPanel
showMsgPanel.setBounds(5,3,width-300,height-250);
inputMsgPanel.add(new JScrollPane(inputMsgField));
this.add(inputMsgPanel);
inputMsgPanel.setBounds(5,height-230,width-300,100);
this.add(sendMsgBtn);
sendMsgBtn.setBounds(515,height-120, 100, 35);
this.add(showUserImageLabel);
showUserImageLabel.setBounds(width-250,100,170,435);
this.add(showUserInfoLabel);
showUserInfoLabel.setBounds(width-300,550, 200, 35);
showUserInfoLabel.setForeground(new Color(0,176,101));//颜色
}
/*给组件添加功能(事件)*/
public void addLIstener() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sendMsgBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String msg = inputMsgField.getText();
if(msg==null || msg.trim().length()<1) {
JOptionPane.showMessageDialog(null, "消息不能为空");
}else {
clientNet.sendMsgToClient(msg);
inputMsgField.setText(""); //发送完 清空
}
}
});
}
public void showUI() {
this.setTitle("客户端 聊天窗口 --- 欢迎你:"+this.username);
this.setIconImage(new ImageIcon(this.getClass().getResource("/images/chatlogo2.png")).getImage());
/*让窗口从屏幕中间弹出*/
int screenWidth = (int)this.getToolkit().getScreenSize().getWidth();
int screenHeight = (int)this.getToolkit().getScreenSize().getHeight();
int x = (screenWidth-width)/2; 坐标
int y = (screenHeight-height)/2;
this.setLocation(x, y);
this.setSize(width, height);
this.setVisible(true);
this.setResizable(false);
}
public static void main(String[] args) {
new ChatClientUI("");
}
}
用户信息处理
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
*
* @Project: chatSever /FileUtils.java/com.angelfate.tools
* @Description:根据属性文件的key(等号 前面的字符串),得到value(等号后面的字符串)值(操作文件IO流,验证登录)
* @author 10406
* @date 2018年4月14日 下午3:22:13
* @company AngelFate
* @return FileUtils
*/
public class FileUtils {
public static String getValue(String key) {
//创建一个集合类,该集合对象装 key 对应的 value 元素
Properties p = new Properties();
try {
InputStream readDB = FileUtils.class.getClassLoader().getResourceAsStream("db.properties");
p.load(readDB);
return p.getProperty(key, null);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @Title:FileUtils.java
* @Descripition:根据uname和pwd检查用户是否可登陆
* @return boolean返回类型
* @param uname
* @param pwd
* @return
* 注意:public static String getValue 是静态方法属于类,不属于对象不能用this,调用直接使用类名
*/
public static boolean checkLogin(String uname,String pwd) {
String username = getValue("username");
String password = getValue("pwd");
//判断用户输入的用户名和密码是否和文件中已经存在的用户名和密码一致,如果一致说明登录成功,返回true。否则登录失败,返回false
if(username.equals(uname) && password.equals(pwd)) {
return true;
}
return false;
}
}
客户端网络通讯
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.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
/**
*
* @Project: chatClient /ChatClientNet.java/com.angelfate.net
* @Description:客户端的网络通讯
* @author 10406
* @date 2018年4月15日 上午11:26:48
* @company AngelFate
* @return ChatClientNet
*/
public class ChatClientNet {
private PrintStream ps = null;
private BufferedReader br = null;
private JTextArea showMsgField = null;
public ChatClientNet(JTextArea showMsgField) {
this.showMsgField = showMsgField;
initNet();
getMessage();
}
private void initNet() {
try {
Socket socket = new Socket("127.0.0.1",8000);
OutputStream os = socket.getOutputStream(); // 输出流
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
ps = new PrintStream(os);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMsgToClient( String msg) {
ps.println(msg);
showMsgField.append("我发送的消息是:"+msg+"\r\n");
}
/*
* 开启新线程,不停地读取服务器端发送过来的消息
*/
public void getMessage() {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
try {
String getMsg = br.readLine();
System.out.println("收到服务器端的消息:\r\n"+getMsg+"\r\n");
showMsgField.append("收到服务器端的消息:"+new Date().toLocaleString()+"\r\n"+getMsg+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
public static void main(String[] args) {
}
}
突然想起来以前上微机课的时候用什么本地的软件进行局域网聊天来着。。