用Java语言编写一个GUI界面的Socket聊天室,具体要求:界面要求服务器端有退出,关闭功能,显示“开始监听9999端口!”客户端有发送,清空功能,显示“连接到127.0.0.1:9999端口”
时间: 2024-02-20 15:01:42 浏览: 78
Java实现带有GUI界面的聊天室,双方通过socket通信
好的,我可以帮你回答这个问题。首先,我们需要使用Java的GUI库来实现界面,比如Swing或JavaFX。其次,我们需要使用Socket编程实现聊天室的功能。以下是一个简单的实现:
服务器端:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ChatServer {
private ServerSocket serverSocket;
private Thread serverThread;
public ChatServer(int port) {
try {
serverSocket = new ServerSocket(port);
System.out.println("开始监听" + port + "端口!");
start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
serverThread = new Thread(() -> {
try {
while (true) {
Socket socket = serverSocket.accept();
System.out.println("有客户端连接到服务器:" + socket.getRemoteSocketAddress());
new ChatServerThread(socket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
});
serverThread.start();
}
public void stop() {
try {
serverSocket.close();
serverThread.interrupt();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ChatServer server = new ChatServer(9999);
// 等待用户输入"exit"命令退出
while (true) {
String command = System.console().readLine();
if ("exit".equals(command)) {
server.stop();
break;
}
}
}
}
```
客户端:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ChatClient extends JFrame {
private static final long serialVersionUID = 1L;
private static final String SERVER_HOST = "127.0.0.1";
private static final int SERVER_PORT = 9999;
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private JPanel panel;
private JTextArea textArea;
private JTextField textField;
private JButton sendButton;
private JButton clearButton;
public ChatClient() {
initUI();
initSocket();
}
private void initUI() {
setTitle("聊天室");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
getContentPane().add(panel);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
panel.add(scrollPane);
textField = new JTextField(20);
panel.add(textField);
sendButton = new JButton("发送");
sendButton.addActionListener(e -> {
String message = textField.getText();
if (!message.isEmpty()) {
writer.println(message);
writer.flush();
textField.setText("");
}
});
panel.add(sendButton);
clearButton = new JButton("清空");
clearButton.addActionListener(e -> {
textArea.setText("");
});
panel.add(clearButton);
}
private void initSocket() {
try {
socket = new Socket(SERVER_HOST, SERVER_PORT);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
System.out.println("连接到" + SERVER_HOST + ":" + SERVER_PORT + "端口");
new ChatClientThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
private class ChatClientThread extends Thread {
@Override
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) {
break;
}
textArea.append(message + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
ChatClient client = new ChatClient();
client.setVisible(true);
}
}
```
以上代码实现了一个简单的GUI界面的Socket聊天室,界面包含发送和清空按钮,服务器端可以退出和关闭,客户端可以连接到指定的主机和端口。
阅读全文