Browse Source

Zeilensortierung

remotes/origin/server
Marc Dimmerling 11 months ago
parent
commit
ad1a16fe26
  1. 59
      src/main/java/Notizbuch.java

59
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());
}
}
}
Loading…
Cancel
Save