⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientchat.java

📁 用java编写的聊天室 多线程 包含服务器和客户端
💻 JAVA
字号:
package WebChat;

import java.awt.*; 
import java.io.*; 
import java.net.*; 
import java.applet.*; 
import java.util.Hashtable; 
public class ClientChat extends Applet implements Runnable 
{ 
Socket socket=null; 
DataInputStream in=null; 
DataOutputStream out=null; 
InputNameTextField 用户提交昵称界面=null; 
ChatArea 用户聊天界面=null; 
Hashtable listTable; 
Label 提示条; 
Panel north, center; 
Thread thread; 
public void init() 
{ 
int width=getSize().width; 
int height=getSize().height; 
listTable=new Hashtable(); 
setLayout(new BorderLayout()); 
用户提交昵称界面=new InputNameTextField(listTable); 
int h=用户提交昵称界面.getSize().height; 
用户聊天界面=new ChatArea("",listTable,width,height-(h+5)); 
用户聊天界面.setVisible(false); 
提示条=new Label("正在连接到服务器,请稍等...",Label.CENTER); 
提示条.setForeground(Color.red); 
north=new Panel(new FlowLayout(FlowLayout.LEFT)); 
center=new Panel(); 
north.add(用户提交昵称界面); 
north.add(提示条); 
center.add(用户聊天界面); 
add(north,BorderLayout.NORTH); 
add(center,BorderLayout.CENTER); 
validate(); 
} 
public void start() 
{ 
if(socket!=null&&in!=null&&out!=null) 
{ try 
{ 
socket.close(); 
in.close(); 
out.close(); 
用户聊天界面.setVisible(false); 

} 
catch(Exception ee) 
{ 
} 
} 
try 
{ 
socket = new Socket(this.getCodeBase().getHost(), 6666); 
in=new DataInputStream(socket.getInputStream()); 
out=new DataOutputStream(socket.getOutputStream()); 
} 
catch (IOException ee) 
{ 
提示条.setText("连接失败"); 
} 
if(socket!=null) 
{ 
InetAddress address=socket.getInetAddress(); 
提示条.setText("连接:"+address+"成功"); 
用户提交昵称界面.setSocketConnection(socket,in,out); 
north.validate(); 
} 
if(thread==null) 
{ 
thread=new Thread(this); 
thread.start(); 
} 
} 

public void stop() 
{ 
try 
{ 
socket.close(); 
thread=null; 
} 
catch(IOException e) 
{ 
this.showStatus(e.toString()); 
} 
} 
public void run() 
{ 
while(thread!=null) 
{ 
if(用户提交昵称界面.get能否聊天()==true) 
{ 
用户聊天界面.setVisible(true); 
用户聊天界面.setName(用户提交昵称界面.getName()); 
用户聊天界面.setSocketConnection(socket,in,out); 
提示条.setText("祝聊天愉快!"); 
center.validate(); 
break; 
} 
try 
{ 
Thread.sleep(100); 
} 
catch(Exception e) 
{ 
} 
} 
} 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -