标签:11 24 String chatArea void new message public
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
实验要求:
1. 提交源代码;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
class NewChatRoom {
private ArrayList<String> messages = new ArrayList<>();
private JTextArea chatArea;
public NewChatRoom(JTextArea chatArea) {
this.chatArea = chatArea;
}
public void sendTextMessage(NewMember member, String text) {
if (text.length() <= 100) {
String message = member.getName() + ": " + text;
messages.add(message);
displayMessage(message);
} else {
System.out.println("Text message exceeds 100 characters. Not sent.");
}
}
public void sendImage(NewMember member, int imageSize) {
if (imageSize <= 20) {
String message = member.getName() + " sent an image.";
messages.add(message);
displayMessage(message);
} else {
System.out.println("Image size exceeds 20M. Not sent.");
}
}
private void displayMessage(String message) {
chatArea.append(message + "\n");
}
}
class NewMember {
private String name;
public NewMember(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void sendTextMessage(NewChatRoom chatRoom, String text) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = dateFormat.format(new Date());
String formattedText = text + " - " + currentTime;
chatRoom.sendTextMessage(this, formattedText);
}
public void sendImage(NewChatRoom chatRoom, int imageSize) {
chatRoom.sendImage(this, imageSize);
}
}
public class ChatGUI extends JFrame {
private NewChatRoom chatRoom;
private JTextField messageField;
public ChatGUI() {
setTitle("Simple Chat Room");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JTextArea chatArea = new JTextArea();
chatArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(chatArea);
add(scrollPane, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
messageField = new JTextField();
bottomPanel.add(messageField, BorderLayout.CENTER);
JButton sendButton = new JButton("Send");
sendButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendMessage();
}
});
bottomPanel.add(sendButton, BorderLayout.EAST);
add(bottomPanel, BorderLayout.SOUTH);
chatRoom = new NewChatRoom(chatArea);
setSize(400, 300);
setVisible(true);
}
private void sendMessage() {
String message = messageField.getText().trim();
if (!message.isEmpty()) {
NewMember member = new NewMember("User");
member.sendTextMessage(chatRoom, message);
messageField.setText("");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ChatGUI();
}
});
}
}
标签:11,
24,
String,
chatArea,
void,
new,
message,
public
From: https://www.cnblogs.com/fupeisen/p/17854811.html