JAVA socket 聊天程序(一)

登陆、注册、聊天页面的实现
(新手上路、请多指教)

一、启动服务器

import java.io.*;
import java.net.*;
import java.util.*;/*** 聊天系统服务器程序* 服务器启动代码*/
public class AppServer extends Thread
{//创建私有变量(封装)private ServerSocket serverSocket;  //创建服务器套接字private ServerFrame sFrame;   private static Vector userOnline = new Vector(1, 1);private static Vector v = new Vector(1, 1);/*** 创建服务器 启动服务监听1001端口*/public AppServer(){sFrame = new ServerFrame();try{serverSocket = new ServerSocket(1001);//获取服务器的主机名和IP地址InetAddress address = InetAddress.getLocalHost();sFrame.txtServerName.setText(address.getHostName());sFrame.txtIP.setText(address.getHostAddress());sFrame.txtPort.setText("1001");} catch (IOException e){fail(e, "不能启动服务!");}sFrame.txtStatus.setText("已启动...");this.start(); // 启动线程}/*** 退出服务器* * @param e*                异常* @param str*                退出信息*/public static void fail(Exception e, String str){System.out.println(str + " 。" + e);}/*** 监听客户的请求,当有用户请求时创建 Connection线程*/public void run(){try{while (true){ //监听并接受客户的请求Socket client = serverSocket.accept();new Connection(sFrame, client, userOnline, v); // 支持多线程}} catch (IOException e){fail(e, "不能监听!");}}/** * 启动服务器*/public static void main(String args[]){new AppServer();}
}

二、创建服务器窗口

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;import javax.swing.*;//////////*服务器窗口类*///////////////
public class ServerFrame extends JFrame implements ActionListener {public  JList list;private static final long serialVersionUID = -8936397327038098620L;/** ****** 服务器管理面板 ********* *///新建面板   pnlServer服务器管理面板,    pnlServerInfo服务器管理左侧信息面板JPanel pnlServer, pnlServerInfo;//创建标签  当前状态  当前在线人数  最多在线人数  服务器名称  访问协议  服务器IP  服务器端口   服务器日志标题JLabel lblStatus, lblNumber, lblMax, lblServerName, lblProtocol, lblIP,lblPort, lblLog;//创建文本public JTextField txtStatus, txtNumber, txtMax, txtServerName, txtProtocol, txtIP,txtPort;//创建按钮  关闭服务器  保存日志JButton btnStop, btnSaveLog;//TextArea对象是显示文本的多行区域。它可以设置为允许编辑或只读。public TextArea taLog;  JTabbedPane tpServer;//A component which provides a tab folder metaphor for displaying one component from a set of components//(一个组件,它提供一个标签文件夹隐喻,用于从一组组件中显示一个组件)//实现服务器管理与用户信息管理一显一隐效果public TextArea taMessage;/** ***** 用户信息面板*******/JPanel pnlUser; //创建面板//创建标签  用户信息  在线用户列表  通知  在线总人数public JLabel lblMessage, lblUser, lblNotice, lblUserCount;JList lstUser; //在线用户列表JScrollPane spUser; //A specialized container that manages a viewport, optional scrollbars and headers//管理视口、可选滚动条和标题的专用容器JTextField txtNotice;  //通知文本JButton btnSend, btnKick;public String serverMessage ="";  //用户消息/*****/public ServerFrame() {// 服务器窗口super("聊天服务器");//新建标题setSize(550, 500); //设置窗口大小setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //可关闭setResizable(false);   //不可改变大小// 在屏幕居中显示Dimension scr = Toolkit.getDefaultToolkit().getScreenSize();Dimension fra = this.getSize();if (fra.width > scr.width) {fra.width = scr.width;}if (fra.height > scr.height) {fra.height = scr.height;}this.setLocation((scr.width - fra.width) / 2,(scr.height - fra.height) / 2);// ==========服务器信息面板=================================pnlServer = new JPanel(); //Creates a new JPanel with a double bufferand a flow layout.//清空布局管理器,常用于窗体大小固定的容器里 ,pnlServer.setLayout(null); //绝对布局:组件的位置和形状不会随窗口的改变而发生变化pnlServer.setBackground(new Color(230, 230, 250)); //设置背景颜色pnlServerInfo = new JPanel(new GridLayout(14, 1));pnlServerInfo.setBackground(new Color(100,149,237));pnlServerInfo.setFont(new Font("宋体", 0, 12));pnlServerInfo.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""), BorderFactory.createEmptyBorder(1, 1, 1, 1)));//创建服务器管理信息  标签+文本框lblStatus = new JLabel("当前状态:");     //创建标签lblStatus.setForeground(Color.YELLOW); //设置字体前景颜色lblStatus.setFont(new Font("宋体", 0, 12));  //设置字体样式txtStatus = new JTextField(10);            //创建文本框txtStatus.setBackground(Color.decode("#d6f4f2")); //设置背景颜色txtStatus.setFont(new Font("宋体", 0, 12));       //设置字体样式txtStatus.setEditable(false); //文本框内容不可编辑//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblNumber = new JLabel("当前在线人数:");lblNumber.setForeground(Color.YELLOW);lblNumber.setFont(new Font("宋体", 0, 12));txtNumber = new JTextField("0 人", 10);txtNumber.setBackground(Color.decode("#d6f4f2"));txtNumber.setFont(new Font("宋体", 0, 12));txtNumber.setEditable(false);//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblMax = new JLabel("最多在线人数:");lblMax.setForeground(Color.YELLOW);lblMax.setFont(new Font("宋体", 0, 12));txtMax = new JTextField("50 人", 10);txtMax.setBackground(Color.decode("#d6f4f2"));txtMax.setFont(new Font("宋体", 0, 12));txtMax.setEditable(false);//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblServerName = new JLabel("服务器名称:");lblServerName.setForeground(Color.YELLOW);lblServerName.setFont(new Font("宋体", 0, 12));txtServerName = new JTextField(10);txtServerName.setBackground(Color.decode("#d6f4f2"));txtServerName.setFont(new Font("宋体", 0, 12));txtServerName.setEditable(false);//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblProtocol = new JLabel("访问协议:");lblProtocol.setForeground(Color.YELLOW);lblProtocol.setFont(new Font("宋体", 0, 12));txtProtocol = new JTextField("HTTP", 10);txtProtocol.setBackground(Color.decode("#d6f4f2"));txtProtocol.setFont(new Font("宋体", 0, 12));txtProtocol.setEditable(false);//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblIP = new JLabel("服务器IP:");lblIP.setForeground(Color.YELLOW);lblIP.setFont(new Font("宋体", 0, 12));txtIP = new JTextField(10);txtIP.setBackground(Color.decode("#d6f4f2"));txtIP.setFont(new Font("宋体", 0, 12));txtIP.setEditable(false);//创建标签 //设置字体前景颜色  //设置字体样式 //创建文本框 //设置背景颜色 //设置字体样式 //文本框内容不可编辑lblPort = new JLabel("服务器端口:");lblPort.setForeground(Color.YELLOW);lblPort.setFont(new Font("宋体", 0, 12));txtPort = new JTextField("8000", 10);txtPort.setBackground(Color.decode("#d6f4f2"));txtPort.setFont(new Font("宋体", 0, 12));txtPort.setEditable(false);//创建关闭按钮 并设置属性btnStop = new JButton("关闭服务器(C)"); btnStop.addActionListener(new ActionListener() {  //创建事件监听器并添加到按钮public void actionPerformed(ActionEvent arg0) {closeServer(); //关闭服务器窗口}});btnStop.setBackground(new Color(255,200,0));  //设置按钮背景颜色btnStop.setFont(new Font("宋体", 0, 12));     //设置字体样式//服务器日志标题lblLog = new JLabel("[服务器日志]");lblLog.setForeground(new Color(128,0,128));  //设置字体前景色lblLog.setFont(new Font("宋体", 0, 12));    //设置字体样式//日志内容taLog = new TextArea(20, 50); //TextArea:显示文本的多行区域。taLog.setFont(new Font("宋体", 0, 12));  //日志内容的字体设置//创建保存按钮 并设置属性btnSaveLog = new JButton("保存日志(S)");btnSaveLog.addActionListener(new ActionListener() {  //创建事件监听器并添加到按钮public void actionPerformed(ActionEvent arg0) {saveLog();  //保存日志内容(手动可持续化)}});btnSaveLog.setBackground(new Color(255,200,0)); //设置字体颜色btnSaveLog.setFont(new Font("宋体", 0, 12));    //设置字体样式//将服务器信息的标签及文本加入服务器信息面板中pnlServerInfo.add(lblStatus);pnlServerInfo.add(txtStatus);pnlServerInfo.add(lblNumber);pnlServerInfo.add(txtNumber);pnlServerInfo.add(lblMax);pnlServerInfo.add(txtMax);pnlServerInfo.add(lblServerName);pnlServerInfo.add(txtServerName);pnlServerInfo.add(lblProtocol);pnlServerInfo.add(txtProtocol);pnlServerInfo.add(lblIP);pnlServerInfo.add(txtIP);pnlServerInfo.add(lblPort);pnlServerInfo.add(txtPort);//设置服务器信息面板、标签、按钮大小及位置pnlServerInfo.setBounds(5, 5, 100, 400);lblLog.setBounds(110, 5, 100, 30);taLog.setBounds(110, 35, 400, 370);btnStop.setBounds(200, 410, 120, 30);btnSaveLog.setBounds(320, 410, 120, 30);pnlServer.add(pnlServerInfo);//将信息面板加入管理面板中pnlServer.add(lblLog);pnlServer.add(taLog);pnlServer.add(btnStop);pnlServer.add(btnSaveLog);// ===========在线用户面板====================================//面板pnlUser = new JPanel();pnlUser.setLayout(null);pnlUser.setBackground(new Color(100,149,237));pnlUser.setFont(new Font("宋体", 0, 14));//用户消息lblMessage = new JLabel("[用户消息]");lblMessage.setFont(new Font("宋体", 0, 14));lblMessage.setForeground(Color.YELLOW);taMessage = new TextArea(20, 20);taMessage.setFont(new Font("宋体", 0, 14));//通知:lblNotice = new JLabel("通知:");lblNotice.setFont(new Font("宋体", 0, 13));txtNotice = new JTextField(20);txtNotice.setFont(new Font("宋体", 0, 14));//消息发送按钮btnSend = new JButton("发送");btnSend.setBackground(Color.ORANGE);btnSend.setFont(new Font("宋体", 0, 14));btnSend.setEnabled(true);  //启用按钮btnSend.addActionListener(new ActionListener() {   //创建事件监听器,并添加到按钮public void actionPerformed(ActionEvent arg0) {serverMessage();}});lblUserCount = new JLabel("在线总人数 0 人");lblUserCount.setFont(new Font("宋体", 0, 13));lblUser = new JLabel("[在线用户列表]");lblUser.setFont(new Font("宋体", 0, 14));lblUser.setForeground(Color.YELLOW);lstUser = new JList();lstUser.setFont(new Font("宋体", 0, 14));lstUser.setVisibleRowCount(17);//无需滚动即可显示的首选行数lstUser.setFixedCellWidth(180);//定义大于零时的固定单元格宽度lstUser.setFixedCellHeight(18);//定义大于零时的固定单元格高度spUser = new JScrollPane();//创建一个空的(无视图)jscrollpane当需要时,水平和垂直滚动条都会出现。spUser.setBackground(Color.decode("#d6f4f2"));spUser.setFont(new Font("宋体", 0, 14));spUser.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);spUser.getViewport().setView(lstUser);//设置标签及按钮位置与大小lblMessage.setBounds(5, 5, 100, 25);taMessage.setBounds(5, 35, 300, 360);lblUser.setBounds(310, 5, 100, 25);spUser.setBounds(310, 35, 220, 360);lblNotice.setBounds(5, 410, 40, 25);txtNotice.setBounds(50, 410, 160, 25);btnSend.setBounds(210, 410, 80, 25);lblUserCount.setBounds(320, 410, 100, 25);//将按钮标签等加入面板pnlUser.add(lblMessage);pnlUser.add(taMessage);pnlUser.add(lblUser);pnlUser.add(spUser);list = new JList();list.setListData(new String[] { "" });spUser.setViewportView(list);  //必要时创建一个视口,然后设置其视图。//不直接向jscrollpaneconstructor提供视图的应用程序应该使用这个方法来指定将要在scrollpane中显示的可滚动子元素pnlUser.add(lblNotice);pnlUser.add(txtNotice);pnlUser.add(btnSend);pnlUser.add(lblUserCount);// ============主标签面板========================tpServer = new JTabbedPane(JTabbedPane.TOP);//创建一个空的选项卡窗格,其中包含指定的选项卡位置tpServer.setBackground(Color.decode("#d6f4f2"));tpServer.setFont(new Font("宋体", 0, 12));tpServer.add("服务器管理", pnlServer);tpServer.add("用户信息管理", pnlUser);this.getContentPane().add(tpServer);setVisible(true);}protected void serverMessage() {//服务器信息this.serverMessage = txtNotice.getText();txtNotice.setText("");}protected void closeServer() {//关闭服务器this.dispose();}protected void saveLog() {//保存日志try {FileOutputStream fileoutput = new FileOutputStream("log.txt",true);String temp = taMessage.getText();fileoutput.write(temp.getBytes());fileoutput.close();JOptionPane.showMessageDialog(null, "记录保存在log.txt");} catch (Exception e) {System.out.println(e);}}private void log(String string) {String newta = taMessage.getText();newta += ("\n"+string);taMessage.setText(newta);}public void actionPerformed(ActionEvent evt) {}public static void main(String args[]) {new ServerFrame();}
}

三、连接类 Connection类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Date;
import java.util.Vector;/*** Title: HappyChat聊天系统服务器程序* @version 1.0*/
public class Connection extends Thread {/*** 与客户端通讯Socket*/private Socket netClient;/*** 在线用户列表*/private Vector<Customer> userOnline;/*** 聊天信息*/private Vector<Chat> userChat;/*** 从客户到服务器 输入流*/private ObjectInputStream fromClient;/*** 传到客户端 打印流*/private PrintStream toClient;/*** 注册用户列表*/private static Vector vList = new Vector();/*** 临时对象*/private Object obj;/*** 服务器日志窗体*/private ServerFrame sFrame;@SuppressWarnings("unchecked")/*** 创建与客户端通讯*/public Connection(ServerFrame frame, Socket client, Vector u, Vector c) {netClient = client;userOnline = u;userChat = c;sFrame = frame;try {// 发生双向通信// 检索客户输入fromClient = new ObjectInputStream(netClient.getInputStream());// 服务器写到客户toClient = new PrintStream(netClient.getOutputStream());} catch (IOException e) {try {netClient.close();} catch (IOException e1) {System.out.println("不能建立流" + e1);return;}}this.start();}/*** 处理与客户端的通讯线程*/public void run() {try {// obj是Object类的对象obj = (Object) fromClient.readObject();if (obj.getClass().getName().equals("Customer")) {serverLogin();}if (obj.getClass().getName().equals("Register_Customer")) {serverRegiste();}if (obj.getClass().getName().equals("Message")) {serverMessage();}if (obj.getClass().getName().equals("Chat")) {serverChat();}if (obj.getClass().getName().equals("Exit")) {serverExit();}} catch (IOException e) {System.out.println(e);} catch (ClassNotFoundException e1) {System.out.println("读对象发生错误!" + e1);} finally {try {netClient.close();} catch (IOException e) {System.out.println(e);}}}/*** * 登录处理*/@SuppressWarnings("deprecation")public void serverLogin() {try {Customer clientMessage2 = (Customer) obj;// 读文件FileInputStream file3 = new FileInputStream("user.txt");ObjectInputStream objInput1 = new ObjectInputStream(file3);vList = (Vector) objInput1.readObject();int find = 0; // 查找判断标志for (int i = 0; i < vList.size(); i++) {Register_Customer reg = (Register_Customer) vList.elementAt(i);if (reg.custName.equals(clientMessage2.custName)) {find = 1;if (!reg.custPassword.equals(clientMessage2.custPassword)) {toClient.println("密码不正确");break;} else {// 判断是否已经登录int login_flag = 0;for (int a = 0; a < userOnline.size(); a++) {String _custName = ((Customer) userOnline.elementAt(a)).custName;if (clientMessage2.custName.equals(_custName)) {login_flag = 1;break;}}if (userOnline.size() >= 50) {toClient.println("登录人数过多,请稍候再试");break;}if (login_flag == 0) {clientMessage2.custHead = reg.head;// getUserHeadByName(clientMessage2.custName);userOnline.addElement(clientMessage2);// // 将该用户名何在toClient.println("登录成功");Date t = new Date();log("用户" + clientMessage2.custName + "登录成功,"+ "登录时间:" + t.toLocaleString() + "\n");freshServerUserList();break;} else {toClient.println("该用户已登录");}}} else {continue;}}if (find == 0) {toClient.println("没有这个用户,请先注册");}file3.close();objInput1.close();fromClient.close();} catch (ClassNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);}}/*** 刷新服务器日志窗体在线列表* */private void freshServerUserList() {String[] userList = new String[50];Customer cus = null;for (int j = 0; j < userOnline.size(); j++) {cus = (Customer) userOnline.get(j);userList[j] = cus.custName;}sFrame.list.setListData(userList);sFrame.txtNumber.setText("" + userOnline.size());sFrame.lblUserCount.setText("当前在线人数:" + userOnline.size());}/*** 注册处理*/@SuppressWarnings( { "unchecked", "deprecation" })public void serverRegiste() {try {int flag = 0; // 是否重名判断标志Register_Customer clientMessage = (Register_Customer) obj;File fList = new File("user.txt");if (fList.length() != 0)// 判断是否是第一个注册用户{ObjectInputStream objInput = new ObjectInputStream(new FileInputStream(fList));vList = (Vector) objInput.readObject();// 判断是否有重名for (int i = 0; i < vList.size(); i++) {Register_Customer reg = (Register_Customer) vList.elementAt(i);if (reg.custName.equals(clientMessage.custName)) {toClient.println("注册名重复,请另外选择");flag = 1;break;} else if (reg.custName.equals("所有人")) {toClient.println("禁止使用此注册名,请另外选择");flag = 1;break;}}}if (flag == 0) {// 添加新注册用户vList.addElement(clientMessage);// 将向量中的类写回文件FileOutputStream file = new FileOutputStream(fList);ObjectOutputStream objout = new ObjectOutputStream(file);objout.writeObject(vList);// 发送注册成功信息toClient.println(clientMessage.custName + "注册成功");Date t = new Date();log("用户" + clientMessage.custName + "注册成功, " + "注册时间:"+ t.toLocaleString() + "\n");file.close();objout.close();fromClient.close();}} catch (ClassNotFoundException e) {System.out.println(e);} catch (IOException e) {System.out.println(e);}}/*** 发送信息处理*/public void serverMessage() {try {Message mess = new Message();mess.userOnLine = userOnline;mess.chat = userChat;mess.serverMessage = "" + sFrame.serverMessage;ObjectOutputStream outputstream = new ObjectOutputStream(netClient.getOutputStream());outputstream.writeObject((Message) mess);netClient.close();outputstream.close();} catch (IOException e) {}}/*** 增加信息处理*/public void serverChat() {// 将接收到的对象值赋给聊天信息的序列化对象Chat cObj = new Chat();cObj = (Chat) obj;chatLog(cObj);// 将聊天信息的序列化对象填加到保存聊天信息的矢量中userChat.addElement((Chat) cObj);return;}/*** 用户退出处理*/@SuppressWarnings("deprecation")public void serverExit() {Exit exit = new Exit();exit = (Exit) obj;removeUser(exit);Date t = new Date();log("用户" + exit.exitname + "已经退出, " + "退出时间:" + t.toLocaleString());freshServerUserList();}/*** 在线用户中删除退出用户* * @param exit 退出用户名对象*/private void removeUser(Exit exit) {Vector<Customer> vec = new Vector<Customer>();Customer _cus = null;for (int j = 0; j < userOnline.size(); j++) {_cus = (Customer) userOnline.get(j);if (!exit.exitname.equals(_cus.custName)) {vec.add(_cus);}}userOnline.removeAllElements();for (int j = 0; j < vec.size(); j++) {_cus = (Customer) vec.get(j);userOnline.add(_cus);}}/*** 日志服务器窗体写 信息* @param log 日志信息*/public void log(String log) {String newlog = sFrame.taLog.getText() + "\n" + log;sFrame.taLog.setText(newlog);}/*** 	* 日志服务器窗体写聊天 信息* @param obj 聊天 信息对象*/@SuppressWarnings("deprecation")public void chatLog(Chat obj) {String newlog = sFrame.taMessage.getText();Date date = new Date();if (!obj.whisper) {newlog += "\n";newlog += ("[" + date.getHours() + ":" + date.getMinutes() + ":"+ date.getSeconds() + "]");newlog += obj.chatUser;newlog += "->";newlog += obj.chatToUser;newlog += ":";newlog += obj.chatMessage;}sFrame.taMessage.setText(newlog);}
}

四、聊天室登陆界面

import javax.swing.*;import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;/*** 聊天系统登录程序*/public class Login extends JFrame implements ActionListener {   //继承JFrame类,实现ActionListener接口private static final long serialVersionUID = -8965773902056088264L;//serialVersionUID 用来表明类的不同版本间的兼容性。//序列化的时候,被序列化的类要有一个唯一标记。//客户端和服务端必须需要同一个对象,serialVersionUID的唯一值判定其为同一个对象。//创建私有对象private JPanel pnlLogin;  //面板private JButton btnLogin, btnRegister, btnExit;  //登陆、注册、退出按钮private JLabel lblServer, lblUserName, lblPassword, lblLogo; //服务器、用户名、口令、背景图片private JTextField txtUserName, txtServer; //用户名及服务器地址 标签所对应的文本框private JPasswordField pwdPassword; //密码框private String strServerIp; //服务器地址对应的字符串// 用于将窗口定位private Dimension scrnsize;// dimension是Java的一个类,封装了一个构件的高度和宽度,这个类与一个构件的许多属性具有相关性,//在Component类中定义多个与之有关的方法。//getSize()和setSize(Dimension size)。分别用来获得和设置方格的大小private Toolkit toolkit = Toolkit.getDefaultToolkit(); //getDefaultToolkit();获得Toolkit对象//ToolKit 是一个抽象类,ToolKit 作为 AWT 工具箱,提供了 GUI 最底层的 Java 访问,//例如从系统获取图像、获取屏幕分辨率,获取屏幕色彩模型、//全屏的时候获得屏幕大小等/** 构造登陆窗体*/public Login() {super("登录聊天室");    //调用父类的构造函数pnlLogin = new JPanel(); //创建面板(登陆界面)//获取内容面板,因为JFrame不能直接添加组件,//需要用getContentPane()函数获取内容面板,再在内容面板上进行添加组件this.getContentPane().add(pnlLogin);//创建标签、文本框、密码框、按钮lblServer = new JLabel("服务器:");lblUserName = new JLabel("用户名:");lblPassword = new JLabel("口  令:");txtServer = new JTextField(20);txtServer.setText("127.0.0.1");//文本框中设置默认文本txtUserName = new JTextField(20);pwdPassword = new JPasswordField(20);btnLogin = new JButton("登录");btnLogin.setToolTipText("登录到服务器");//将鼠标放在按钮上出现提示 btnLogin.setMnemonic('L');//运行之后,在键盘上键入Alt+L等同于按下了button按钮btnRegister = new JButton("注册");btnRegister.setToolTipText("注册新用户");//将鼠标放在按钮上出现提示btnRegister.setMnemonic('R');//运行之后,在键盘上键入Alt+L等同于按下了button按钮btnExit = new JButton("退出");	btnExit.setToolTipText("退出系统");//将鼠标放在按钮上出现提示btnExit.setMnemonic('X');//运行之后,在键盘上键入Alt+L等同于按下了button按钮/* **********************************************************************     该布局采用手动布局 setBounds设置组件位置*  setFont设置字体、字型、字号*  setForeground设置文字的颜色 **  setBackground设置背景色*  setOpaque将背景设置为透明*/pnlLogin.setLayout(null); // 组件用手动布局pnlLogin.setBackground(new Color(211, 211, 211));//安排组件位置lblServer.setBounds(50, 100, 100, 30);txtServer.setBounds(150, 100, 120, 25);lblUserName.setBounds(50, 130, 100, 30);txtUserName.setBounds(150, 130, 120, 25);lblPassword.setBounds(50, 160, 100, 30);pwdPassword.setBounds(150, 160, 120, 25);btnLogin.setBounds(50, 200, 80, 25);btnRegister.setBounds(130, 200, 80, 25);btnExit.setBounds(210, 200, 80, 25);Font fontstr = new Font("楷体", Font.PLAIN, 20);//创建字体模板//设置字体样式lblServer.setFont(fontstr);txtServer.setFont(fontstr);lblUserName.setFont(fontstr);txtUserName.setFont(fontstr);lblPassword.setFont(fontstr);pwdPassword.setFont(fontstr);btnLogin.setFont(fontstr);btnRegister.setFont(fontstr);btnExit.setFont(fontstr);//设置组件颜色lblUserName.setForeground(Color.BLACK);lblPassword.setForeground(Color.BLACK);btnLogin.setBackground(new Color(255,255,255));btnRegister.setBackground(new Color(255,255,255));btnExit.setBackground(new Color(255,255,255));//向登陆面板中添加组件pnlLogin.add(lblServer);pnlLogin.add(txtServer);pnlLogin.add(lblUserName);pnlLogin.add(txtUserName);pnlLogin.add(lblPassword);pnlLogin.add(pwdPassword);pnlLogin.add(btnLogin);pnlLogin.add(btnRegister);pnlLogin.add(btnExit);// 设置背景图片Icon logo1 = new ImageIcon("images\\loginlogo.jpg");lblLogo = new JLabel(logo1);lblLogo.setBounds(0, 0, 340, 66);pnlLogin.add(lblLogo);// 设置登录窗口setResizable(false);		//不可改变窗口大小setSize(340, 260); 		   //设置窗口大小setVisible(true);scrnsize = toolkit.getScreenSize();    		//获取电脑屏幕大小setLocation(scrnsize.width / 2 - this.getWidth() / 2, scrnsize.height/ 2 - this.getHeight() / 2); 		//将窗口定位Image img = toolkit.getImage("images\\appico.jpg");setIconImage(img);	   //设置窗口图标// 三个按钮注册监听btnLogin.addActionListener(this);btnRegister.addActionListener(this);btnExit.addActionListener(this);} // 构造方法结束/*** 按钮监听响应*/@SuppressWarnings({ "static-access", "deprecation" })public void actionPerformed(ActionEvent ae) {Object source = ae.getSource();if (source.equals(btnLogin)) {// 判断用户名和密码是否为空if (txtUserName.getText().equals("")||pwdPassword.getText().equals("")) {JOptionPane op1 = new JOptionPane();op1.showMessageDialog(null, "用户名或密码不能为空");} else {strServerIp = txtServer.getText();login();}}if (source.equals(btnRegister)) {strServerIp = txtServer.getText();this.dispose();new Register(strServerIp);}if (source == btnExit) {System.exit(0);}} // actionPerformed()结束/** * 登录事件响应方法*/@SuppressWarnings("deprecation")public void login() {// 接受客户的详细资料Customer data = new Customer();data.custName = txtUserName.getText();data.custPassword = pwdPassword.getText();try {// 连接到服务器Socket toServer;toServer = new Socket(strServerIp, 1001);ObjectOutputStream streamToServer = new ObjectOutputStream(toServer.getOutputStream());// 写客户详细资料到服务器socketstreamToServer.writeObject((Customer) data);// 读来自服务器socket的登录状态BufferedReader fromServer = new BufferedReader(new InputStreamReader(toServer.getInputStream()));String status = fromServer.readLine();if (status.equals("登录成功")) {new ChatRoom((String) data.custName, strServerIp);this.dispose();// 关闭流对象streamToServer.close();fromServer.close();toServer.close();} else {JOptionPane.showMessageDialog(null, status);// 关闭流对象streamToServer.close();fromServer.close();toServer.close();}} catch (ConnectException e1) {JOptionPane.showMessageDialog(null, "未能建立到指定服务器的连接!");} catch (InvalidClassException e2) {JOptionPane.showMessageDialog(null, "类错误!");} catch (NotSerializableException e3) {JOptionPane.showMessageDialog(null, "对象未序列化!");} catch (IOException e4) {JOptionPane.showMessageDialog(null, "不能写入到指定服务器!");}} // login()结束/*** 启动登陆窗体* @param args*/public static void main(String args[]) {new Login();}} // Class Login结束

五、聊天室注册界面

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InvalidClassException;
import java.io.NotSerializableException;
import java.io.ObjectOutputStream;
import java.net.Socket;import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;/*<p>Title:HappyChat聊天系统用户注册程序</p>*@version 1.0*/public class Register extends JFrame  implements ActionListener
{private JComboBox comboBox;private static final long serialVersionUID = 9019746127517522180L;JPanel  pnlRegister;JLabel  lblUserName,lblGender,lblAge;JLabel  lblPassword,lblConfirmPass,lblEmail,logoPosition;JTextField  txtUserName,txtAge,txtEmail;JPasswordField  pwdUserPassword,pwdConfirmPass;JRadioButton  rbtnMale,rbtnFemale;ButtonGroup  btngGender;JButton  btnOk,btnCancel,btnClear;String  strServerIp;final JLabel headLabel = new JLabel();//用于将窗口用于定位Dimension scrnsize;Toolkit toolkit=Toolkit.getDefaultToolkit();//构造方法public Register(String ip){super("聊天室注册窗口");strServerIp=ip;pnlRegister=new JPanel();this.getContentPane().add(pnlRegister);lblUserName=new JLabel("用 户 名:");lblGender=new JLabel("性    别:");lblAge=new JLabel("年    龄:");lblPassword=new JLabel("口    令:");lblConfirmPass=new JLabel("确认口令:");lblEmail=new JLabel("电子邮件:");txtUserName=new JTextField(30);txtEmail=new JTextField(30);txtAge=new JTextField(10);pwdUserPassword=new JPasswordField(30);pwdConfirmPass=new JPasswordField(30);rbtnMale=new JRadioButton("男",true);rbtnFemale=new JRadioButton("女");btngGender=new ButtonGroup();btnOk=new JButton("确定");btnOk.setMnemonic('O');btnOk.setToolTipText("保存注册信息");btnCancel=new JButton("返回");btnCancel.setMnemonic('B');btnCancel.setToolTipText("返回登录窗口");btnClear=new JButton("清空");btnClear.setMnemonic('L');btnClear.setToolTipText("清空注册信息");/*  该布局采用手动布局           ** setBounds设置组件位置        **  setFont设置字体、字型、字号  ** setForeground设置文字的颜色  **  setBackground设置背景色      **  setOpaque将背景设置为透明    */pnlRegister.setLayout(null);    //组件用手动布局pnlRegister.setBackground(Color.decode("#90d7ec"));lblUserName.setBounds(30,80,100,30);txtUserName.setBounds(110,85,120,20);lblPassword.setBounds(30,141,100,30);pwdUserPassword.setBounds(110,146,120,20);lblConfirmPass.setBounds(30,166,100,30);pwdConfirmPass.setBounds(110,171,120,20);lblGender.setBounds(30,191,100,30);rbtnMale.setBounds(110,196,60,20);rbtnFemale.setBounds(190,196,60,20);lblAge.setBounds(30,216,100,30);txtAge.setBounds(110,221,120,20);lblEmail.setBounds(30,241,100,30);txtEmail.setBounds(110,246,120,20);btnOk.setBounds(246,166,80,25);	btnCancel.setBounds(246,201,80,25);btnClear.setBounds(246,241,80,25);Font fontstr=new Font("宋体",Font.PLAIN,13);	lblUserName.setFont(fontstr);lblGender.setFont(fontstr);lblPassword.setFont(fontstr);lblConfirmPass.setFont(fontstr);lblAge.setFont(fontstr);lblEmail.setFont(fontstr);rbtnMale.setFont(fontstr);rbtnFemale.setFont(fontstr);txtUserName.setFont(fontstr);txtEmail.setFont(fontstr);	btnOk.setFont(fontstr);btnCancel.setFont(fontstr);btnClear.setFont(fontstr);lblUserName.setForeground(Color.BLACK);lblGender.setForeground(Color.BLACK);lblPassword.setForeground(Color.BLACK);lblAge.setForeground(Color.BLACK);lblConfirmPass .setForeground(Color.BLACK);lblEmail.setForeground(Color.BLACK);rbtnMale.setForeground(Color.BLACK);rbtnFemale.setForeground(Color.BLACK);rbtnMale.setBackground(Color.white);rbtnFemale.setBackground(Color.white);btnOk.setBackground(new Color(255,235,205));	btnCancel.setBackground(new Color(255,235,205));btnClear.setBackground(new Color(255,235,205));rbtnMale.setOpaque(false);   rbtnFemale.setOpaque(false);pnlRegister.add(lblUserName);pnlRegister.add(lblGender);pnlRegister.add(lblPassword);pnlRegister.add(lblConfirmPass);pnlRegister.add(lblEmail);pnlRegister.add(lblAge);pnlRegister.add(txtAge);pnlRegister.add(txtUserName);pnlRegister.add(txtEmail);pnlRegister.add(pwdUserPassword);pnlRegister.add(pwdConfirmPass);pnlRegister.add(btnOk);pnlRegister.add(btnCancel);pnlRegister.add(btnClear);pnlRegister.add(rbtnMale);pnlRegister.add(rbtnFemale);btngGender.add(rbtnMale);btngGender.add(rbtnFemale);//设置背景图片Icon logo = new ImageIcon("images\\registerlogo.jpg");logoPosition = new JLabel(logo);logoPosition.setBounds(0, 0, 360,78);pnlRegister.add(logoPosition);this.setSize(360,313);this.setVisible(true);this.setResizable(false);//将窗口定位在屏幕中央scrnsize=toolkit.getScreenSize();this.setLocation(scrnsize.width/2-this.getWidth()/2,scrnsize.height/2-this.getHeight()/2);Image img=toolkit.getImage("images\\appico.jpg");this.setIconImage(img);//三个按钮注册监听btnOk    .addActionListener(this);btnCancel.addActionListener(this);btnClear   .addActionListener(this);final JLabel label = new JLabel();label.setText("头    像:");label.setBounds(30, 120, 60, 15);pnlRegister.add(label);comboBox = new JComboBox();comboBox.setAutoscrolls(true);comboBox.setModel(new DefaultComboBoxModel(new String[] {"4","5","6","7","8","9","10"}));comboBox.setBounds(110, 116, 47, 23);comboBox.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent arg0) {Icon logo = new ImageIcon("face\\"+comboBox.getSelectedItem().toString()+".jpg");headLabel.setIcon(logo);}});pnlRegister.add(comboBox);headLabel.setHorizontalAlignment(SwingConstants.CENTER);headLabel.setIcon(new ImageIcon("face//10.jpg"));headLabel.setBounds(247, 88, 74, 72);pnlRegister.add(headLabel);}  //构造方法结束//按钮监听响应public void actionPerformed(ActionEvent ae){Object source=new Object();source=ae.getSource();if (source.equals(btnOk))      //"确定"按钮{register();}if (source.equals(btnCancel))  //"返回"按钮{new Login();this.dispose();}if (source.equals(btnClear))     //"清空"按钮{txtUserName.setText("");pwdUserPassword.setText("");pwdConfirmPass.setText("");txtAge.setText("");txtEmail.setText("");	}}  //actionPerformed()结束//////////"确定"按钮事件响应//////////@SuppressWarnings({ "deprecation", "static-access" })public void register(){//接受客户的详细资料Register_Customer data=new Register_Customer();data.custName     = txtUserName.getText();data.custPassword = pwdUserPassword.getText();data.age          = txtAge.getText();data.sex          = rbtnMale.isSelected()?"男":"女";data.email        = txtEmail.getText();//chenmindata.head		  = comboBox.getSelectedItem().toString();//验证用户名是否为空if(data.custName.length()==0){JOptionPane.showMessageDialog(null,"用户名不能为空");	return;	}//验证密码是否为空if(data.custPassword.length()==0){JOptionPane.showMessageDialog(null,"密码不能为空");	return;	}//验证密码的一致性if(!data.custPassword.equals(pwdConfirmPass.getText())){JOptionPane.showMessageDialog(null,"密码两次输入不一致,请重新输入");	return;}//验证年龄是否为空if(data.age.length()==0){JOptionPane.showMessageDialog(null,"年龄不能为空");	return;	}//验证年龄的合法性int age=Integer.parseInt(txtAge.getText());if (age<=0||age>100){JOptionPane.showMessageDialog(null,"年龄输入不合法");return;}//验证Email的正确性int Found_flag=0;    //判断标志for (int i=0;i<data.email.length();i++){if(data.email.charAt(i)=='@'){Found_flag++;	}	}if(Found_flag!=1){JOptionPane.showMessageDialog(null,"电子邮箱格式不正确,请重新输入");	return;	}try{//连接到服务器Socket toServer;toServer = new Socket(strServerIp,1001);ObjectOutputStream streamToServer=new ObjectOutputStream (toServer.getOutputStream());					//写客户详细资料到服务器socketstreamToServer.writeObject((Register_Customer)data);//读来自服务器socket的登陆状态BufferedReader fromServer=new BufferedReader(new InputStreamReader(toServer.getInputStream()));String status=fromServer.readLine();//显示成功消息JOptionPane op=new JOptionPane();op.showMessageDialog(null,status);if(status.equals(data.custName+"注册成功")){txtUserName.setText("");pwdUserPassword.setText("");pwdConfirmPass.setText("");txtAge.setText("");txtEmail.setText("");}//关闭流对象streamToServer.close();fromServer.close();}catch(InvalidClassException e1){JOptionPane.showMessageDialog(null,"类错误!");}catch(NotSerializableException e2){JOptionPane.showMessageDialog(null,"对象未序列化!");}catch(IOException e3){JOptionPane.showMessageDialog(null,"不能写入到指定服务器!");}}  //方法register()结束public static void main(String args[]){new Register("127.0.0.1");}}  //class Register 结束

六、可序列化Chat类

import java.io.Serializable;public class Chat implements Serializable
{private static final long serialVersionUID = 4058485121419391969L;/*** 发言人用户名*/public String chatUser;/*** 聊天内容*/public String chatMessage;/*** 接受对象用户名*/public String chatToUser;/*** 是否私聊*/public boolean whisper;
}

七、聊天室界面

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import java.util.*;public class ChatRoom extends Thread implements ActionListener {static JFrame frmChat;JPanel pnlChat;JButton btnCls, btnExit, btnSend, btnClear, btnSave;JLabel lblUserList, lblUserMessage, lblSendMessage, lblChatUser;JLabel lblUserTotal, lblCount, lblBack;JTextField txtMessage;java.awt.List lstUserList;TextArea taUserMessage;JComboBox cmbUser;//JCheckBox chPrivateChat;String strServerIp, strLoginName;Thread thread;final JLabel headLabel = new JLabel();Dimension scrnsize;Toolkit toolkit = Toolkit.getDefaultToolkit();Message messobj = null;String serverMessage = "";// 构造方法public ChatRoom(String name, String ip) {strServerIp = ip;     //服务器地址strLoginName = name; //用户登陆名frmChat = new JFrame("聊天室" + "[用户:" + name + "]");pnlChat = new JPanel(); //创建带有双缓冲区和流布局的新JPanel。frmChat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口可关闭frmChat.getContentPane().add(pnlChat);Font fntDisp1 = new Font("宋体", Font.PLAIN, 13);  //聊天室上方字体Font fntDisp2 = new Font("楷体", Font.PLAIN, 16); //聊天室左下方字体Font fntDisp3 = new Font("楷体", Font.PLAIN, 16);  //聊天室右下方字体String list[] = { "所有人" };  //默认对所有人发起会话btnCls = new JButton("清屏");btnExit = new JButton("退出");btnSend = new JButton("发送");btnSave = new JButton("保存");lblUserList = new JLabel("【在线用户列表】");lblUserMessage = new JLabel("【聊天信息】");lblSendMessage = new JLabel("聊天内容:");lblChatUser = new JLabel("你对:");lblUserTotal = new JLabel("在线人数:");lblCount = new JLabel("0");lstUserList = new java.awt.List();txtMessage = new JTextField(170);cmbUser = new JComboBox(list);cmbUser.addItemListener(new ItemListener() {public void itemStateChanged(ItemEvent arg0) {freshHead();}});//chPrivateChat = new JCheckBox("私聊");       // 创建私聊按钮,用于双方对话taUserMessage = new TextArea("", 300, 200,TextArea.SCROLLBARS_VERTICAL_ONLY);// 只能向下滚动taUserMessage.setForeground(new Color(0, 0, 0));taUserMessage.setEditable(false);        // 不可写入pnlChat.setLayout(null); //清空布局管理器,常用于窗体大小固定的容器里 ,绝对布局:组件的位置和形状不会随窗口的改变而发生变化pnlChat.setBackground(new Color(176,196,222));  //设置面板背景颜色btnSave.setBounds(500, 330, 80, 25);btnCls.setBounds(400, 330, 80, 25);btnExit.setBounds(500, 360, 80, 25);btnSend.setBounds(500, 300, 80, 25);lblUserList.setBounds(5, 0, 120, 40);lblUserTotal.setBounds(130, 0, 60, 40);lblCount.setBounds(190, 0, 60, 40);lblUserMessage.setBounds(225, 0, 180, 40);lblChatUser.setBounds(6, 295, 40, 40);lblSendMessage.setBounds(195, 295, 78, 40);lstUserList.setBounds(5, 40, 210, 255);taUserMessage.setBounds(225, 40, 360, 255);txtMessage.setBounds(270, 300, 210, 25);cmbUser.setBounds(50, 300, 80, 25);//chPrivateChat.setBounds(333, 336, 60, 20);私聊btnCls.setFont(fntDisp3);btnExit.setFont(fntDisp3);btnSend.setFont(fntDisp3);btnSave.setFont(fntDisp3);lblUserList.setFont(fntDisp1);lblUserMessage.setFont(fntDisp1);lblChatUser.setFont(fntDisp2);lblSendMessage.setFont(fntDisp2);lblUserTotal.setFont(fntDisp1);lblCount.setFont(fntDisp1);cmbUser.setFont(fntDisp1);//chPrivateChat.setFont(fntDisp1);lblUserList.setForeground(Color.YELLOW);lblUserMessage.setForeground(Color.YELLOW);lblSendMessage.setForeground(Color.black);lblChatUser.setForeground(Color.black);lblSendMessage.setForeground(Color.black);lblUserTotal.setForeground(Color.YELLOW);lblCount.setForeground(Color.YELLOW);cmbUser.setForeground(Color.black);//chPrivateChat.setForeground(Color.black);lstUserList.setBackground(Color.white);taUserMessage.setBackground(Color.white);btnCls.setBackground(Color.ORANGE);btnExit.setBackground(Color.ORANGE);btnSend.setBackground(Color.PINK);btnSave.setBackground(Color.ORANGE);pnlChat.add(btnCls);pnlChat.add(btnExit);pnlChat.add(btnSend);pnlChat.add(btnSave);pnlChat.add(lblUserList);pnlChat.add(lblUserMessage);pnlChat.add(lblSendMessage);pnlChat.add(lblChatUser);pnlChat.add(lblUserTotal);pnlChat.add(lblCount);pnlChat.add(lstUserList);pnlChat.add(taUserMessage);pnlChat.add(txtMessage);pnlChat.add(cmbUser);//pnlChat.add(chPrivateChat);frmChat.addWindowListener(new Windowclose());btnCls.addActionListener(this);btnExit.addActionListener(this);btnSend.addActionListener(this);btnSave.addActionListener(this);lstUserList.addActionListener(this);txtMessage.addActionListener(this);//导入聊天室标题图标headLabel.setHorizontalAlignment(SwingConstants.CENTER);headLabel.setIcon(new ImageIcon("face//1.jpg"));headLabel.setBounds(15, 330, 70, 60);pnlChat.add(headLabel);// 启动聊天页面信息刷新线程Thread thread = new Thread(this);thread.start();//设置窗口 大小、可见、大小不可调frmChat.setSize(600, 420);frmChat.setVisible(true);frmChat.setResizable(false);// 将窗口定位在屏幕中央scrnsize = toolkit.getScreenSize();frmChat.setLocation(scrnsize.width / 2 - frmChat.getWidth() / 2,scrnsize.height / 2 - frmChat.getHeight() / 2);Image img = toolkit.getImage("images\\appico.jpg");frmChat.setIconImage(img);} // 构造方法结束@SuppressWarnings("deprecation")public void run() {int intMessageCounter = 0;int intUserTotal = 0;boolean isFirstLogin = true; // 判断是否刚登陆boolean isFound; // 判断是否找到用户Vector user_exit = new Vector();try {for (;;) {Socket toServer;toServer = new Socket(strServerIp, 1001);// 将信息发往服务器messobj = new Message();ObjectOutputStream streamtoserver = new ObjectOutputStream(toServer.getOutputStream());streamtoserver.writeObject((Message) messobj);// 收来自服务器的信息ObjectInputStream streamfromserver = new ObjectInputStream(toServer.getInputStream());messobj = (Message) streamfromserver.readObject();// //////刷新聊天信息列表//////////if (isFirstLogin) // 如果刚登陆{intMessageCounter = messobj.chat.size(); // 屏蔽该用户登陆前的聊天内容isFirstLogin = false;}if (!serverMessage.equals(messobj.serverMessage)) {serverMessage = messobj.serverMessage;taUserMessage.append("[系统消息]:" + serverMessage+"\n");}for (int i = intMessageCounter; i < messobj.chat.size(); i++) {Chat temp = (Chat) messobj.chat.elementAt(i);String temp_message; //系统提示语if (temp.chatUser.equals(strLoginName)) {if (temp.chatToUser.equals(strLoginName)) {  //temp_message = "系统提示您:请不要自言自语!" + "\n";} else {if (!temp.whisper) // 不是悄悄话{temp_message = "【你】对【" + temp.chatToUser + "】"+ "说:" + temp.chatMessage+ "\n";} else {temp_message = "【你】悄悄对【" + temp.chatToUser+ "】" + "说:" + temp.chatMessage+ "\n";}}} else {if (temp.chatToUser.equals(strLoginName)) {if (!temp.whisper) // 不是悄悄话{temp_message = "【" + temp.chatUser + "】对【你】"+ "说:" + temp.chatMessage+ "\n";} else {temp_message = "【" + temp.chatUser + "】悄悄对【你】"+ "说:" + temp.chatMessage+ "\n";}} else {if (!temp.chatUser.equals(temp.chatToUser)) // 对方没有自言自语{if (!temp.whisper) // 不是悄悄话{temp_message = "【" + temp.chatUser + "】对【"+ temp.chatToUser + "】"+ "说:" + temp.chatMessage + "\n";} else {temp_message = "";}} else {temp_message = "";}}}taUserMessage.append(temp_message);intMessageCounter++;}// //////刷新在线用户//////////lstUserList.clear();for (int i = 0; i < messobj.userOnLine.size(); i++) {String User = ((Customer) messobj.userOnLine.elementAt(i)).custName;lstUserList.addItem(User);}Integer a = new Integer(messobj.userOnLine.size());lblCount.setText(a.toString());// 显示用户进入聊天室的信息if (messobj.userOnLine.size() > intUserTotal) {String tempstr = ((Customer) messobj.userOnLine.elementAt(messobj.userOnLine.size() - 1)).custName;if (!tempstr.equals(strLoginName)) {taUserMessage.append("【" + tempstr + "】来了" + "\n");}}if (messobj.userOnLine.size() < intUserTotal) {for (int b = 0; b < user_exit.size(); b++) {isFound = false;for (int c = 0; c < messobj.userOnLine.size(); c++) {String tempstr = ((Customer) user_exit.elementAt(b)).custName;if (tempstr.equals(((Customer) messobj.userOnLine.elementAt(c)).custName)) {isFound = true;break;}}if (!isFound) // 没有发现该用户{String tempstr = ((Customer) user_exit.elementAt(b)).custName;if (!tempstr.equals(strLoginName)) {taUserMessage.append("【" + tempstr + "】走了"+ "\n");}}}}user_exit = messobj.userOnLine;intUserTotal = messobj.userOnLine.size();streamtoserver.close();streamfromserver.close();toServer.close();Thread.sleep(3000);}} catch (Exception e) {@SuppressWarnings("unused")JOptionPane jop = new JOptionPane();JOptionPane.showMessageDialog(null, "不能连接服务器!");e.printStackTrace();frmChat.dispose();}} // run()结束private void exitChatRoom() {exit();}// /////////监听按钮响应//////////////public void actionPerformed(ActionEvent ae) {Object source = (Object) ae.getSource();if (source.equals(btnCls)) {clearMessage();}if (source.equals(btnExit)) {exit();}if (source.equals(btnSend)) {sendMessage();}if (source.equals(btnSave)) {saveMessage();}if (source.equals(lstUserList)) // 双击列表框{changeUser();}} // actionPerformed()结束// /////////监听窗口关闭响应//////////////class Windowclose extends WindowAdapter {public void windowClosing(WindowEvent e) {exit();}}// "清屏"按钮public void clearMessage() {taUserMessage.setText("");}// "退出"按钮public void exit() {Exit exit = new Exit();exit.exitname = strLoginName;// 发送退出信息try {Socket toServer = new Socket(strServerIp, 1001);// 向服务器发送信息ObjectOutputStream outObj = new ObjectOutputStream(toServer.getOutputStream());outObj.writeObject(exit);outObj.close();toServer.close();frmChat.dispose();} catch (Exception e) {}} // exit()结束// "发送"按钮public void sendMessage() {Chat chatobj = new Chat();chatobj.chatUser = strLoginName;chatobj.chatMessage = txtMessage.getText();chatobj.chatToUser = String.valueOf(cmbUser.getSelectedItem());//chatobj.whisper = chPrivateChat.isSelected() ? true : false;try {Socket toServer = new Socket(strServerIp, 1001);ObjectOutputStream outObj = new ObjectOutputStream(toServer.getOutputStream());outObj.writeObject(chatobj);txtMessage.setText(""); // 清空文本框outObj.close();toServer.close();} catch (Exception e) {}}// "保存"按钮public void saveMessage() {try {FileOutputStream fileoutput = new FileOutputStream(this.strLoginName + "_message.txt", true);String temp = taUserMessage.getText();fileoutput.write(temp.getBytes());fileoutput.close();JOptionPane.showMessageDialog(null, "聊天记录保存在" + this.strLoginName+ "_message.txt");} catch (Exception e) {System.out.println(e);}}// 将所选用户添加到cmbUser中public void changeUser() {boolean key = true;String selected = lstUserList.getSelectedItem();for (int i = 0; i < cmbUser.getItemCount(); i++) {if (selected.equals(cmbUser.getItemAt(i))) {key = false;break;}}if (key == true) {cmbUser.insertItemAt(selected, 0);}String head = getUserHead(lstUserList.getSelectedItem());cmbUser.setSelectedItem(selected);headLabel.setIcon(new ImageIcon("face//" + head + ".JPG"));}protected void freshHead() {String head = getUserHead(cmbUser.getSelectedItem().toString());headLabel.setIcon(new ImageIcon("face//" + head + ".JPG"));}private String getUserHead(String selectedItem) {String head = "oo";for (int i = 0; i < messobj.userOnLine.size(); i++) {String User = ((Customer) messobj.userOnLine.elementAt(i)).custName;head = ((Customer) messobj.userOnLine.elementAt(i)).custHead;if (User.equals(selectedItem)) {break;}}return head;}public static void main(String args[]) {new ChatRoom("测试用户", "127.0.0.1");}}

八、从客户端进入聊天室程序

public class ChatClient
{public ChatClient(){}public static void main(String args[]){new Login();}
}

九、存储用户信息类

public class Customer extends Object implements java.io.Serializable
{private static final long serialVersionUID = -9215977405584592618L;/*** 用户名*/public String custName;/*** 密码*/public String custPassword;/*** 用户头像*/public String custHead;
}

十、退出用户名

import java.io.Serializable;public class Exit implements Serializable
{private static final long serialVersionUID = -5267537916643834426L;/*** 退出者用户名*/public String exitname;	
}

十一、可序列化用户对象及聊天信息

import java.io.Serializable;
import java.util.Vector;public class Message implements Serializable {private static final long serialVersionUID = -3831507106408529855L;/*** 用户在线对象集*/public Vector userOnLine;/*** 聊天信息集*/public Vector chat;/*** 公告*/public String serverMessage;}

十二、可序列化用户注册信息

public class Register_Customer extends Object implements java.io.Serializable
{private static final long serialVersionUID = 7116984729771538742L;/*** 姓名*/public String custName;/*** 密码*/public String custPassword;/*** 年龄*/	public String age;/*** 性别*/	public String sex;/*** 电子邮件*/public String email;/*** 头像文件名*/public String head;}

参考链接:https://wenku.baidu.com/view/1e158ef774a20029bd64783e0912a21614797fe8.html

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. CECBC区块链专委会副主任吴桐受邀成为伏羲智库兼职研究员

    7月17日,伏羲智库邀请中央财经大学金融学博士、商务部CECBC区块链专家会副主任、数字经济商学院院长、区块链及数字经济领域知名学者吴桐,受邀在伏羲书院主讲“大国博弈视角下的数字货币和区块链”主题讲座。伏羲智库创始人李晓东教授、秘书长孙永革女士以及智库区块链相关人…...

    2024/5/8 12:37:57
  2. Spring事件监听模型

    1.案例 在开始分析前我们先来段小demo,便于后面分析。环境准备 Java、Maven、Spring工具 IDEA、电脑Spring框架主要提供了ApplicationListener事件监听,只需要我们手动实现该接口就能达到监听效果。 (1)我们首先定义一个Car实体类,提供一个无参构造 public class Car {publ…...

    2024/4/25 1:31:16
  3. 从入门到进阶,那些让你看了以后大呼过瘾的数据分析六件套

    平时经常收到邀请回答或者微信私下询问,数据分析该怎么去学、我要转行数据分析该学习什么书籍、我是否适合从事数据分析这个岗位、数据分析进阶方法、…。看到这些提问后感慨万千,自己也是两眼一抹黑的走上了数据领域这条道路,从业务运营,到数据分析、数据挖掘,数据处理工…...

    2024/4/17 14:24:37
  4. 机器学习西瓜书笔记(四)--------------决策树

    目录决策树算法ID3决策树C4.5决策树CART决策树决策树剪枝决策树学习是根据数据的属性采用树状结构建立的一种决策模型,可以用此模型解决分类和回归问题。常见的算法包括 ,ID3, C4.5,CART(Classification And Regression Tree)等。我们往往根据数据集来构建一棵决策树,他的一…...

    2024/5/8 16:33:25
  5. python网络爬虫——Scrapy中selenium的使用

    引入 在通过scrapy框架进行某些网站数据爬取的时候,往往会碰到页面动态数据加载的情况发生,如果直接使用scrapy对其url发请求,是绝对获取不到那部分动态加载出来的数据值。但是通过观察我们会发现,通过浏览器进行url请求发送则会加载出对应的动态加载出的数据。那么如果我们…...

    2024/4/9 0:03:07
  6. 还不了解Bean的生命周期?

    Bean的生命周期 要求有spring基础 了解过一些设计模式1.什么是生命周期? 生命周期就是指一个对象的生老病死,在bean中简单的说就是bean的创建到初始化再到bean的销毁的过程(创建->初始化->销毁),然而在spring中正是由spring容器帮我们管理着bean的生命周期,同时也给…...

    2024/4/9 0:03:04
  7. 机器学习-随机森林

    什么是随机森林 随机森林是一种由决策树构成的集成算法,在很多情况下都有不错的表现。在解释随机森林前,需要先提一下决策树。决策树是一种很简单的算法,他的解释性强,也符合人类的直观思维,是一种基于if-then-else规则的监督学习算法。 随机森林是由很多决策树构成的,不…...

    2024/5/8 23:32:23
  8. JavaSE学习笔记-----第二章基础语法

    关键字和保留字: 关键字 keyword所有的关键字都是小写的 保留字 reserved word 现有java版本尚未使用,以后版本可能使用 goto const 标识符:Identifier 类名 变量名 方法名 接口名 包名 规则:由字母、数字、下划线 或者$ 组成 数字不可以开头 不可以使用关键字和保留字 …...

    2024/4/9 0:03:02
  9. 算法题4||快乐数判断

    题目 编写一个算法来判断一个数 n 是不是快乐数。 「快乐数」定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。如果 可以变为 1,那么这个数就是快乐数。 如果 n 是快乐数就返回…...

    2024/4/9 0:03:01
  10. centos 8.2.2004 窗口没有最大化最小化按钮只有关闭按钮

    解决办法:下载安装 gnome-tweaks :yum install gnome-tweaks执行 gnome-tweaks命令...

    2024/5/3 21:36:57
  11. 如何更换Windows中命令提示符(cmd)中的字体

    前言 CMD(命令提示符),全称“Command Prompt”;对于这个东西我相信大部分用电脑的人基本都知道,因为常常会用到一些基本的DOS命令进行一些电脑的基本查看处理;但是我相信一些用这个东西的人其实有一种苦恼,那就是“字体”;这个“字体”不管是在win7还是win10中,它的样…...

    2024/4/23 7:11:48
  12. 外设驱动库开发笔记10:SHT2x系列温湿度传感器驱动

    温湿度检测是嵌入式编程中经常应用到的一项功能。在我们的产品中亦经常使用。SHT2x系列温湿度传感器作为一种高精度低成本的集成模块,一直应用于我们的产品中。在这里我们讨论如何封装SHT2x系列温湿度传感器的驱动。1、功能概述SHT20配有一个全新设计的CMOSens芯片、一个经过改…...

    2024/5/4 17:57:42
  13. Leetcode刷题——搜索插入位置

    文章链接:Leetcode刷题——搜索插入位置...

    2024/4/28 1:48:12
  14. 深度卷积生成对抗网络(DCGAN)来生成对抗图像

    DCGAN 实现深度卷积生成对抗网络(DCGAN)来生成对抗图像 图来源网络main.pyimport os import numpy as np import tensorflow as tf from tensorflow import keras from scipy.misc import toimagefrom gen import Generator, Discriminatordef save_result(val_out…...

    2024/4/8 22:40:33
  15. ++i 与 i++

    ++是单目运算符,++i与i++不同的本质在于优先级的问题。 一、只有一个运算符时 当只有一个运算符时,即只有++运算时,不管是++i还是i++,它的作用都是自增1。 ++i情形时代码如下: int i = 10; ++i; System.out.println("i = " + i); // 11i++情形时代码如下: int…...

    2024/4/24 3:37:05
  16. 2020牛客多校第三场F. Fraction Construction Problem(exgcd)

    F.F.F. 题意: 给定a,ba,ba,b,求出满足cd−ef=ab\frac{c}{d}-\frac{e}{f}=\frac{a}{b}dc​−fe​=ba​的任意一组c,d,e,fc,d,e,fc,d,e,f。 数据范围:a,b≤2106a,b\leq 2\times 10^6a,b≤2106,1≤d<b,f<b1\leq d<b,f<b1≤d<b,f<b,且1≤c,e≤410121\leq c,…...

    2024/4/21 3:18:43
  17. 如何(正确)使用搜索引擎?使用搜索引擎的高效技巧(例如:百度、谷歌)

    前言 提起这个搜索引擎,我们对它就有三种级别的认识第一种:完全不知道“搜索引擎”是什么或者是“我只知道浏览器” 第二种:知道搜索引擎,但不知道这玩意还有使用方式! 第三种:知道搜索引擎并知道怎么使用的大量相关知识。而最近我发现,周围的小伙伴好像都不是对这个有太…...

    2024/5/3 17:05:49
  18. eclipse中如何查看程序源码

    在eclipse中如何查看程序源码 1.查看方法,按下Ctrl键同时,用鼠标指向自己想要查看源码的关键字,该字会出现下划线,单击左键,就可以进入源码查看了 2.在此前,我们需要配置一下eclipse,将src.zip的压缩包导入进去 3.点击Window–>preferences在左侧找到Java–>insta…...

    2024/4/13 4:49:35
  19. 题312. 戳气球

    C++class Solution { public:int maxCoins(vector<int>& nums) {int n = nums.size();nums.insert(nums.begin(), 1);nums.push_b...

    2024/4/25 9:25:22
  20. LeetCode刷题——合并区间

    文章链接:LeetCode刷题——合并区间...

    2024/4/28 23:49:43

最新文章

  1. 修改el-checkbox样式

    一定要在最外层&#xff1b; //未选中框/deep/ .el-checkbox__inner{border-color: #0862a3;}//选中框/deep/ .el-checkbox__input.is-checked .el-checkbox__inner{background-color: #0862a3;border-color: #0862a3;}//未选中框时右侧文字/deep/ .el-checkbox__label{}//选中…...

    2024/5/9 3:51:50
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/5/7 10:36:02
  3. 学透Spring Boot — 004. Spring Boot Starter机制和自动配置机制

    如果你项目中一直用的是 Spring Boot&#xff0c;那么恭喜你没有经历过用 Spring 手动集成其它框架的痛苦。 都说 Spring Boot 大大简化了 Spring 框架开发 Web 应用的难度&#xff0c;这里我们通过配置 Hibernate 的两种方式来深刻体会这一点&#xff1a; 使用 Spring 框架集…...

    2024/4/30 6:47:33
  4. 第十三届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组 题解

    VP比赛链接 : 数据加载中... - 蓝桥云课 1 . 九进制 转 十进制 直接模拟就好了 #include <iostream> using namespace std; int main() {// 请在此输入您的代码int x 22*92*81*9;cout << x << endl ;return 0; } 2 . 顺子日期 枚举出每个情况即可 : …...

    2024/5/6 13:50:47
  5. vue3项目运行正常但vscode红色波浪线报错

    以下解决办法如不生效&#xff0c;可尝试 重启 vscode 一、Vetur插件检测问题 vetur 是一个 vscode 插件&#xff0c;用于为 .vue 单文件组件提供代码高亮以及语法支持。但 vue 以及 vetur 对于 ts 的支持&#xff0c;并不友好。 1、原因 如下图&#xff1a;鼠标放到红色波浪…...

    2024/5/8 2:15:24
  6. 416. 分割等和子集问题(动态规划)

    题目 题解 class Solution:def canPartition(self, nums: List[int]) -> bool:# badcaseif not nums:return True# 不能被2整除if sum(nums) % 2 ! 0:return False# 状态定义&#xff1a;dp[i][j]表示当背包容量为j&#xff0c;用前i个物品是否正好可以将背包填满&#xff…...

    2024/5/8 19:32:33
  7. 【Java】ExcelWriter自适应宽度工具类(支持中文)

    工具类 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet;/*** Excel工具类** author xiaoming* date 2023/11/17 10:40*/ public class ExcelUti…...

    2024/5/7 22:31:36
  8. Spring cloud负载均衡@LoadBalanced LoadBalancerClient

    LoadBalance vs Ribbon 由于Spring cloud2020之后移除了Ribbon&#xff0c;直接使用Spring Cloud LoadBalancer作为客户端负载均衡组件&#xff0c;我们讨论Spring负载均衡以Spring Cloud2020之后版本为主&#xff0c;学习Spring Cloud LoadBalance&#xff0c;暂不讨论Ribbon…...

    2024/5/9 2:44:26
  9. TSINGSEE青犀AI智能分析+视频监控工业园区周界安全防范方案

    一、背景需求分析 在工业产业园、化工园或生产制造园区中&#xff0c;周界防范意义重大&#xff0c;对园区的安全起到重要的作用。常规的安防方式是采用人员巡查&#xff0c;人力投入成本大而且效率低。周界一旦被破坏或入侵&#xff0c;会影响园区人员和资产安全&#xff0c;…...

    2024/5/8 20:33:13
  10. VB.net WebBrowser网页元素抓取分析方法

    在用WebBrowser编程实现网页操作自动化时&#xff0c;常要分析网页Html&#xff0c;例如网页在加载数据时&#xff0c;常会显示“系统处理中&#xff0c;请稍候..”&#xff0c;我们需要在数据加载完成后才能继续下一步操作&#xff0c;如何抓取这个信息的网页html元素变化&…...

    2024/5/9 3:15:57
  11. 【Objective-C】Objective-C汇总

    方法定义 参考&#xff1a;https://www.yiibai.com/objective_c/objective_c_functions.html Objective-C编程语言中方法定义的一般形式如下 - (return_type) method_name:( argumentType1 )argumentName1 joiningArgument2:( argumentType2 )argumentName2 ... joiningArgu…...

    2024/5/7 16:57:02
  12. 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】

    &#x1f468;‍&#x1f4bb;博客主页&#xff1a;花无缺 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! 本文由 花无缺 原创 收录于专栏 【洛谷算法题】 文章目录 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】&#x1f30f;题目描述&#x1f30f;输入格…...

    2024/5/7 14:58:59
  13. 【ES6.0】- 扩展运算符(...)

    【ES6.0】- 扩展运算符... 文章目录 【ES6.0】- 扩展运算符...一、概述二、拷贝数组对象三、合并操作四、参数传递五、数组去重六、字符串转字符数组七、NodeList转数组八、解构变量九、打印日志十、总结 一、概述 **扩展运算符(...)**允许一个表达式在期望多个参数&#xff0…...

    2024/5/8 20:58:56
  14. 摩根看好的前智能硬件头部品牌双11交易数据极度异常!——是模式创新还是饮鸩止渴?

    文 | 螳螂观察 作者 | 李燃 双11狂欢已落下帷幕&#xff0c;各大品牌纷纷晒出优异的成绩单&#xff0c;摩根士丹利投资的智能硬件头部品牌凯迪仕也不例外。然而有爆料称&#xff0c;在自媒体平台发布霸榜各大榜单喜讯的凯迪仕智能锁&#xff0c;多个平台数据都表现出极度异常…...

    2024/5/9 1:35:21
  15. Go语言常用命令详解(二)

    文章目录 前言常用命令go bug示例参数说明 go doc示例参数说明 go env示例 go fix示例 go fmt示例 go generate示例 总结写在最后 前言 接着上一篇继续介绍Go语言的常用命令 常用命令 以下是一些常用的Go命令&#xff0c;这些命令可以帮助您在Go开发中进行编译、测试、运行和…...

    2024/5/8 1:37:35
  16. 用欧拉路径判断图同构推出reverse合法性:1116T4

    http://cplusoj.com/d/senior/p/SS231116D 假设我们要把 a a a 变成 b b b&#xff0c;我们在 a i a_i ai​ 和 a i 1 a_{i1} ai1​ 之间连边&#xff0c; b b b 同理&#xff0c;则 a a a 能变成 b b b 的充要条件是两图 A , B A,B A,B 同构。 必要性显然&#xff0…...

    2024/5/7 16:05:05
  17. 【NGINX--1】基础知识

    1、在 Debian/Ubuntu 上安装 NGINX 在 Debian 或 Ubuntu 机器上安装 NGINX 开源版。 更新已配置源的软件包信息&#xff0c;并安装一些有助于配置官方 NGINX 软件包仓库的软件包&#xff1a; apt-get update apt install -y curl gnupg2 ca-certificates lsb-release debian-…...

    2024/5/8 18:06:50
  18. Hive默认分割符、存储格式与数据压缩

    目录 1、Hive默认分割符2、Hive存储格式3、Hive数据压缩 1、Hive默认分割符 Hive创建表时指定的行受限&#xff08;ROW FORMAT&#xff09;配置标准HQL为&#xff1a; ... ROW FORMAT DELIMITED FIELDS TERMINATED BY \u0001 COLLECTION ITEMS TERMINATED BY , MAP KEYS TERMI…...

    2024/5/8 1:37:32
  19. 【论文阅读】MAG:一种用于航天器遥测数据中有效异常检测的新方法

    文章目录 摘要1 引言2 问题描述3 拟议框架4 所提出方法的细节A.数据预处理B.变量相关分析C.MAG模型D.异常分数 5 实验A.数据集和性能指标B.实验设置与平台C.结果和比较 6 结论 摘要 异常检测是保证航天器稳定性的关键。在航天器运行过程中&#xff0c;传感器和控制器产生大量周…...

    2024/5/9 1:42:21
  20. --max-old-space-size=8192报错

    vue项目运行时&#xff0c;如果经常运行慢&#xff0c;崩溃停止服务&#xff0c;报如下错误 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 因为在 Node 中&#xff0c;通过JavaScript使用内存时只能使用部分内存&#xff08;64位系统&…...

    2024/5/8 1:37:31
  21. 基于深度学习的恶意软件检测

    恶意软件是指恶意软件犯罪者用来感染个人计算机或整个组织的网络的软件。 它利用目标系统漏洞&#xff0c;例如可以被劫持的合法软件&#xff08;例如浏览器或 Web 应用程序插件&#xff09;中的错误。 恶意软件渗透可能会造成灾难性的后果&#xff0c;包括数据被盗、勒索或网…...

    2024/5/8 1:37:31
  22. JS原型对象prototype

    让我简单的为大家介绍一下原型对象prototype吧&#xff01; 使用原型实现方法共享 1.构造函数通过原型分配的函数是所有对象所 共享的。 2.JavaScript 规定&#xff0c;每一个构造函数都有一个 prototype 属性&#xff0c;指向另一个对象&#xff0c;所以我们也称为原型对象…...

    2024/5/8 12:44:41
  23. C++中只能有一个实例的单例类

    C中只能有一个实例的单例类 前面讨论的 President 类很不错&#xff0c;但存在一个缺陷&#xff1a;无法禁止通过实例化多个对象来创建多名总统&#xff1a; President One, Two, Three; 由于复制构造函数是私有的&#xff0c;其中每个对象都是不可复制的&#xff0c;但您的目…...

    2024/5/8 9:51:44
  24. python django 小程序图书借阅源码

    开发工具&#xff1a; PyCharm&#xff0c;mysql5.7&#xff0c;微信开发者工具 技术说明&#xff1a; python django html 小程序 功能介绍&#xff1a; 用户端&#xff1a; 登录注册&#xff08;含授权登录&#xff09; 首页显示搜索图书&#xff0c;轮播图&#xff0…...

    2024/5/8 1:37:29
  25. 电子学会C/C++编程等级考试2022年03月(一级)真题解析

    C/C++等级考试(1~8级)全部真题・点这里 第1题:双精度浮点数的输入输出 输入一个双精度浮点数,保留8位小数,输出这个浮点数。 时间限制:1000 内存限制:65536输入 只有一行,一个双精度浮点数。输出 一行,保留8位小数的浮点数。样例输入 3.1415926535798932样例输出 3.1…...

    2024/5/7 17:09:45
  26. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  27. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  28. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  29. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  30. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  31. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  32. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  33. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  34. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  35. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  36. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  37. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  38. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  39. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  40. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  41. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  42. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  43. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  44. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  45. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57