JavaFX Mehrere JavaFX Szenen mit einem Menü

Luckshotx

Mitglied
Das Hauptproblem sich schon erledigt. Allerdings besteht ein weiteres Problem, dass die im center geladene Szene leider nach unten verschoben ist. Jemand eine Idee wie ich das ändern kann?

Hallo,
seit einigen Tagen versuche ich, eine grafische Oberfläche zu erstellen, bei der ich über ein gemeinsames Menü das aus Buttons besteht die Szene zu wechseln. Dabei habe ich ein BorderPane als root Pane genommen und versuche das zentrale Pane auszuwechseln. Leider bekomme ich sobald ich nach der Ausführung einen Button klicke eine Exception. Ich hoffe, dass einer von euch den Fehler erkennt oder die Exception deuten kann.

Main.java
Code:
package main;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.net.URL;

public class Main extends Application {

    // Creating a static root to pass to the controller
    private static BorderPane root = new BorderPane();

    // Get root for the controller
    public static BorderPane getRoot() {
        return root;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        // Loading FXML resources
        URL menuUrl = getClass().getResource("Menu.fxml");
        VBox menu = FXMLLoader.load( menuUrl );

        URL dashboardUrl = getClass().getResource("Dashboard.fxml");
        HBox dashboard = FXMLLoader.load( dashboardUrl );

        // Constructing the scene using the static root
        root.setLeft(menu);
        root.setCenter(dashboard);

        Scene scene = new Scene(root, 640, 480);
        scene
                .getStylesheets()
                .add(getClass()
                        .getResource("main.css")
                        .toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

MenuControl.java
Code:
package main;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;


public class MenuControl {

    @FXML
    private Button displayDashboard;

    @FXML
    private Button displayChatbot;

    @FXML
    void switchToDashboard() {

        try {

            URL dashboardUrl = getClass().getResource("Dashboard.fxml");
            HBox dashboard = FXMLLoader.load( dashboardUrl );

            BorderPane border = Main.getRoot();
            border.setCenter(dashboard);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @FXML
    void switchToChatbot() {

        try {

            URL chatbotUrl = getClass().getResource("Chatbot.fxml");
            HBox chatbot = FXMLLoader.load( chatbotUrl );

            BorderPane border = Main.getRoot();
            border.setCenter(chatbot);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Menu.fxml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.VBox?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.MenuControl">
   <children>
      <Button fx:id="displayDashboard" mnemonicParsing="false" onMouseClicked="#switchToDashboard" text="Dashboard" />
      <Button fx:id="displayChatbot" mnemonicParsing="false" onMouseClicked="#switchToChatbot" text="Chatbot" />
   </children>
</VBox>

Dashboard.fxml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label text="Dashboard" />
   </children>
</HBox>

Chatbot.fxml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>


<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <Label text="Chatbot" />
   </children>
</HBox>
 
Zuletzt bearbeitet:

Neue Themen


Zurück
Oben