?? 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 + -