Browse Source

Merge branch 'JSrollBar' into 'main'

J sroll bar

See merge request fdai7332/java-chat!15
remotes/origin/hotfix/client/improve-chat-window-appearence
fdai7579 11 months ago
parent
commit
b120ad3b2e
  1. 19
      src/main/java/Notizbuch.java

19
src/main/java/Notizbuch.java

@ -20,12 +20,14 @@ public class Notizbuch extends JFrame implements ActionListener {
setTextArea(new JTextArea()); setTextArea(new JTextArea());
add(new JScrollPane(getTextArea()), BorderLayout.CENTER); add(new JScrollPane(getTextArea()), BorderLayout.CENTER);
// Erstellen der Speichern- und Laden-Buttons
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);
// Erstellen eines Panels für die Buttons und Hinzufügen der Buttons
JPanel buttonPanel = new JPanel(); JPanel buttonPanel = new JPanel();
buttonPanel.add(saveButton); buttonPanel.add(saveButton);
buttonPanel.add(loadButton); buttonPanel.add(loadButton);
@ -35,6 +37,7 @@ public class Notizbuch extends JFrame implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
// ActionListener-Methode für Button-Events
if (e.getSource() == saveButton) { if (e.getSource() == saveButton) {
saveNote(); saveNote();
} else if (e.getSource() == loadButton) { } else if (e.getSource() == loadButton) {
@ -42,7 +45,7 @@ public class Notizbuch extends JFrame implements ActionListener {
} }
} }
// Methode zum Speichern von Notizen
public void saveNote() { public void saveNote() {
try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) { try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) {
writer.print(getTextArea().getText()); writer.print(getTextArea().getText());
@ -51,27 +54,27 @@ public class Notizbuch extends JFrame implements ActionListener {
JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Notiz: " + e.getMessage()); JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Notiz: " + e.getMessage());
} }
} }
// Methode zum Laden von Notizen
public void loadNote() { public 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; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
noteText.append(line).append("\n");
noteText.append(line).append("\n"); // Zeilen aus der Datei lesen und StringBuilder hinzufügen
} }
getTextArea().setText(noteText.toString()); getTextArea().setText(noteText.toString());
} catch (IOException e) { } catch (IOException e) {
JOptionPane.showMessageDialog(this, "Fehler beim Laden der Notiz: " + e.getMessage()); JOptionPane.showMessageDialog(this, "Fehler beim Laden der Notiz: " + e.getMessage());
} }
} }
// Hauptmethode zum Starten des Programms
public static void main(String[] args) { public static void main(String[] args) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
Notizbuch notizbuch = new Notizbuch(); Notizbuch notizbuch = new Notizbuch();
notizbuch.setVisible(true);
notizbuch.setVisible(true); // Das Notizbuch-Fenster anzeigen
}); });
} }
// Getter und Setter für die TextArea
public JTextArea getTextArea() { public JTextArea getTextArea() {
return textArea; return textArea;
} }

Loading…
Cancel
Save