📄 framechatclient.java
字号:
//FrameChatClient.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameChatClient extends JFrame implements Runnable,ActionListener{
private JTextField txtServer = null; //服务器名
private JTextField txtPort = null; //服务器端口
private JTextField txtUser = null; //登录用户名
private JTextField txtChat = null; //当前发送信息
private JButton btnConnect = null; //连接按钮
private JButton btnSend = null; //发送消息按钮
private JButton btnClose = null; //断开连接按钮
private JTextArea ta = null; //聊天内容
private Socket client = null; //套接字
private BufferedReader in = null;
private DataOutputStream out = null;
private Thread curThread = null; //当前线程
public FrameChatClient() {
super("Java聊天程序");
txtServer = new JTextField(10);
txtPort = new JTextField(6);
txtUser = new JTextField(5);
txtChat = new JTextField(38);
btnConnect = new JButton("连接");
btnSend = new JButton("发送");
btnClose = new JButton("关闭");
Container cp = this.getContentPane();
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("服务器:"));
p.add(txtServer);
p.add(new JLabel("端口:"));
p.add(txtPort);
p.add(new JLabel("用户名:"));
p.add(txtUser);
p.add(btnConnect);
p.add(btnClose);
cp.add(p , BorderLayout.NORTH);
p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("发言:"));
p.add(txtChat);
p.add(btnSend);
cp.add(p , BorderLayout.SOUTH);
ta = new JTextArea(6 , 35);
JScrollPane sp = new JScrollPane(ta);
cp.add(sp , BorderLayout.CENTER);
btnConnect.addActionListener(this); //连接
btnClose.addActionListener(this); //断开
btnSend.addActionListener(this); //发送
}
public void actionPerformed(ActionEvent e) { //处理按钮
if (e.getSource() == btnConnect) { //如果是连接
String strServer , strPort , strUser ;
strServer = txtServer.getText().trim();
strPort = txtPort.getText().trim();
strUser = txtUser.getText().trim();
/*要求服务器,端口,用户名都不能为空*/
if (strServer.equals("") || strPort.equals("") || strUser.equals("")) {
return;
}
if (client != null ){ //如果已经连接
JOptionPane.showMessageDialog(this , "请先断开连接!" ,
"连接错误" , JOptionPane.PLAIN_MESSAGE);
return;
}
try { //建立连接
client = new Socket(strServer , Integer.parseInt(strPort)); //创建套接字
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new DataOutputStream(client.getOutputStream());
out.writeBytes(strUser + "\n"); //先发送用户名
curThread = new Thread(this); //创建服务线程
curThread.start(); //启动线程
}catch(Exception ce) {
JOptionPane.showMessageDialog(this , "请检查服务器和端口设置!" ,
"连接错误" , JOptionPane.PLAIN_MESSAGE);
}
}
if (e.getSource() == btnClose) { //如果是断开,则调用close()
close();
}
if (e.getSource() == btnSend) { //如果是发送信息
String strChat = txtChat.getText().trim();
txtChat.setText("");
if (strChat.equals("")) { //忽略空消息
return;
}
if (strChat.equals("Quit")) { //如果是退出
close();
}else {
sendMsg(strChat); //发送消息
}
}
}
public void close() { //断开连接
curThread = null; //停止线程
if (client != null) {
try {
sendMsg("Quit"); //发送结束标志
in.close();
out.close();
client.close();
}catch(Exception xe) {
xe.printStackTrace();
}
client = null;
}else {
JOptionPane.showMessageDialog(this , "未连接到服务器!" ,
"错误" , JOptionPane.PLAIN_MESSAGE);
}
}
public void sendMsg(String msg) { //发送消息
if (out != null && client != null) {
try {
out.writeBytes(msg + "\n");
}catch(IOException e) {
e.printStackTrace();
}
}
}
public void run() { //主线程
Thread t = Thread.currentThread();
while (t == curThread && client != null) {
try {
String str = in.readLine(); //读取服务器发送的信息
ta.append(str + "\n");
}catch(IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[]args) {
FrameChatClient fcc = new FrameChatClient();
fcc.setSize(540,300);
fcc.setDefaultCloseOperation(EXIT_ON_CLOSE);
fcc.setVisible(true);
fcc.setResizable(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -