Event Handling

This commit is contained in:
FUH22860 2022-05-12 13:03:16 +02:00
parent 05194000c1
commit 116cb37c92
2 changed files with 109 additions and 29 deletions

View File

@ -3,22 +3,31 @@ package application;
import javafx.application.Application; import javafx.application.Application;
import javafx.stage.Stage; import javafx.stage.Stage;
import javafx.scene.Scene; 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 class Main extends Application {
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
try { try {
RootGridPane root = new RootGridPane(); // BorderPane machen und wahrscheinlich FlowPane RootGridPane root = new RootGridPane();
// BorderPane root = new BorderPane();
Scene scene = new Scene(root, 400, 400); Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Erstes Fenster");
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.setTitle("GUI Übung 2");
primaryStage.show(); primaryStage.show();
} catch (Exception e) { } 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) { public static void main(String[] args) {
launch(args); launch(args);
} }

View File

@ -1,43 +1,114 @@
package application; package application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.scene.control.RadioButton; import javafx.geometry.Pos;
import javafx.scene.control.ToggleGroup; import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane; import javafx.scene.layout.GridPane;
public class RootGridPane extends GridPane { public class RootGridPane extends GridPane {
// Button btButton1; private Button btButton1, btLaden, btBeenden;
// Button btLaden; private Button btButton4, btButton5, btButton6, btButtonSieben, btButtonAcht;
// Button btSpeichern; private Button btBack, btBack2;
private Label lbErsetzt;
private RadioButton radbtLeft, radbtCenter, radbtRight; private FlowPane fpRechtsUnten;
public RootGridPane() { public RootGridPane() {
initComponents(); initComponents();
addComponents(); addComponents();
addHandler();
} }
public void initComponents() { public void initComponents() {
btButton1 = new Button("Button1");
radbtLeft = new RadioButton(""); btButton1.setPrefSize(100, 100);
radbtLeft.setPadding(new Insets(10)); btButton1.setAlignment(Pos.BOTTOM_RIGHT);
btLaden = new Button("Laden-Button");
radbtCenter = new RadioButton(""); btLaden.setPrefSize(200, 200);
radbtCenter.setPadding(new Insets(10)); btLaden.setAlignment(Pos.TOP_CENTER);
btBeenden = new Button("Beenden-Button");
radbtRight = new RadioButton(""); btBeenden.setPrefSize(200, 200);
radbtRight.setPadding(new Insets(10)); 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() { public void addComponents() {
add(btButton1, 0, 0);
add(radbtLeft, 0, 0); add(btLaden, 1, 0);
add(radbtCenter, 0, 0); add(btBeenden, 0, 1);
add(radbtRight, 0, 0); add(fpRechtsUnten, 1, 1);
} }
private void addHandler() {
btBack = new Button("Back");
btBack2 = new Button("Back");
lbErsetzt = new Label("Ersetzt!");
btButton1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
getChildren().remove(btButton1);
getChildren().add(btBack2);
Main.showAlert("Bin " + btButton1.getText());
}
});
btBack2.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
getChildren().remove(btBack2);
getChildren().add(btButton1);
}
});
btBack.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fpRechtsUnten.getChildren().setAll(btButton4, btButton5, btButton6, btButtonSieben, btButtonAcht);
}
});
btButton4.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
fpRechtsUnten.getChildren().setAll(btBack);
}
});
btLaden.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent arg0) {
Main.showAlert("Loading in progress...");
}
});
btBeenden.setOnAction(event -> beenden());
}
private void beenden() {
Platform.exit();
}
} }