Event Handling & ToogleGroup & Formatting

This commit is contained in:
FUH22860 2022-05-17 12:01:18 +02:00
parent 5391573e08
commit e267cd9245
2 changed files with 71 additions and 21 deletions

View File

@ -3,17 +3,37 @@ 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 {
@Override
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) {
e.printStackTrace();
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();
}
}

View File

@ -1,53 +1,83 @@
package application;
import javafx.geometry.Insets;
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 Button btOK, btBeenden;
private CheckBox cbWaehleMich;
private CheckBox cb;
private RadioButton rb1, rb2, rb3;
private Button btOk, btBeenden;
private ToggleGroup tgRadios;
public RootFlowPane() {
initComponents();
addComponents();
addHandler();
addHandlers();
}
private void initComponents() {
tgRadios = new ToggleGroup();
btOK = new Button("OK");
btBeenden = new Button("Beenden");
btOK.setPrefWidth(100);
btBeenden.setPrefWidth(100);
cbWaehleMich = new CheckBox("Waehle Mich!");
cb = new CheckBox("Waehle mich");
rb1 = new RadioButton("RB1");
rb2 = new RadioButton("RB2");
rb3 = new RadioButton("RB3");
tgRadios.getToggles().addAll(rb1, rb2, rb3);
tgRadios = new ToggleGroup();
btOk = new Button("OK");
btOk.setPrefWidth(100);
btBeenden = new Button("Beenden");
btBeenden.setPrefWidth(100);
setPadding(new Insets(10));
setAlignment(Pos.CENTER);
setVgap(10);
setHgap(10);
setHgap(20);
setVgap(20);
}
private void addComponents() {
getChildren().addAll(cbWaehleMich, rb1, rb2, rb3, btOK, btBeenden);
tgRadios.getToggles().addAll(rb1, rb2, rb3);
tgRadios.selectToggle(rb1);
getChildren().addAll(cb, rb1, rb2, rb3, btOk, btBeenden);
}
private void addHandler() {
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");
}
}