From ad1a16fe26e1ea83e0fc07fd341192537898b9a2 Mon Sep 17 00:00:00 2001 From: Marc Dimmerling Date: Fri, 9 Feb 2024 17:23:33 +0100 Subject: [PATCH] Zeilensortierung --- src/main/java/Notizbuch.java | 59 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/main/java/Notizbuch.java b/src/main/java/Notizbuch.java index 18ef67c..caffb68 100644 --- a/src/main/java/Notizbuch.java +++ b/src/main/java/Notizbuch.java @@ -6,67 +6,62 @@ 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); + buttonPanel.add(loadButton); + + add(buttonPanel, BorderLayout.SOUTH); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == saveButton) { - saveNote(); - } else if (e.getSource() == loadButton) { - loadNote(); - } - + 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()); - } + 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()); - } + 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()); } } +}