import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.border.EmptyBorder; public class SensitiveWordAnalyzer extends JFrame { private JPanel contentPane; private JTextField textFile; private JTextField textKeyword; private JList<String> listWords; private Map<String, Integer> wordMap; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { SensitiveWordAnalyzer frame = new SensitiveWordAnalyzer(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public SensitiveWordAnalyzer() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300);分析这段代码
时间: 2024-04-28 18:25:25 浏览: 156
这段代码定义了一个名为SensitiveWordAnalyzer的Java GUI应用程序。该应用程序可以用来分析文本文件中的敏感词并统计它们出现的次数。应用程序包含一个JFrame窗口和几个Swing组件,包括JPanel、JTextField、JList、JScrollPane、JLabel和JButton等。
在GUI中,用户可以选择要分析的文本文件,还可以添加或删除敏感词。文本文件和敏感词都保存在内存中的Map中。当用户单击“分析”按钮时,应用程序将读取文本文件并搜索其中的敏感词。在JList中,用户可以查看所有的敏感词及其出现次数。
该应用程序使用了Java的Swing GUI库来创建用户界面。它还使用了Java的文件I/O类来读取和写入文件。
相关问题
编写一个GUI界面的Socket聊天室(UDPChatGUI.java)
很高兴回答你的问题!下面是一个简单的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库,创建了一个简单的聊天室界面,包括一个文本框和一个发送按钮。当用户点击发送按钮时,程序将文本框中的消息发送到服务器,并将其显示在聊天窗口中。程序还启动了一个单独的线程来接收来自服务器的消息,并将它们显示在聊天窗口中。
请注意,这个示例程序仅用于演示目的,不能直接在生产环境中使用,因为它缺乏安全性和错误处理。在实际应用中,您需要更多的代码来处理异常情况,如网络连接中断、数据包丢失等。
运用Java编写一个GUI界面的Socket聊天室(UDPChatGUl.java) 具体要求: (2)用Socket编程实现服务器端和客户端程序的编写。 (3)服务器端只负责好友列表的刷新,不负责客户端聊天信息的转发。
好的,以下是一个简单的UDP聊天室GUI界面程序的实现,您可以参考一下:
UDPChatGUl.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 java.net.SocketException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class UDPChatGUl extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextArea textArea;
private JList<String> list;
private DefaultListModel<String> listModel;
private Map<String, String> friendMap;
private DatagramSocket socket;
private DatagramPacket packet;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UDPChatGUl frame = new UDPChatGUl();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public UDPChatGUl() throws SocketException {
setTitle("UDP聊天室");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
textField = new JTextField();
textField.setBounds(10, 227, 313, 23);
contentPane.add(textField);
textField.setColumns(10);
JButton sendButton = new JButton("发送");
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
send();
}
});
sendButton.setBounds(333, 227, 91, 23);
contentPane.add(sendButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 10, 313, 206);
contentPane.add(scrollPane);
textArea = new JTextArea();
scrollPane.setViewportView(textArea);
listModel = new DefaultListModel<String>();
list = new JList<String>(listModel);
list.setBounds(333, 10, 91, 206);
contentPane.add(list);
friendMap = new HashMap<String, String>();
// 创建服务器端socket
socket = new DatagramSocket(12345);
// 启动接收线程
new Thread(new Runnable() {
public void run() {
receive();
}
}).start();
// 向服务器端请求好友列表
requestFriendList();
}
// 发送消息
private void send() {
String message = textField.getText();
if (message != null && !message.trim().equals("")) {
String friend = list.getSelectedValue();
if (friend != null) {
String friendAddress = friendMap.get(friend);
if (friendAddress != null) {
try {
byte[] data = message.getBytes();
InetAddress address = InetAddress.getByName(friendAddress.split(":")[0]);
int port = Integer.parseInt(friendAddress.split(":")[1]);
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
textArea.append("我对" + friend + "说:" + message + "\n");
textField.setText("");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
// 接收消息
private void receive() {
while (true) {
try {
byte[] data = new byte[1024];
packet = new DatagramPacket(data, data.length);
socket.receive(packet);
String message = new String(packet.getData(), 0, packet.getLength());
String friend = getFriend(packet.getAddress().getHostAddress(), packet.getPort());
textArea.append(friend + "对我说:" + message + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 向服务器端请求好友列表
private void requestFriendList() {
try {
byte[] data = "list".getBytes();
InetAddress address = InetAddress.getByName("localhost");
int port = 12345;
DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
// 处理好友列表信息
private void handleFriendList(String message) {
String[] friends = message.split(",");
for (String friend : friends) {
String[] info = friend.split(":");
String name = info[0];
String address = info[1] + ":" + info[2];
friendMap.put(name, address);
listModel.addElement(name);
}
}
// 获取好友信息
private String getFriend(String hostAddress, int port) {
for (Map.Entry<String, String> entry : friendMap.entrySet()) {
String value = entry.getValue();
String[] info = value.split(":");
if (info[0].equals(hostAddress) && info[1].equals(String.valueOf(port))) {
return entry.getKey();
}
}
return null;
}
}
```
上述代码中,我们使用了Java的Swing库来实现GUI界面。程序分为客户端和服务器端两部分,其中,客户端负责向服务器端请求好友列表、发送消息和接收消息,而服务器端只负责维护好友列表并定时向客户端广播好友列表信息。
在实现中,我们使用了UDP协议进行通信。客户端向服务器端发送请求获取好友列表的信息,服务器端在收到请求后会将好友列表信息发送给客户端。客户端接收到好友列表信息后将其显示在GUI界面的好友列表中。客户端向指定好友发送消息时,需要获取该好友的IP地址和端口号,并通过UDP连接向该好友发送消息。当客户端接收到其他好友发送的消息时,需要在聊天窗口中显示该消息。
再次提醒,这是一个简单的UDP聊天室GUI界面程序的实现,具体的实现细节和代码,需要根据您的具体需求和环境进行调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)