设计一个留言板,使用Application
时间: 2024-01-14 20:02:43 浏览: 81
留言板设计
好的,以下是一个基本的留言板设计,使用Java Swing和Application进行实现:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class MessageBoard extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private static final String FILE_NAME = "messages.txt"; // 存储留言的文件名
private JPanel mainPanel;
private JTextArea messageArea;
private JLabel nameLabel, emailLabel, messageLabel;
private JTextField nameField, emailField;
private JTextArea messageField;
private JButton submitButton, clearButton;
public MessageBoard() {
super("留言板");
// 创建主面板
mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// 创建留言面板
JPanel messagePanel = new JPanel(new GridLayout(3, 2, 10, 5));
messagePanel.setBorder(BorderFactory.createTitledBorder("留言信息"));
nameLabel = new JLabel("姓名:", SwingConstants.RIGHT);
nameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
emailLabel = new JLabel("邮箱:", SwingConstants.RIGHT);
emailLabel.setFont(new Font("宋体", Font.PLAIN, 16));
messageLabel = new JLabel("留言:", SwingConstants.RIGHT);
messageLabel.setFont(new Font("宋体", Font.PLAIN, 16));
nameField = new JTextField();
emailField = new JTextField();
messageField = new JTextArea();
messageField.setLineWrap(true);
JScrollPane messageScrollPane = new JScrollPane(messageField);
messagePanel.add(nameLabel);
messagePanel.add(nameField);
messagePanel.add(emailLabel);
messagePanel.add(emailField);
messagePanel.add(messageLabel);
messagePanel.add(messageScrollPane);
// 创建按钮面板
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 5));
submitButton = new JButton("提交");
submitButton.addActionListener(this);
clearButton = new JButton("清空");
clearButton.addActionListener(this);
buttonPanel.add(submitButton);
buttonPanel.add(clearButton);
// 创建留言显示面板
JPanel messageDisplayPanel = new JPanel(new BorderLayout());
messageDisplayPanel.setBorder(BorderFactory.createTitledBorder("留言信息"));
messageArea = new JTextArea();
messageArea.setEditable(false);
messageArea.setBackground(Color.WHITE);
messageArea.setLineWrap(true);
messageArea.setWrapStyleWord(true);
JScrollPane messageScroll = new JScrollPane(messageArea);
messageDisplayPanel.add(messageScroll, BorderLayout.CENTER);
// 添加组件到主面板
mainPanel.add(messagePanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(messageDisplayPanel, BorderLayout.SOUTH);
// 加载之前存储的留言
loadMessages();
// 设置窗口属性
setContentPane(mainPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 500);
setLocationRelativeTo(null);
setVisible(true);
}
// 加载之前存储的留言
private void loadMessages() {
try {
Scanner scanner = new Scanner(new File(FILE_NAME));
while (scanner.hasNextLine()) {
String name = scanner.nextLine();
String email = scanner.nextLine();
String message = scanner.nextLine();
LocalDateTime dateTime = LocalDateTime.parse(scanner.nextLine());
addMessage(name, email, message, dateTime);
}
scanner.close();
} catch (IOException e) {
System.out.println("无法加载之前的留言!");
}
}
// 添加新留言
private void addMessage(String name, String email, String message, LocalDateTime dateTime) {
String text = String.format("[%tF %tT] %s (%s) 说:\n%s\n\n", dateTime, dateTime, name, email, message);
messageArea.append(text);
}
// 保存留言到文件中
private void saveMessage(String name, String email, String message, LocalDateTime dateTime) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME, true));
writer.write(name + "\n");
writer.write(email + "\n");
writer.write(message + "\n");
writer.write(dateTime.toString() + "\n");
writer.flush();
writer.close();
} catch (IOException e) {
System.out.println("无法保存留言!");
}
}
// 清空留言输入框
private void clearFields() {
nameField.setText("");
emailField.setText("");
messageField.setText("");
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == submitButton) {
String name = nameField.getText().trim();
String email = emailField.getText().trim();
String message = messageField.getText().trim();
// 检查输入是否为空
if (name.isEmpty() || email.isEmpty() || message.isEmpty()) {
JOptionPane.showMessageDialog(this, "请填写完整的留言信息!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 添加新留言
addMessage(name, email, message, LocalDateTime.now());
// 保存留言并清空输入框
saveMessage(name, email, message, LocalDateTime.now());
clearFields();
JOptionPane.showMessageDialog(this, "留言已提交,谢谢您的参与!", "提示", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getSource() == clearButton) {
clearFields();
}
}
public static void main(String[] args) {
new MessageBoard();
}
}
```
这个留言板的功能比较简单,用户可以输入姓名、邮箱和留言内容,并点击“提交”按钮将留言添加到留言板中。同时,程序会将留言存储到本地文件中,并在程序启动时加载之前的留言。用户还可以点击“清空”按钮清空输入框的内容。
阅读全文