commit 18c1e33942f3fead29187459019a4124a8809580 Author: FUH22860 Date: Tue May 17 12:18:43 2022 +0200 Initial Commit diff --git a/src/application/Main.java b/src/application/Main.java new file mode 100644 index 0000000..6996f83 --- /dev/null +++ b/src/application/Main.java @@ -0,0 +1,43 @@ +package application; + +import javafx.application.Application; +import javafx.stage.Stage; +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.control.ButtonType; + +public class Main extends Application { + public void start(Stage primaryStage) { + try { + RootFlowPane root = new RootFlowPane(); + Scene scene = new Scene(root, 700, 500); + primaryStage.setScene(scene); + primaryStage.setTitle("GUI-Uebung 3"); + primaryStage.show(); + } catch (Exception e) { + showAlert(AlertType.ERROR, e.getMessage()); + } + } + + public static void showAlert(AlertType alertType, String message) { + if (message != null) { + Alert alert = new Alert(alertType, message, ButtonType.OK); + alert.setHeaderText(null); + if (alertType == AlertType.WARNING) + alert.setTitle("Warnung"); + else + alert.setTitle("Hinweis-Meldung"); + alert.showAndWait(); + } else { + Alert alert = new Alert(alertType, "Bitte waehlen Sie \"Weiter\" aus", ButtonType.NEXT); + alert.setHeaderText(null); + alert.setTitle("Hinweis-Meldung"); + alert.showAndWait(); + } + } + + public static void main(String[] args) { + launch(args); + } +} diff --git a/src/application/RootFlowPane.java b/src/application/RootFlowPane.java new file mode 100644 index 0000000..85a071a --- /dev/null +++ b/src/application/RootFlowPane.java @@ -0,0 +1,83 @@ +package application; + +import javafx.application.Platform; +import javafx.geometry.Pos; +import javafx.scene.control.Button; +import javafx.scene.control.CheckBox; +import javafx.scene.control.RadioButton; +import javafx.scene.control.ToggleGroup; +import javafx.scene.control.Alert.AlertType; +import javafx.scene.layout.FlowPane; + +public class RootFlowPane extends FlowPane { + private CheckBox cb; + private RadioButton rb1, rb2, rb3; + private Button btOk, btBeenden; + private ToggleGroup tgRadios; + + public RootFlowPane() { + initComponents(); + addComponents(); + addHandlers(); + } + + private void initComponents() { + cb = new CheckBox("Waehle mich"); + + rb1 = new RadioButton("RB1"); + rb2 = new RadioButton("RB2"); + rb3 = new RadioButton("RB3"); + + tgRadios = new ToggleGroup(); + + btOk = new Button("OK"); + btOk.setPrefWidth(100); + btBeenden = new Button("Beenden"); + btBeenden.setPrefWidth(100); + + setAlignment(Pos.CENTER); + setHgap(20); + setVgap(20); + } + + private void addComponents() { + tgRadios.getToggles().addAll(rb1, rb2, rb3); + tgRadios.selectToggle(rb1); + + getChildren().addAll(cb, rb1, rb2, rb3, btOk, btBeenden); + } + + private void addHandlers() { + + btOk.setOnAction(event -> okClicked()); + btBeenden.setOnAction(event -> beenden()); + + setOnMouseClicked(event -> flowPaneClicked()); // this = RootFlowPane -> das sind wir + } + + // ------------------------ handlers --------------------- + + private void okClicked() { + + StringBuilder sb = new StringBuilder(); + sb.append("Auswertung zu den Buttons:\n"); + sb.append("Checkbox ist "); + if (!cb.isSelected()) + sb.append("nicht "); + sb.append("selected;\n"); + + sb.append("Ausgewaehlter RadioButton: "); + RadioButton rbSelected = (RadioButton) tgRadios.getSelectedToggle(); + sb.append(rbSelected.getText()); + + Main.showAlert(AlertType.INFORMATION, sb.toString()); + } + + private void beenden() { + Platform.exit(); + } + + private void flowPaneClicked() { + Main.showAlert(AlertType.WARNING, "Es wurde irgendwo ins Fenster mit der Maus geklickt"); + } +}