From 116cb37c92d0d1e333ad1375ee77d84d33916f99 Mon Sep 17 00:00:00 2001 From: FUH22860 Date: Thu, 12 May 2022 13:03:16 +0200 Subject: [PATCH] Event Handling --- src/application/Main.java | 19 +++-- src/application/RootGridPane.java | 119 ++++++++++++++++++++++++------ 2 files changed, 109 insertions(+), 29 deletions(-) diff --git a/src/application/Main.java b/src/application/Main.java index 0ad67cf..23a304a 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -3,22 +3,31 @@ package application; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; -//import javafx.scene.layout.BorderPane; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; public class Main extends Application { public void start(Stage primaryStage) { try { - RootGridPane root = new RootGridPane(); // BorderPane machen und wahrscheinlich FlowPane - // BorderPane root = new BorderPane(); + RootGridPane root = new RootGridPane(); Scene scene = new Scene(root, 400, 400); + + primaryStage.setTitle("Erstes Fenster"); primaryStage.setScene(scene); - primaryStage.setTitle("GUI Übung 2"); primaryStage.show(); + } catch (Exception e) { - e.printStackTrace(); + e.getStackTrace(); } } + public static void showAlert(String message) { + Alert alert = new Alert(AlertType.INFORMATION, message); + alert.setHeaderText(null); + alert.setTitle(null); + alert.showAndWait(); + } + public static void main(String[] args) { launch(args); } diff --git a/src/application/RootGridPane.java b/src/application/RootGridPane.java index 4c68136..bf5e7a7 100644 --- a/src/application/RootGridPane.java +++ b/src/application/RootGridPane.java @@ -1,43 +1,114 @@ package application; +import javafx.application.Platform; +import javafx.event.ActionEvent; +import javafx.event.EventHandler; +import javafx.geometry.HPos; import javafx.geometry.Insets; -import javafx.scene.control.RadioButton; -import javafx.scene.control.ToggleGroup; +import javafx.geometry.Pos; +import javafx.geometry.VPos; +import javafx.scene.control.Button; +import javafx.scene.control.Label; +import javafx.scene.layout.FlowPane; import javafx.scene.layout.GridPane; public class RootGridPane extends GridPane { -// Button btButton1; -// Button btLaden; -// Button btSpeichern; - - private RadioButton radbtLeft, radbtCenter, radbtRight; + private Button btButton1, btLaden, btBeenden; + private Button btButton4, btButton5, btButton6, btButtonSieben, btButtonAcht; + private Button btBack, btBack2; + private Label lbErsetzt; + private FlowPane fpRechtsUnten; public RootGridPane() { initComponents(); addComponents(); + addHandler(); } public void initComponents() { - - radbtLeft = new RadioButton(""); - radbtLeft.setPadding(new Insets(10)); - - - radbtCenter = new RadioButton(""); - radbtCenter.setPadding(new Insets(10)); - - - radbtRight = new RadioButton(""); - radbtRight.setPadding(new Insets(10)); - + btButton1 = new Button("Button1"); + btButton1.setPrefSize(100, 100); + btButton1.setAlignment(Pos.BOTTOM_RIGHT); + + btLaden = new Button("Laden-Button"); + btLaden.setPrefSize(200, 200); + btLaden.setAlignment(Pos.TOP_CENTER); + + btBeenden = new Button("Beenden-Button"); + btBeenden.setPrefSize(200, 200); + btBeenden.setAlignment(Pos.BOTTOM_LEFT); + + btButton4 = new Button("Button4"); + btButton5 = new Button("Button5"); + btButton6 = new Button("Button6"); + btButtonSieben = new Button("ButtonSieben"); + btButtonAcht = new Button("ButtonAcht"); + + fpRechtsUnten = new FlowPane(); + fpRechtsUnten.setPrefSize(200, 200); + fpRechtsUnten.getChildren().addAll(btButton4, btButton5, btButton6, btButtonSieben, btButtonAcht); + fpRechtsUnten.setAlignment(Pos.CENTER_LEFT); + fpRechtsUnten.setPadding(new Insets(10)); + fpRechtsUnten.setVgap(10); + fpRechtsUnten.setHgap(10); + + GridPane.setHalignment(btButton1, HPos.CENTER); + GridPane.setValignment(btButton1, VPos.BOTTOM); + } public void addComponents() { - - add(radbtLeft, 0, 0); - add(radbtCenter, 0, 0); - add(radbtRight, 0, 0); - + add(btButton1, 0, 0); + add(btLaden, 1, 0); + add(btBeenden, 0, 1); + add(fpRechtsUnten, 1, 1); } + + private void addHandler() { + + btBack = new Button("Back"); + btBack2 = new Button("Back"); + lbErsetzt = new Label("Ersetzt!"); + + btButton1.setOnAction(new EventHandler() { + public void handle(ActionEvent event) { + getChildren().remove(btButton1); + getChildren().add(btBack2); + Main.showAlert("Bin " + btButton1.getText()); + } + }); + + btBack2.setOnAction(new EventHandler() { + public void handle(ActionEvent event) { + getChildren().remove(btBack2); + getChildren().add(btButton1); + } + }); + + btBack.setOnAction(new EventHandler() { + public void handle(ActionEvent event) { + fpRechtsUnten.getChildren().setAll(btButton4, btButton5, btButton6, btButtonSieben, btButtonAcht); + } + }); + + btButton4.setOnAction(new EventHandler() { + public void handle(ActionEvent event) { + fpRechtsUnten.getChildren().setAll(btBack); + } + }); + + btLaden.setOnAction(new EventHandler() { + public void handle(ActionEvent arg0) { + Main.showAlert("Loading in progress..."); + } + }); + + btBeenden.setOnAction(event -> beenden()); + } + + private void beenden() { + Platform.exit(); + } + }