对于我的学校项目,我正在尝试用 Java 创建一个正常运行的笔记本项目,当我尝试更新我的 GUI 中的现有笔记时,我遇到了问题,它不仅仅是覆盖 GUI 中的现有笔记,它只是创建一个新的,将旧的注释留在菜单中。所以就像我遇到的问题一样,我也不想要重复的笔记标题。
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class NotizbuchGUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JTextField titleField;
private JList<Notiz> noteList;
private Notizbuch notizbuch;
public NotizbuchGUI() {
super("Notizbuch");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// Erstelle GUI-Komponenten
JPanel panel = new JPanel(new BorderLayout());
textArea = new JTextArea();
titleField = new JTextField();
noteList = new JList<>();
JScrollPane scrollPane = new JScrollPane(noteList);
JButton newButton = new JButton("Neue Notiz");
JButton saveButton = new JButton("Speichern");
JButton deleteButton = new JButton("Löschen");
// Füge GUI-Komponenten zum Panel hinzu
JPanel topPanel = new JPanel(new BorderLayout());
topPanel.add(new JLabel("Titel:"), BorderLayout.WEST);
topPanel.add(titleField, BorderLayout.CENTER);
topPanel.add(newButton, BorderLayout.EAST);
panel.add(topPanel, BorderLayout.NORTH);
panel.add(textArea, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(saveButton);
buttonPanel.add(deleteButton);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.add(scrollPane, BorderLayout.WEST);
add(panel);
// Erstelle Notizbuch
notizbuch = new Notizbuch();
// Fülle Notizliste
updateNoteList();
// Füge Listener hinzu
noteList.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
Notiz selectedNote = noteList.getSelectedValue();
if (selectedNote != null) {
titleField.setText(selectedNote.getTitel());
textArea.setText(selectedNote.getText());
}
}
});
newButton.addActionListener(e -> {
titleField.setText("");
textArea.setText("");
});
saveButton.addActionListener(e -> {
String titel = titleField.getText();
String text = textArea.getText();
if (!titel.isEmpty() && !text.isEmpty()) {
Notiz notiz = new Notiz(titel, text);
notizbuch.addNotiz(notiz);
updateNoteList();
saveNotiz(notiz);
}
});
deleteButton.addActionListener(e -> {
Notiz selectedNote = noteList.getSelectedValue();
if (selectedNote != null) {
notizbuch.removeNotiz(selectedNote);
updateNoteList();
deleteNotiz(selectedNote);
titleField.setText("");
textArea.setText("");
}
});
setVisible(true);
}
private void updateNoteList() {
DefaultListModel<Notiz> model = new DefaultListModel<>();
for (Notiz notiz : notizbuch.getNotizen()) {
model.addElement(notiz);
}
noteList.setModel(model);
}
private void saveNotiz(Notiz notiz) {
try {
String filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".txt";
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
ArrayList<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
boolean notizFound = false;
for (int i = 0; i < lines.size(); i += 2) {
String titel = lines.get(i);
String text = lines.get(i + 1);
if (titel.equals(notiz.getTitel())) {
writer.write(notiz.getTitel() + "\n");
writer.write(notiz.getText() + "\n");
notizFound = true;
} else {
writer.write(titel + "\n");
writer.write(text + "\n");
}
}
if (!notizFound) {
writer.write(notiz.getTitel() + "\n");
writer.write(notiz.getText() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void deleteNotiz(Notiz notiz) {
String filename = new SimpleDateFormat("yyyy-MM-dd").format(notiz.getDatum()) + ".txt";
File file = new File(filename);
if (file.exists()) {
file.delete();
}
}
public static void main(String[] args) {
new NotizbuchGUI();
}
}
我不知道如何解决这个问题并尝试更新我的保存方法:
private void saveNotiz(Notiz notiz) {
try {
String filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".txt";
File file = new File(filename);
if (!file.exists()) {
file.createNewFile();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
boolean notizFound = false;
for (int i = 0; i < lines.size(); i += 2) {
String titel = lines.get(i);
String text = lines.get(i + 1);
if (titel.equals(notiz.getTitel())) {
writer.write(notiz.getTitel() + "\n");
writer.write(notiz.getText() + "\n");
notizFound = true;
} else {
writer.write(titel + "\n");
writer.write(text + "\n");
}
}
if (!notizFound) {
writer.write(notiz.getTitel() + "\n");
writer.write(notiz.getText() + "\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}