📄 chatpanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
class ChatPanel extends JPanel
{
JTextArea ta = new JTextArea();
JTextField tf = new JTextField(12);
JButton bn = new JButton("发送");
Socket sock;
Thread thr;
String playerName;
final int width = 200;
final int height = 240;
ChatPanel(ChessGame game)
{
this.sock = game.chatSock;
setLayout(new BorderLayout());
setSize(width, height);
ta.setEditable(false);
add("Center", new JScrollPane(ta));
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add("West", tf);
p.add("East", bn);
add("South", p);
playerName = game.cpad.listeningPlayer.playerName;
bn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
if(tf.getText() == null)
return;
try{
String s = playerName + "说:" + tf.getText() + "\n";
tf.setText(null);
ta.append(s);
sock.getOutputStream().write(s.getBytes());
}
catch(IOException ex)
{
System.out.println("Send Message Error");
}
}
});
thr = new Thread() {
public void run()
{
try{
while(true)
{
byte[] b = new byte[256];
sock.getInputStream().read(b);
String s = new String(b);
s = s.trim();
s += "\n";
//System.out.println(s);
ta.append(s);
}
}
catch(IOException ex)
{
System.out.println("Receive Message Error!");
}
}
};
thr.start();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -