Javafx 将新FXML文件加载到场景中时是否存在某种形式的优先级或功能问题?(采用jewelsea提供的框架)

Javafx 将新FXML文件加载到场景中时是否存在某种形式的优先级或功能问题?(采用jewelsea提供的框架),javafx,fxml,scene,actionevent,fxmlloader,Javafx,Fxml,Scene,Actionevent,Fxmlloader,我意识到只有通过清理代码,我认为我可以清理,并从我的一些控制器类中清除了我的问题所在。为了更好地表达我的问题,让优先级的概念更加清晰,我将给出一个简短的解释,然后是一些代码。设想一个界面,屏幕的一侧与界面保持一致和永久性,界面上有用于菜单选择的按钮,在选项卡之间移动,更改将fxml文件传递到相关窗格的其他阶段 通过阅读示例和问题,我理解这是可行的,并使用此链接中的框架,在vista1.fxml和vista2.fxml之间切换场景时,看到标题保持一致,我的理解是,如果标题作为场景的一部分保留在ma

我意识到只有通过清理代码,我认为我可以清理,并从我的一些控制器类中清除了我的问题所在。为了更好地表达我的问题,让优先级的概念更加清晰,我将给出一个简短的解释,然后是一些代码。设想一个界面,屏幕的一侧与界面保持一致和永久性,界面上有用于菜单选择的按钮,在选项卡之间移动,更改将fxml文件传递到相关窗格的其他阶段

通过阅读示例和问题,我理解这是可行的,并使用此链接中的框架,在vista1.fxml和vista2.fxml之间切换场景时,看到标题保持一致,我的理解是,如果标题作为场景的一部分保留在main.fxml中,则舞台将同时使用main.fxml和vista1.fxml或main.fxml和vista2.fxml,因此两个关联的控制器都存在,并将根据与之交互的窗格访问相应的方法

但情况显然并非如此,本例中的一致性窗格main.fxml没有其控制器,因为当加载另一个并创建其控制器时,它会以某种形式替换或清除另一个控制器,我现在假定是这样。我想我可以通过使用独立的窗格和菜单栏的一致性来避免代码重复,但也可以使用它的按钮

以下是一些代码示例: Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{
        stage.setTitle("Vista Viewer");

        stage.setScene(createScene(loadMainPane()));

        stage.show();
    }

    private Pane loadMainPane() throws IOException {
        FXMLLoader loader = new FXMLLoader();

        Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(
                        VistaNavigator.MAIN));

        MainController mainController = loader.getController();

        VistaNavigator.setMainController(mainController);
        VistaNavigator.loadVista(VistaNavigator.VISTA_1);

        return mainPane;
    }

    private Scene createScene(Pane mainPane) {
        Scene scene = new Scene(mainPane);

        return scene;
    }

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

package sample;

import javafx.fxml.FXMLLoader;
import java.io.IOException;

public class VistaNavigator {

    public static final String MAIN    = "main.fxml";
    public static final String VISTA_1 = "vista1.fxml";
    public static final String VISTA_2 = "vista2.fxml";
    public static final String OTHER_MENU_OPTION = "otherMenu.fxml";

    private static MainController mainController;

    public static void setMainController(MainController mainController) {
        VistaNavigator.mainController = mainController;
    }

    public static void loadVista(String fxml) {
        try {
            mainController.setVista(
                    FXMLLoader.load(
                            VistaNavigator.class.getResource(
                                    fxml
                            )
                    )
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
MainController.java

package sample;

import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.layout.StackPane;

public class MainController {

    @FXML
    private StackPane vistaHolder;

    public void setVista(Node node) {
        vistaHolder.getChildren().setAll(node);
    }

    public void homebtn() throws Exception{
        VistaNavigator.loadVista(VistaNavigator.VISTA_1);
    }

    public void otherMenuOption() throws Exception{
        VistaNavigator.loadVista(VistaNavigator.OTHER_MENU_OPTION);
    }
}
package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

public class Vista1Controller {

    @FXML
    void nextPane(ActionEvent event) {
        VistaNavigator.loadVista(VistaNavigator.VISTA_2);
    }
}
package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

public class Vista2Controller {

    @FXML
    void previousPane(ActionEvent event) {
        VistaNavigator.loadVista(VistaNavigator.VISTA_1);
    }
}
main.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="200.0" prefWidth="300.0" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.MainController">
    <children>
        <VBox prefWidth="155.0">
            <children>
                <Label fx:id="headerLabel" maxWidth="120.0" text="Header" />
            </children>
        </VBox>
        <Pane layoutY="40.0" prefHeight="200.0" prefWidth="120.0" style="-fx-background-color: #091D34;">
            <children>
                <Button fx:id="button1" layoutY="60.0" mnemonicParsing="false" onAction="#homebtn" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="Home" textFill="WHITE">
                    <font>
                        <Font name="System Bold" size="12.0" />
                    </font>
                </Button>
                <Button fx:id="button2" layoutY="110.0" mnemonicParsing="false" onAction="#otherMenuOption" prefHeight="44.0" prefWidth="120.0" style="-fx-background-color: #133863;" text="OtherMenuOption" textFill="WHITE">
                    <font>
                        <Font name="System Bold" size="12.0" />
                    </font>
                </Button>
            </children>
        </Pane>
        <StackPane fx:id="vistaHolder" VBox.vgrow="ALWAYS" />
    </children>
</AnchorPane>
vista1.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller" prefHeight="400.0" prefWidth="600.0">
    <children>
        <Pane fx:id="Vista1" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
            <Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#nextPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista2" textFill="#ffffff">
                <font>
                    <Font name="Britannic Bold" size="20.0" />
                </font>
            </Button>
        </Pane>
    </children>
</AnchorPane>
vista2.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
    <children>
        <Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
            <Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
                <font>
                    <Font name="Britannic Bold" size="20.0" />
                </font>
            </Button>
        </Pane>
    </children>
</AnchorPane>


是否有一种好的方法来维护这种设计并防止特定控制器内的代码重复?是通过继承还是通过传递参数,我不确定这与什么有可比性,也不确定这是否可能?

请回答。你的问题没有意义。加载FXML文件时,
FXMLLoader
会创建一个控制器,并创建FXML文件定义的层次结构中所有内容(控件和窗格等)的实例。例如,如果用户按下从FXML文件创建的按钮之一,则会执行控制器中创建的相应方法。如果再次加载FXML文件,将创建另一个控制器,并创建与FXML元素对应的另一个层次结构。没有“优先顺序”。控件和控制器之间只有一个关联。好吧,我试着创建一个最小的可复制示例,并在保持相同问题的同时最小化文件。我可以通过比较文件看到这个问题,但我发现很难创建一个最小的示例。我可能能够很快地鼓起一些东西,因为我最初的理论是,与我的问题相关的与其说是内在的内容,不如说是外在的内容。将尽快用一些代码编辑问题。@kleopatra生成了一个example@James_D希望我的问题现在更有意义?由于两个FXML文件分别使用和加载,我应该知道最新加载的文件将创建一个控制器,而上一个文件将没有控制器,我推测这可能是因为每次使用FXML文件更改场景时,您都不希望对象和方法仍然占用空间或干扰。我专注于删除重复代码的实用性。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista2Controller" prefHeight="400.0" prefWidth="600.0">
    <children>
        <Pane fx:id="Vista2" layoutX="155.0" layoutY="-1.0" prefHeight="430.0" prefWidth="574.0" style="-fx-background-color: transparent">
            <Button fx:id="btn" layoutX="135.0" layoutY="57.0" mnemonicParsing="false" onAction="#previousPane" prefHeight="149.0" prefWidth="318.0" style="-fx-background-color: #133863;" text="Open Vista1" textFill="#ffffff">
                <font>
                    <Font name="Britannic Bold" size="20.0" />
                </font>
            </Button>
        </Pane>
    </children>
</AnchorPane>