JAVA课程设计代码1.笔记共享平台: 要求:使用图形化界面,用户通过注册账号(用户名和密码)后,登录笔记共享平台。 1.可以编写自己的笔记 2.可以保存自己的笔记,并记录最后一次保存的时间 3.设置笔记的状态(共享or私有) 4.若笔记共享则发布到笔记广场,供其他用户参阅,运用Java生成完整代码,并给出代码思路
时间: 2024-03-20 09:43:34 浏览: 111
JAVA课程设计代码
生成代码思路:
1. 创建Java项目,添加GUI库,如JavaFX或Swing。
2. 创建用户类,包含用户名和密码属性,以及笔记列表属性。
3. 创建笔记类,包含笔记内容、状态(共享/私有)、作者和最后保存时间等属性。
4. 创建笔记广场类,包含一个笔记列表,用于存储共享的笔记。
5. 创建登录窗口,用户可以输入用户名和密码进行登录或注册。
6. 创建笔记编辑窗口,用户可以编写自己的笔记、设置笔记状态、保存笔记等操作。
7. 创建笔记广场窗口,用户可以查看其他用户共享的笔记。
完整代码如下:
User.java
```
import java.util.ArrayList;
import java.util.List;
public class User {
private String username;
private String password;
private List<Note> notes;
public User(String username, String password) {
this.username = username;
this.password = password;
this.notes = new ArrayList<>();
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public List<Note> getNotes() {
return notes;
}
public void addNote(Note note) {
notes.add(note);
}
public void removeNote(Note note) {
notes.remove(note);
}
}
```
Note.java
```
import java.time.LocalDateTime;
public class Note {
private String content;
private boolean isShared;
private User author;
private LocalDateTime lastSavedTime;
public Note(String content, boolean isShared, User author) {
this.content = content;
this.isShared = isShared;
this.author = author;
this.lastSavedTime = LocalDateTime.now();
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
this.lastSavedTime = LocalDateTime.now();
}
public boolean isShared() {
return isShared;
}
public void setShared(boolean shared) {
isShared = shared;
}
public User getAuthor() {
return author;
}
public LocalDateTime getLastSavedTime() {
return lastSavedTime;
}
}
```
NoteSquare.java
```
import java.util.ArrayList;
import java.util.List;
public class NoteSquare {
private List<Note> notes;
public NoteSquare() {
this.notes = new ArrayList<>();
}
public List<Note> getNotes() {
return notes;
}
public void addNote(Note note) {
notes.add(note);
}
public void removeNote(Note note) {
notes.remove(note);
}
}
```
LoginWindow.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginWindow extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton registerButton;
public LoginWindow() {
initComponents();
setupLayout();
setupListeners();
}
private void initComponents() {
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
loginButton = new JButton("登录");
registerButton = new JButton("注册");
}
private void setupLayout() {
JPanel panel = new JPanel(new GridLayout(3, 2, 5, 5));
panel.add(new JLabel("用户名:"));
panel.add(usernameField);
panel.add(new JLabel("密码:"));
panel.add(passwordField);
panel.add(loginButton);
panel.add(registerButton);
setContentPane(panel);
setTitle("登录");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(null);
}
private void setupListeners() {
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
// TODO: 根据用户名和密码进行登录验证
}
});
registerButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = String.valueOf(passwordField.getPassword());
// TODO: 注册新用户
}
});
}
}
```
NoteEditorWindow.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NoteEditorWindow extends JFrame {
private JTextArea contentArea;
private JCheckBox shareCheckBox;
private JButton saveButton;
private Note note;
public NoteEditorWindow(User user, Note note) {
this.note = note;
initComponents();
setupLayout();
setupListeners();
}
private void initComponents() {
contentArea = new JTextArea(note.getContent(), 20, 40);
shareCheckBox = new JCheckBox("共享", note.isShared());
saveButton = new JButton("保存");
}
private void setupLayout() {
JPanel panel = new JPanel(new BorderLayout(5, 5));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(shareCheckBox);
buttonPanel.add(saveButton);
panel.add(new JScrollPane(contentArea), BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(panel);
setTitle("编辑笔记");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(null);
}
private void setupListeners() {
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String content = contentArea.getText();
boolean isShared = shareCheckBox.isSelected();
note.setContent(content);
note.setShared(isShared);
// TODO: 更新最后保存时间
}
});
}
}
```
NoteSquareWindow.java
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NoteSquareWindow extends JFrame {
private JList<Note> noteList;
private JButton viewButton;
public NoteSquareWindow(User user, NoteSquare noteSquare) {
initComponents();
setupLayout();
setupListeners();
}
private void initComponents() {
DefaultListModel<Note> noteListModel = new DefaultListModel<>();
for (Note note : noteSquare.getNotes()) {
if (note.isShared()) {
noteListModel.addElement(note);
}
}
noteList = new JList<>(noteListModel);
viewButton = new JButton("查看");
}
private void setupLayout() {
JPanel panel = new JPanel(new BorderLayout(5, 5));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(viewButton);
panel.add(new JScrollPane(noteList), BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(panel);
setTitle("笔记广场");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setResizable(false);
setLocationRelativeTo(null);
}
private void setupListeners() {
viewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Note note = noteList.getSelectedValue();
if (note != null) {
NoteEditorWindow editorWindow = new NoteEditorWindow(note.getAuthor(), note);
editorWindow.setVisible(true);
}
}
});
}
}
```
运行程序的入口类Main.java
```
public class Main {
private static User currentUser;
private static NoteSquare noteSquare;
public static void main(String[] args) {
// 初始化数据
init();
// 显示登录窗口
LoginWindow loginWindow = new LoginWindow();
loginWindow.setVisible(true);
}
private static void init() {
// 初始化笔记广场和用户数据
noteSquare = new NoteSquare();
User user1 = new User("user1", "password1");
Note note1 = new Note("Hello, World!", true, user1);
user1.addNote(note1);
noteSquare.addNote(note1);
User user2 = new User("user2", "password2");
Note note2 = new Note("This is my note.", false, user2);
user2.addNote(note2);
currentUser = null;
}
public static User getCurrentUser() {
return currentUser;
}
public static void setCurrentUser(User user) {
currentUser = user;
}
public static NoteSquare getNoteSquare() {
return noteSquare;
}
}
```
以上代码实现了一个简单的笔记共享平台,包括用户登录、笔记编辑、笔记广场等功能。用户可以注册账号并登录,编写自己的笔记并保存,设置笔记的状态为共享或私有,共享的笔记会发布到笔记广场供其他用户参阅。
阅读全文