diff --git a/src/main/java/Notizbuch.java b/src/main/java/Notizbuch.java new file mode 100644 index 0000000..caffb68 --- /dev/null +++ b/src/main/java/Notizbuch.java @@ -0,0 +1,67 @@ +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.io.*; +import javax.swing.JFrame; + +public class Notizbuch extends JFrame implements ActionListener { + + private JTextArea textArea; + private JButton saveButton; + private JButton loadButton; + + public Notizbuch() { + setTitle("Notizbuch"); + setSize(400, 300); + setDefaultCloseOperation(EXIT_ON_CLOSE); + setLayout(new BorderLayout()); + + textArea = new JTextArea(); + add(new JScrollPane(textArea), BorderLayout.CENTER); + + saveButton = new JButton("Speichern"); + saveButton.addActionListener(this); + loadButton = new JButton("Laden"); + loadButton.addActionListener(this); + + JPanel buttonPanel = new JPanel(); + buttonPanel.add(saveButton); + buttonPanel.add(loadButton); + + add(buttonPanel, BorderLayout.SOUTH); + } + + @Override + public void actionPerformed(ActionEvent e) { + if (e.getSource() == saveButton) { + saveNote(); + } else if (e.getSource() == loadButton) { + loadNote(); + } + + } + + private void saveNote() { + try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) { + writer.print(textArea.getText()); + JOptionPane.showMessageDialog(this, "Notiz erfolgreich gespeichert!"); + } catch (IOException e) { + JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Notiz: " + e.getMessage()); + } + } + + private void loadNote() { + try (BufferedReader reader = new BufferedReader(new FileReader("notizen.txt"))) { + StringBuilder noteText = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + noteText.append(line).append("\n"); + } + textArea.setText(noteText.toString()); + } catch (IOException e) { + JOptionPane.showMessageDialog(this, "Fehler beim Laden der Notiz: " + e.getMessage()); + } + } +}