是否可以使用一个 javafx 控制器在不同的 fxml 之间移动?

问题描述

我的第一个 javafx 程序/项目有问题。 我需要制作主 fxml 场景,我可以在其中插入一些按钮和其他一些 fxml 页面到主场景中。我希望能够通过两个左/右按钮移动到左侧或右侧页面,并且可以编辑加载的 fxml 页面上的“标签”字段。

我正在尝试在主场景的 borderpane 区域中显示这些页面。我使用 javafx Controller 加载另一个页面并且它可以工作,但是当我尝试在加载的 fxml page1 上设置“标签”字段(名为“计数器”)时,我有一个错误,控制器中的这个标签字段为空。 (我为所有页面设置了一个控制器)

我的控制器:

public class Controller implements Initializable {

    @FXML
    private Button Start;

    @FXML
    private borderpane computer;

    @FXML
    private Label counter;

    int status = 0,next=0;

    @FXML
    void onpressed(ActionEvent event) {
        counter.setText(String.valueOf(next));
        next += 1;
    }

    @FXML
    void startComputer() throws IOException {
        loadPage("page1");
        setStatus(1);
    }

    @FXML
    void right_navigation(ActionEvent event) throws IOException {
        if(getStatus() == 1) {
            loadPage("page2");
            setStatus(2);
        } else if(getStatus() == 2){
            loadPage("page3");
            setStatus(3);
        } else if(getStatus() == 3){
            loadPage("page4");
            setStatus(4);
        } else if(getStatus() == 4){
            loadPage("page1");
            setStatus(1);
        }
    }

    @FXML
    void left_navigation(ActionEvent event) throws IOException {
        if(getStatus() == 4){
            loadPage("page3");
            setStatus(3);
        } else if(getStatus() == 3) {
            loadPage("page2");
            setStatus(2);
        } else if(getStatus() == 2){
            loadPage("page1");
            setStatus(1);
        } else if(getStatus() == 1){
            loadPage("page4");
            setStatus(4);
        }
    }

    private void loadPage(String page) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource(page + ".fxml"));
        computer.setCenter(root);
    }

    private void setStatus(int status){
        this.status = status;
    }

    private int getStatus(){
        return status;
    }

}

主要 fxml 场景:

<?import javafx.scene.image.Image?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <children>
        <AnchorPane prefheight="600.0" prefWidth="800.0">
            <children>
                <Button fx:id="start" layoutX="520.0" layoutY="540.0" mnemonicParsing="false" onAction="#startComputer" text="Start" />
                <Button fx:id="count" layoutX="620.0" layoutY="540.0" mnemonicParsing="false" onAction="#onpressed" text="Count" />
                <Button fx:id="left" layoutX="150.0" layoutY="540.0" mnemonicParsing="false" onAction="#left_navigation" text="&lt;" />
                <Button fx:id="right" layoutX="180.0" layoutY="540.0" mnemonicParsing="false" onAction="#right_navigation" text="&gt;" />
                <borderpane fx:id="computer" layoutX="300.0" layoutY="200.0" prefheight="200.0" prefWidth="180.0" styleClass="comp_display" stylesheets="@../myCss.css" />
            </children>
        </AnchorPane>
    </children>
</GridPane>

borderpane 中的 fxml 页面之一:

<borderpane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefheight="200.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <top>
   </top>
   <center>
      <AnchorPane prefheight="186.0" prefWidth="144.0" styleClass="page_screen" stylesheets="@../myCss.css" borderpane.alignment="CENTER">
         <children>
            <Label fx:id="counter" layoutX="51.0" layoutY="59.0" text="0" />
         </children>
      </AnchorPane>
   </center>
</borderpane>

我想在控制器的 fxml page1 上设置“counter”标签。 但是可以通过一个控制器设置这个 fxml 页面吗? 也许还有其他一些方法可以代替在主场景中加载 borderpane。 我将不胜感激。

解决方法

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

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

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