java聊天室实现带界面
时间: 2023-08-14 14:08:19 浏览: 41
Java聊天室实现带界面可以使用Java Swing界面库和Socket网络编程技术。
以下是一个简单的Java聊天室实现带界面的代码示例:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ChatClient extends JFrame implements ActionListener {
private JTextField tf;
private JTextArea ta;
private JButton sendBtn;
private Socket socket;
private BufferedReader br;
private BufferedWriter bw;
public ChatClient() {
super("Chat Client");
tf = new JTextField();
ta = new JTextArea();
sendBtn = new JButton("Send");
JScrollPane scrollPane = new JScrollPane(ta);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(tf, BorderLayout.CENTER);
panel.add(sendBtn, BorderLayout.EAST);
Container contentPane = getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(panel, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setVisible(true);
tf.addActionListener(this);
sendBtn.addActionListener(this);
try {
socket = new Socket("localhost", 8000);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
new Thread(new ReceiveThread()).start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void actionPerformed(ActionEvent e) {
try {
String message = tf.getText();
bw.write(message + "\n");
bw.flush();
ta.append("> " + message + "\n");
tf.setText("");
} catch (Exception ex) {
ex.printStackTrace();
}
}
class ReceiveThread implements Runnable {
public void run() {
try {
while (true) {
String message = br.readLine();
ta.append("< " + message + "\n");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public static void main(String[] args) {
new ChatClient();
}
}
```
这个聊天室客户端程序包括一个文本框、一个文本域、一个发送按钮和一个接收线程。它使用Socket连接到localhost的8000端口,发送和接收消息。用户输入消息后按Enter键或点击发送按钮,消息就会发送到服务器,服务器将消息广播给所有客户端。当有新消息到达时,接收线程将读取消息并在文本域中显示。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)