|
@ -6,67 +6,62 @@ import java.awt.event.*; |
|
|
import java.io.*; |
|
|
import java.io.*; |
|
|
import javax.swing.JFrame; |
|
|
import javax.swing.JFrame; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Notizbuch extends JFrame implements ActionListener { |
|
|
public class Notizbuch extends JFrame implements ActionListener { |
|
|
|
|
|
|
|
|
private JTextArea textArea; |
|
|
private JTextArea textArea; |
|
|
private JButton saveButton; |
|
|
private JButton saveButton; |
|
|
private JButton loadButton; |
|
|
private JButton loadButton; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Notizbuch() { |
|
|
public Notizbuch() { |
|
|
setTitle("Notizbuch"); |
|
|
setTitle("Notizbuch"); |
|
|
setSize(400, 300); |
|
|
setSize(400, 300); |
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE); |
|
|
setDefaultCloseOperation(EXIT_ON_CLOSE); |
|
|
setLayout(new BorderLayout()); |
|
|
setLayout(new BorderLayout()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
textArea = new JTextArea(); |
|
|
textArea = new JTextArea(); |
|
|
add(new JScrollPane(textArea), BorderLayout.CENTER); |
|
|
add(new JScrollPane(textArea), BorderLayout.CENTER); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
saveButton = new JButton("Speichern"); |
|
|
saveButton = new JButton("Speichern"); |
|
|
saveButton.addActionListener(this); |
|
|
saveButton.addActionListener(this); |
|
|
loadButton = new JButton("Laden"); |
|
|
loadButton = new JButton("Laden"); |
|
|
loadButton.addActionListener(this); |
|
|
loadButton.addActionListener(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JPanel buttonPanel = new JPanel(); |
|
|
JPanel buttonPanel = new JPanel(); |
|
|
buttonPanel.add(saveButton); |
|
|
buttonPanel.add(saveButton); |
|
|
buttonPanel.add(loadButton); |
|
|
|
|
|
|
|
|
|
|
|
add(buttonPanel, BorderLayout.SOUTH); |
|
|
|
|
|
|
|
|
buttonPanel.add(loadButton); |
|
|
|
|
|
|
|
|
|
|
|
add(buttonPanel, BorderLayout.SOUTH); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
public void actionPerformed(ActionEvent e) { |
|
|
if (e.getSource() == saveButton) { |
|
|
if (e.getSource() == saveButton) { |
|
|
saveNote(); |
|
|
|
|
|
} else if (e.getSource() == loadButton) { |
|
|
|
|
|
loadNote(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
saveNote(); |
|
|
|
|
|
} else if (e.getSource() == loadButton) { |
|
|
|
|
|
loadNote(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void saveNote() { |
|
|
private void saveNote() { |
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) { |
|
|
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() { |
|
|
private void loadNote() { |
|
|
try (BufferedReader reader = new BufferedReader(new FileReader("notizen.txt"))) { |
|
|
try (BufferedReader reader = new BufferedReader(new FileReader("notizen.txt"))) { |
|
|
StringBuilder noteText = new StringBuilder(); |
|
|
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()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |