编写一个GUI界面的Socket聊天室(UDPChatGUI.java)
时间: 2024-02-09 16:12:33 浏览: 94
Java实现带有GUI界面的聊天室,双方通过socket通信
很高兴回答你的问题!下面是一个简单的UDP Socket聊天室的GUI界面的示例代码(UDPChatGUI.java):
```java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class UDPChatGUI extends JFrame {
private JPanel contentPane;
private JTextField txtMessage;
private JTextArea txtChat;
private JButton btnSend;
private DatagramSocket socket;
private InetAddress address;
private int port;
private Thread receiveThread;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UDPChatGUI frame = new UDPChatGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UDPChatGUI() {
setTitle("UDP Chat");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
txtChat = new JTextArea();
txtChat.setEditable(false);
JScrollPane scrollPane = new JScrollPane(txtChat);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
panel.setLayout(new BorderLayout(0, 0));
txtMessage = new JTextField();
panel.add(txtMessage, BorderLayout.CENTER);
txtMessage.setColumns(10);
btnSend = new JButton("Send");
btnSend.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
panel.add(btnSend, BorderLayout.EAST);
// Create a new DatagramSocket
try {
socket = new DatagramSocket();
} catch (IOException e1) {
e1.printStackTrace();
}
// Get the IP address and port number of the server
try {
address = InetAddress.getByName("localhost");
port = 8888;
} catch (Exception e1) {
e1.printStackTrace();
}
// Start a new thread to receive messages from the server
receiveThread = new Thread(new Runnable() {
public void run() {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
try {
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
txtChat.append("Server: " + message + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
receiveThread.start();
}
private void sendMessage() {
try {
// Get the message from the text field
String message = txtMessage.getText();
// Convert the message to bytes
byte[] buffer = message.getBytes();
// Create a new DatagramPacket
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
// Send the packet
socket.send(packet);
// Append the message to the chat window
txtChat.append("Me: " + message + "\n");
// Clear the text field
txtMessage.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个程序使用了Java的Swing GUI库,创建了一个简单的聊天室界面,包括一个文本框和一个发送按钮。当用户点击发送按钮时,程序将文本框中的消息发送到服务器,并将其显示在聊天窗口中。程序还启动了一个单独的线程来接收来自服务器的消息,并将它们显示在聊天窗口中。
请注意,这个示例程序仅用于演示目的,不能直接在生产环境中使用,因为它缺乏安全性和错误处理。在实际应用中,您需要更多的代码来处理异常情况,如网络连接中断、数据包丢失等。
阅读全文