原理
这一次改动挺大的,我就讲一下原理吧
首先把服务端和客户端的MsgType接口改一下,因为实现的功能多了,用整形排列比较便于后面的实现
package com.chatroom0808.clclient0809;
public interface MsgType {
//登录
//登录
public static final byte LOGIN = 1;
//注册
public static final byte REGISTER = 2;
public static final byte SUCCESS = 3;
public static final byte ERROR = 4;
public static final byte DUPLICATE_LOGIN = 5;
public static final byte DUPLICATE_REGISTER = 6;
public static final byte MESSAGE = 7;
}
然后我们实现一个新的ChatUI类 ,这个类的实现要在登陆成功之后才能实现。
UI整体采用边框布局,左侧为好友列表,中间的聊天部分单独采用流式布局以此加入消息框,南部加入消息发送框以及发送按钮。因为需要实现实时的收发消息,所以加入一个新的线程用于接收消息。
package com.chatroom0808.clclient0809;
import com.chatroom0808.User.User;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//聊天界面
public class ChatUI implements MsgType{
public JList<String> usernameJList;
private UserManage userManage;
private MClient mClient;
private int index;
private User user;
private ClientThread ct;
private JTextArea jta;
public ChatUI(String username, UserManage userManage, MClient mClient){
this.userManage = userManage;
this.mClient = mClient;
initUI(username);
//启动线程读取数据
ct = new ClientThread(mClient,jta);
new Thread(ct).start();
}
private void initUI(String username){
//JFrame 默认是边框布局
JFrame jf = new JFrame();
jf.setSize(580,600);
jf.setTitle(username);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 中间面板
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.GREEN);
centerPanel.setLayout(new BorderLayout());
jf.add(centerPanel, BorderLayout.CENTER);
// 显示消息
jta = new JTextArea();
jta.setPreferredSize(new Dimension(450,480));
jta.setEditable(false);
centerPanel.add(new JScrollPane(jta), BorderLayout.CENTER); // 添加滚动条支持
// 底部消息发送区域
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new FlowLayout());
jf.add(bottomPanel, BorderLayout.SOUTH);
JTextField jtf = new JTextField();
jtf.setPreferredSize(new Dimension(400,40));
bottomPanel.add(jtf);
JButton send = new JButton("发送");
bottomPanel.add(send);
// 好友列表面板
JPanel westPanel = new JPanel();
westPanel.setLayout(new BorderLayout());
westPanel.setPreferredSize(new Dimension(80,0));
jf.add(westPanel, BorderLayout.WEST);
// 显示好友数据
usernameJList = new JList<>();
usernameJList.setFont(new Font("楷体", Font.PLAIN, 20));
westPanel.add(new JScrollPane(usernameJList), BorderLayout.CENTER);
usernameJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
//当选择的好友变化时
index = usernameJList.getSelectedIndex();
user = userManage.selectUser(index); // 聊天对象
jta.setText("");
}
});
send.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// // 确定选择好友
// index = usernameJList.getSelectedIndex();
// user = userManage.selectUser(index); // 聊天对象
String chatName = user.getUsername();
String chatMsg = jtf.getText(); // 聊天内容
mClient.sendByte(MESSAGE);
mClient.sendMessage(chatName + ":" + chatMsg);
jta.append("我: " + chatMsg + "\n"); // 显示自己的消息
jtf.setText(""); // 发送后清空输入框
}
});
// 显示
jf.setVisible(true);
}
}
同时在登陆界面将username,userManage,mClient三个对象传输过去分别用于实现聊天窗口命名,好友列表获取,以及聊天功能的实现所需的客户端。
package com.chatroom0808.clclient0809;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginUI {
public static void main(String[] args) {
MClient mClient = new MClient();
UserManage userManage = new UserManage(mClient);
new LoginUI(userManage,mClient).showUI();
}
private UserManage userManage;
private MClient mClient;
public LoginUI(UserManage userManage,MClient mClient ){
this.userManage = userManage;
this.mClient = mClient;
}
public void showUI() {
//窗体
JFrame jf = new JFrame();
//像素点 => 分辨率
jf.setSize(430, 550);
jf.setTitle("登录界面");
jf.setLocationRelativeTo(null); //居中显示
jf.setDefaultCloseOperation(3); //退出进程
//流式布局管理器
FlowLayout flow = new FlowLayout();
jf.setLayout(flow);
//加载图片
ImageIcon image = new ImageIcon("C:\\Users\\eugene\\Desktop\\1723364705862.png");
//标签
JLabel jla = new JLabel(image);
jf.add(jla);
//提示信息
JLabel user = new JLabel("账号:");
jf.add(user);
//账号框(文本框)
JTextField usernameField = new JTextField();
//除了JFrame,其它组件设置大小都是该方法
//类 本身就是一种数据类型(引用类型/自定义类型)
Dimension dm = new Dimension(350, 30);
usernameField.setPreferredSize(dm);
jf.add(usernameField);
JLabel pas = new JLabel("密码:");
jf.add(pas);
//账号框(文本框)
JTextField passwordField = new JTextField();
passwordField.setPreferredSize(dm);
jf.add(passwordField);
//按钮
JButton loginBtn = new JButton("登录");
jf.add(loginBtn);
JButton registerBtn = new JButton("注册");
jf.add(registerBtn);
//匿名内部类
loginBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent

最低0.47元/天 解锁文章
2965

被折叠的 条评论
为什么被折叠?



