**之前在封装代码后出现了问题,按照最初的写法,在登陆完毕后弹出聊天界面后会卡死,原因是当时登陆界面和聊天界面都是使用的主线程,我的代码没有写其他线程,无法多线程运行,因此在弹出聊天界面时线程不够用了,就卡在了监听线程里,因为在监听线程中再有新的监听就会被卡住
在宇哥的指点下,将启动服务器和客户端的部分改为线程,就解决了此问题。改善后的代码简练和清晰了很多,更方便大家参考,代码如下:
**
一、登陆界面的实现
登陆界面主要使用了JFrame,以及相关的一些组件,并且在界面中加上监听
登陆界面效果图
登陆界面代码Login类
package com.lding.net;
import javax.swing.*;
import java.awt.*;
/**
* @program: Chat
* @description:
* @author: 王丁
* @date: 2021-09-26 08:58
**/
public class Login{
JTextField jTextField;
public static void main(String[] args){
Login login = new Login ();
login.showUI ("kk");
}
public void showUI(String str){
JFrame jf = new JFrame ();
jf.setTitle ("❤️DDqq登陆界面❤️");
jf.setSize (500, 400);
jf.setDefaultCloseOperation (3);
jf.setLocationRelativeTo (null);
jf.setResizable (false);
FlowLayout fl = new FlowLayout (FlowLayout.CENTER, 5, 5);
jf.setLayout (fl);
Dimension dim1 = new Dimension (500, 200);//图片大小
Dimension dim2 = new Dimension (100, 50);//标签大小
Dimension dim3 = new Dimension (300, 30);//输入框大小
Dimension dim4 = new Dimension (100, 40);//按钮大小
ImageIcon icon = new ImageIcon ("source/Login1.jpg");
JLabel labimg = new JLabel (icon);
labimg.setPreferredSize (dim1);
jf.add (labimg);
JLabel labuser = new JLabel ();
labuser.setText ("user:");
labuser.setPreferredSize (dim2);
jf.add (labuser);
JTextField textuser = new JTextField ();
textuser.setPreferredSize (dim3);
jf.add (textuser);
JLabel labpassword = new JLabel ();
labpassword.setText ("password:");
labpassword.setPreferredSize (dim2);
jf.add (labpassword);
JPasswordField textPassword = new JPasswordField ();
textPassword.setPreferredSize (dim3);
jf.add (textPassword);
JButton button = new JButton ();
button.setBorder (BorderFactory.createRaisedBevelBorder ());
button.setText ("login");
button.setPreferredSize (dim4);
jf.add (button);
jf.setVisible (true);
LoginListener ll = new LoginListener(textuser,textPassword, jf);
button.addActionListener (ll);
this.jTextField=ll.jTextField;
}