summaryrefslogtreecommitdiff
path: root/Java-Kompendium/kap14/1/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'Java-Kompendium/kap14/1/src/main/java')
-rw-r--r--Java-Kompendium/kap14/1/src/main/java/EnterHandler.java31
-rw-r--r--Java-Kompendium/kap14/1/src/main/java/Task1.java40
2 files changed, 71 insertions, 0 deletions
diff --git a/Java-Kompendium/kap14/1/src/main/java/EnterHandler.java b/Java-Kompendium/kap14/1/src/main/java/EnterHandler.java
new file mode 100644
index 0000000..f612707
--- /dev/null
+++ b/Java-Kompendium/kap14/1/src/main/java/EnterHandler.java
@@ -0,0 +1,31 @@
+import java.io.*;
+
+import javafx.event.ActionEvent;
+import javafx.event.EventHandler;
+import javafx.scene.control.Alert;
+import javafx.scene.control.TextField;
+import javafx.scene.control.Alert.AlertType;
+
+public final class EnterHandler implements EventHandler<ActionEvent> {
+ private BufferedWriter outputStream;
+ private TextField inputField;
+
+ public EnterHandler(TextField input, BufferedWriter output) {
+ this.outputStream = output;
+ this.inputField = input;
+ }
+
+ public void handle(ActionEvent event) {
+ Alert alert;
+ try {
+ this.outputStream.write(inputField.getText() + "\n");
+ } catch (IOException exception) {
+ alert = new Alert(AlertType.ERROR, exception.getMessage());
+ alert.showAndWait();
+ return;
+ }
+ alert = new Alert(AlertType.INFORMATION, "Gespeichert");
+ alert.showAndWait();
+ inputField.setText("");
+ }
+}
diff --git a/Java-Kompendium/kap14/1/src/main/java/Task1.java b/Java-Kompendium/kap14/1/src/main/java/Task1.java
new file mode 100644
index 0000000..14ced3a
--- /dev/null
+++ b/Java-Kompendium/kap14/1/src/main/java/Task1.java
@@ -0,0 +1,40 @@
+import java.io.*;
+
+import javafx.scene.Scene;
+import javafx.scene.control.TextField;
+import javafx.scene.layout.StackPane;
+import javafx.application.Application;
+import javafx.stage.Stage;
+import javafx.scene.control.Alert;
+import javafx.scene.control.Alert.AlertType;
+
+public class Task1 extends Application {
+ BufferedWriter output = null;
+
+ @Override
+ public void start(Stage stage) {
+ var userInput = new TextField();
+ var root = new StackPane(userInput);
+ var scene = new Scene(root, 400, 300);
+
+ stage.setTitle("Geben Sie Ihre Notiz ein:");
+ stage.setScene(scene);
+
+ try {
+ this.output = new BufferedWriter(new FileWriter("Notizen.txt", true));
+
+ userInput.setOnAction(new EnterHandler(userInput, output));
+ } catch (IOException exception) {
+ var alert = new Alert(AlertType.ERROR, exception.getMessage());
+ alert.showAndWait();
+ }
+ stage.show();
+ }
+
+ @Override
+ public void stop() throws IOException {
+ if (this.output != null) {
+ output.close();
+ }
+ }
+}