Browse Source

Test1

remotes/origin/hotfix/client/improve-chat-window-appearence
Marc Dimmerling 11 months ago
parent
commit
d92996d0dc
  1. 22
      src/main/java/Notizbuch.java
  2. 21
      src/test/java/NotizbuchTest.java

22
src/main/java/Notizbuch.java

@ -8,7 +8,7 @@ import javax.swing.JFrame;
public class Notizbuch extends JFrame implements ActionListener {
private JTextArea textArea;
JTextArea textArea;
private JButton saveButton;
private JButton loadButton;
@ -18,8 +18,8 @@ public class Notizbuch extends JFrame implements ActionListener {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
setTextArea(new JTextArea());
add(new JScrollPane(getTextArea()), BorderLayout.CENTER);
saveButton = new JButton("Speichern");
saveButton.addActionListener(this);
@ -43,23 +43,23 @@ public class Notizbuch extends JFrame implements ActionListener {
}
private void saveNote() {
public void saveNote() {
try (PrintWriter writer = new PrintWriter(new FileWriter("notizen.txt"))) {
writer.print(textArea.getText());
writer.print(getTextArea().getText());
JOptionPane.showMessageDialog(this, "Notiz erfolgreich gespeichert!");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Fehler beim Speichern der Notiz: " + e.getMessage());
}
}
private void loadNote() {
public 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());
getTextArea().setText(noteText.toString());
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Fehler beim Laden der Notiz: " + e.getMessage());
}
@ -71,4 +71,12 @@ public class Notizbuch extends JFrame implements ActionListener {
notizbuch.setVisible(true);
});
}
public JTextArea getTextArea() {
return textArea;
}
public void setTextArea(JTextArea textArea) {
this.textArea = textArea;
}
}

21
src/test/java/NotizbuchTest.java

@ -0,0 +1,21 @@
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class NotizbuchTest {
@Test
public void testSpeichernUndLaden() {
Notizbuch notizbuch = new Notizbuch();
notizbuch.textArea.setText("Testnotiz");
notizbuch.saveNote();
notizbuch.loadNote();
assertEquals("Testnotiz\n", notizbuch.textArea.getText());
}
}
Loading…
Cancel
Save