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

问题描述

我只是通过清理代码才意识到我认为我可以清理并且已经从我的一些控制器类中清除了我的问题。为了更好地表达我的问题并使优先级的概念更加清晰,我将给出一个简短的解释,然后是一些代码。想象一下,屏幕一侧的界面与具有菜单按钮的界面一致且永久,您基本上可以在选项卡之间移动,从而更改舞台的其余部分,将 fxml 文件传递​​到关联的窗格。

阅读示例和问题我认为这是可行的,并使用此链接中的框架 https://gist.github.com/jewelsea/6460130 看到标题保持一致,同时在 vista1.fxml 和 vista2.fxml 之间切换场景,我的理解是,如果标题保持不变从 main.fxml 作为场景的一部分,舞台因此同时使用 main.fxml 和 vista1.fxml 或 main.fxml 和 vista2.fxml,因此两个关联的控制器都存在并且可以根据窗格访问相应的方法互动。

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

以一些代码为例: 主程序

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);
    }
}

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>

otherMenu.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<StackPane fx:id="otherMenuOption" xmlns:fx="http://javafx.com/fxml" fx:controller="sample.Vista1Controller">
    <children>
        <Label fx:id="label" layoutX="135.0" layoutY="57.0" maxWidth="120.0" text="other program stuff" VBox.vgrow="NEVER" />
    </children>
</StackPane>

Vista1Controller.java

package sample;

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

public class Vista1Controller {

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

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>

Vista2Controller.java

package sample;

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

public class Vista2Controller {

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

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>

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...