📄 chatarea.java
字号:
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class ChatArea extends Panel implements ActionListener, Runnable
{
Socket socket = null;
DataInputStream in = null;
DataOutputStream out = null;
Thread msgThread = null;
TextArea pubArea = null;
Label pubLabel = null;
TextField sendMsgField = null;
Button ok, freshPubArea, freshSendMsgField;
Label userListLabel = null;
static Label remind = null;
String name = null;
Hashtable userList;
List listComponent = null;
int width, height;
public ChatArea(String name, Hashtable listTable, int width, int height)
{
setLayout(null);
setBackground(Color.GRAY);
this.width = width;
this.height = height;
setSize(width, height);
this.userList = listTable;
this.name = name;
msgThread = new Thread(this);
pubArea = new TextArea(10, 10);
pubArea.setEditable(false);
pubArea.setBackground(Color.WHITE);
pubLabel = new Label("公共聊天区", Label.CENTER);
ok = new Button("发送消息");
freshSendMsgField = new Button("清除消息");
freshPubArea = new Button("清除谈话区");
userListLabel = new Label("用户列表", Label.CENTER);
remind = new Label(" 欢迎来到XX聊天室! ", Label.CENTER);
remind.setSize(100, 20);
remind.setForeground(Color.BLUE);
sendMsgField = new TextField(28);
ok.addActionListener(this);
sendMsgField.addActionListener(this);
freshSendMsgField.addActionListener(this);
freshPubArea.addActionListener(this);
listComponent = new List();
listComponent.addActionListener(this);
add(pubArea);
pubArea.setBounds(10, 10, (width - 120), (height - 160));
add(pubLabel);
pubLabel.setBounds(10, 10 + (height - 160), width - 120, 30);
add(listComponent);
listComponent.setBounds(10 + (width - 120), 10, 100, (height - 160));
add(userListLabel);
userListLabel.setBounds(10 + (width - 120), 10 + (height - 160), 100,
30);
Panel southPanel = new Panel();
southPanel.add(sendMsgField);
southPanel.add(ok);
southPanel.add(freshSendMsgField);
southPanel.add(freshPubArea);
add(southPanel);
southPanel.setBounds(10, (height - 120), width - 20, 40);
Panel bottomPanel = new Panel();
bottomPanel.add(remind);
add(bottomPanel);
bottomPanel.setBounds(10, (height - 80), width - 20, 30);
}
public void setName(String s)
{
name = s;
}
public void setSocketConnection(Socket socket, DataInputStream in,
DataOutputStream out)
{
this.socket = socket;
this.in = in;
this.out = out;
try
{
msgThread.start();
} catch (Exception e)
{
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == ok || e.getSource() == sendMsgField)
{
String message = "";
message = sendMsgField.getText();
if (message.length() > 0)
{
try
{
out.writeUTF("公共聊天内容:" + name + "说:" + message);
} catch (IOException event)
{
}
} else
{
remind.setText("您不能发送一条空消息!");
}
} else if (e.getSource() == freshPubArea)
{
pubArea.setText(null);
} else if (e.getSource() == freshSendMsgField)
{
sendMsgField.setText(null);
}
}
public void run()
{
while (true) // 在谈话显示区显示谈话内容
{
String info = null;
try
{
info = in.readUTF();
if (info.startsWith("聊天内容:"))
{
String content = info.substring(info.indexOf(":") + 1);
pubArea.append("\n" + content);
} else if (info.startsWith("聊天者:"))
{
String user = info.substring(info.indexOf(":") + 1, info
.indexOf("昵称"));
String nickName = info.substring(info.indexOf("昵称:") + 3);
userList.put(user, user + "(" + nickName + ")");
listComponent.add((String) userList.get(user));
listComponent.repaint();
} else if (info.startsWith("用户离线:"))
{
String userLeave = info.substring(info.indexOf(":") + 1);
listComponent.remove((String) userList.get(userLeave));
listComponent.repaint();
pubArea.append("\n" + (String) userList.get(userLeave)
+ "离线");
userList.remove(userLeave);
}
Thread.sleep(5);
} catch (IOException e)
{
listComponent.removeAll();
listComponent.repaint();
userList.clear();
pubArea.setText("已断开和服务器的连接\n请刷新浏览器再次聊天\n");
break;
} catch (InterruptedException e)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -