📄 clientchat.java
字号:
/*
* Title: 网络应用
* Description: b/s模式网络聊天室
* Copyright: Copyright (c) 2004 飞鱼工作室
* Company: HOHAI
* @author: lishaofeng,yuanfeng
* @version: 1.0
*/
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.util.Hashtable;
import java.awt.event.*;
import javax.swing.*;
////////////////////////////////////////////////////
class MyThread extends Thread
{
ChatArea chat;
DataInputStream in;
MyThread(ChatArea chat,DataInputStream in)
{
this.chat=chat;
this.in=in;
}
public void run()
{
// System.out.println("我被运行了");
while(true)
{
String s=null;
try
{
s=in.readUTF();
// System.out.println("受到消息了啊啊啊啊啊啊啊 ");
chat.ProcessMessage(s);
}
catch(IOException e)
{
}
}
}
}
///////////////////////////////////////////////////////////
public class ClientChat extends Frame implements ActionListener//,Runnable
{
MyThread thread;
Socket socket=null;
DataInputStream in=null;
DataOutputStream out=null;
InputNameTextField UserHdChat=null;
ChatArea UserChat=null;
Hashtable listTable; //存放在线聊天者昵称的散列表。
Label Inf1=null,Inf;
Panel north, center;
String name=null;
int width=800,height=600;
TextField enterip=null;
Button login=null;
File file=null;
/////////////////////////////////////////////////////////////////
public ClientChat()
{
super("飞鱼聊天室");
this.listTable=listTable;
enterip=new TextField(16);
login=new Button("连接");
login.addActionListener(this);
Inf1=new Label("输入服务器的IP:");
listTable=new Hashtable();
UserHdChat=new InputNameTextField(listTable,this);
UserHdChat.setVisible(false);
UserHdChat.InChat.addActionListener(this);
UserChat=new ChatArea(name,listTable,width,height,this);
UserChat.setVisible(true);
Inf=new Label("",Label.CENTER);
Inf.setForeground(Color.red);
Panel H1=new Panel();
H1.add(Inf1);
H1.add(enterip);
H1.add(login);
H1.add(UserHdChat);
Panel H3=new Panel();
H3.add(Inf);
Panel H4=new Panel();
H4.add(UserChat);
north=new Panel();
center=new Panel();
north.setLayout(new GridLayout(2,1));
north.add(H1);
north.add(H3);
center.setLayout(new BorderLayout());
center.add(H4,BorderLayout.CENTER);
add(north,BorderLayout.NORTH);
add(center,BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setVisible(true);
setBounds(50,40,800,700);
validate();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==login)
{
if(socket!=null&&in!=null&&out!=null) //清除此前的套接字信息。
{
try
{
socket.close();
in.close();
out.close();
UserHdChat.setVisible(false);
UserChat.setVisible(false);
}
catch(Exception ee)
{
}
}
String ip=enterip.getText().trim();
try
{
socket = new Socket(ip, 6000);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
thread=new MyThread(UserChat,in);
thread.start();
// System.out.println("线程被启动");
UserChat.setSocketConnection(socket,in,out);
}
catch (IOException ee)
{
enterip.setText("呼叫失败");
Inf.setText("连接失败");
}
if(socket!=null) //如果login成功,则提示用户输入昵称。
{
InetAddress address=socket.getInetAddress();
Inf.setEnabled(true);
Inf.setText("连接:"+address+"成功");
UserHdChat.setVisible(true);
UserHdChat.setSocketConnection(socket,in,out);
UserHdChat.validate();
north.validate();
validate();
}
}
if(e.getSource()==UserHdChat.OutChat)
{
System.exit(0);
}
}
public static void main(String args[])
{
new ClientChat();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -