1.笔记共享平台: 要求:使用图形化界面,用户通过注册账号(用户名和密码)后,登录笔记共享平台。 1.可以编写自己的笔记 2.可以保存自己的笔记,并记录最后一次保存的时间 3.设置笔记的状态(共享or私有) 4.若笔记共享则发布到笔记广场,供其他用户参阅Java代码书写
时间: 2024-03-20 18:42:38 浏览: 214
基于Java编写实现记事本的全部功能,使用了JFrame,图形界面显示,下载下来就可以使用,注释完整,一看就会
好的,下面是一个简单的Java代码示例,实现了上述要求:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
public class NoteSharingPlatform extends JFrame implements ActionListener {
private JTextField usernameField;
private JPasswordField passwordField;
private JTextArea noteTextArea;
private JLabel statusLabel;
private JCheckBox shareCheckBox;
private JButton saveButton;
private JButton publishButton;
private Connection conn;
public NoteSharingPlatform() {
// 初始化界面
setTitle("笔记共享平台");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建组件
JLabel usernameLabel = new JLabel("用户名:");
usernameField = new JTextField(20);
JLabel passwordLabel = new JLabel("密码:");
passwordField = new JPasswordField(20);
JLabel noteLabel = new JLabel("笔记:");
noteTextArea = new JTextArea(10, 30);
statusLabel = new JLabel("");
shareCheckBox = new JCheckBox("共享");
saveButton = new JButton("保存");
publishButton = new JButton("发布");
// 布局组件
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new GridLayout(2, 2));
loginPanel.add(usernameLabel);
loginPanel.add(usernameField);
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
JPanel notePanel = new JPanel();
notePanel.setLayout(new BorderLayout());
notePanel.add(noteLabel, BorderLayout.NORTH);
notePanel.add(new JScrollPane(noteTextArea), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 3));
buttonPanel.add(shareCheckBox);
buttonPanel.add(saveButton);
buttonPanel.add(publishButton);
JPanel statusPanel = new JPanel();
statusPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
statusPanel.add(statusLabel);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(loginPanel, BorderLayout.NORTH);
mainPanel.add(notePanel, BorderLayout.CENTER);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(statusPanel, BorderLayout.SOUTH);
// 添加事件监听器
saveButton.addActionListener(this);
publishButton.addActionListener(this);
// 显示界面
setContentPane(mainPanel);
setVisible(true);
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/note_sharing_platform", "root", "");
} catch (SQLException | ClassNotFoundException ex) {
ex.printStackTrace();
statusLabel.setText("无法连接数据库!");
}
}
public static void main(String[] args) {
new NoteSharingPlatform();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == saveButton) {
saveNote();
} else if (e.getSource() == publishButton) {
publishNote();
}
}
private void saveNote() {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String note = noteTextArea.getText();
boolean shared = shareCheckBox.isSelected();
Date lastModified = new Date();
// 检查用户名和密码是否正确
if (!checkLogin(username, password)) {
statusLabel.setText("用户名或密码错误!");
return;
}
// 保存笔记到文件
String filename = String.format("%s-%d.txt", username, lastModified.getTime());
File file = new File(filename);
try (PrintWriter out = new PrintWriter(file)) {
out.print(note);
} catch (IOException ex) {
ex.printStackTrace();
statusLabel.setText("保存笔记失败!");
return;
}
// 更新笔记元数据到数据库
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO notes (filename, username, last_modified, shared) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, filename);
stmt.setString(2, username);
stmt.setTimestamp(3, new Timestamp(lastModified.getTime()));
stmt.setBoolean(4, shared);
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
statusLabel.setText("保存笔记失败!");
return;
}
statusLabel.setText(String.format("笔记已保存(%s)", lastModified));
}
private void publishNote() {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
String note = noteTextArea.getText();
boolean shared = shareCheckBox.isSelected();
Date lastModified = new Date();
// 检查用户名和密码是否正确
if (!checkLogin(username, password)) {
statusLabel.setText("用户名或密码错误!");
return;
}
// 保存笔记到文件
String filename = String.format("%s-%d.txt", username, lastModified.getTime());
File file = new File(filename);
try (PrintWriter out = new PrintWriter(file)) {
out.print(note);
} catch (IOException ex) {
ex.printStackTrace();
statusLabel.setText("保存笔记失败!");
return;
}
// 更新笔记元数据到数据库
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO notes (filename, username, last_modified, shared) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, filename);
stmt.setString(2, username);
stmt.setTimestamp(3, new Timestamp(lastModified.getTime()));
stmt.setBoolean(4, shared);
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
statusLabel.setText("发布笔记失败!");
return;
}
// 发布笔记到广场
if (shared) {
try (PreparedStatement stmt = conn.prepareStatement("INSERT INTO notes_on_square (filename, username, last_modified) VALUES (?, ?, ?)")) {
stmt.setString(1, filename);
stmt.setString(2, username);
stmt.setTimestamp(3, new Timestamp(lastModified.getTime()));
stmt.executeUpdate();
} catch (SQLException ex) {
ex.printStackTrace();
statusLabel.setText("发布笔记失败!");
return;
}
}
statusLabel.setText(String.format("笔记已发布(%s)", lastModified));
}
private boolean checkLogin(String username, String password) {
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?")) {
stmt.setString(1, username);
stmt.setString(2, password);
try (ResultSet rs = stmt.executeQuery()) {
return rs.next();
}
} catch (SQLException ex) {
ex.printStackTrace();
statusLabel.setText("无法验证登录信息!");
return false;
}
}
}
```
这个示例程序使用了Java Swing来创建一个图形化界面,其中包含了用户名、密码、笔记内容、共享状态等组件。我们使用JDBC API来连接MySQL数据库,并将笔记的元数据存储在notes表中,将共享的笔记存储在notes_on_square表中。
在保存笔记时,程序会将笔记内容保存为文件,并将笔记元数据插入到notes表中。在发布笔记时,程序会将笔记内容保存为文件,并将笔记元数据插入到notes表中,并将共享的笔记插入到notes_on_square表中。
同时,在保存和发布笔记时,程序会检查用户名和密码是否正确,以确保用户已登录。
希望这个示例程序对你有所帮助!
阅读全文