有没有办法通过按钮更改 <fx: include source = "source.fxml"> 的源路径

Is there a way to change source path of <fx: include source = "source.fxml"> through a button

我正在尝试通过按钮更改 VBox 的 fxml 代码,但该代码给我一个 NullPointerException 错误:

这是我的sample.fxml

    <VBox fx:id="optionsVBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="515.0" prefWidth="515.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <fx:include fx:id="VBoxInclude" source="VBox1.fxml"/>
   </VBox>

有我的 VBox1(默认):

<VBox fx:id="options" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="370.0" prefWidth="270.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
                  <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
                     <children>
                        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Options" onAction="#goToOptions">
                           <font>
                              <Font name="Century Gothic" size="20.0" />
                           </font>
                        </Button>
                     </children>
                  </HBox>
                  <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
                     <children>
                        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Re-Play?">
                           <font>
                              <Font name="Century Gothic" size="20.0" />
                           </font>
                        </Button>
                     </children>
                  </HBox>
               </children>
</VBox>

并且当按下 VBox1 中的按钮时,示例的 fxml 路径应与此 VBOX2 交换

<VBox fx:id="change" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="370.0" prefWidth="270.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
         <children>
            <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Log out?">
               <font>
                  <Font name="Century Gothic" size="20.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" onAction="#goBack" prefHeight="70.0" prefWidth="100.0" text="Back..." HBox.hgrow="ALWAYS">
               <HBox.margin>
                  <Insets left="5.0" />
               </HBox.margin>
               <font>
                  <Font name="Century Gothic" size="20.0" />
               </font>
            </Button>
         </children>
      </HBox>
   </children>
</VBox>

还有我的控制器(当在 vbox 中按下按钮时,它应该将 VBox 更改为另一个 VBox):

@FXML
public void goToOptions() throws IOException{

    optionsVBox.getChildren().clear();
    optionsVBox.getChildren().add(FXMLLoader.load(getClass().getResource("VBox2.fxml")));

}
@FXML
public void goBack() throws IOException {

    optionsVBox.getChildren().clear();
    optionsVBox.getChildren().add(FXMLLoader.load(getClass().getResource("VBox1.fxml")));
}

P.s。所有的 fxml 文件都在同一个包中

这是堆栈跟踪(希望我做对了)

Caused by: java.lang.NullPointerException
    at sample.Controller.goToOptions(Controller.java:29)
    ... 58 more

这是它指向的内容

optionsVBox.getChildren().clear();
        optionsVBox.getChildren().add(FXMLLoader.load(getClass().getResource("/VBox2.fxml")));

其余没有我的代码的路径

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8413)
    at javafx.scene.control.Button.fire(Button.java:185)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
    at javafx.scene.Scene$MouseHandler.access00(Scene.java:3485)
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent3(GlassViewEventHandler.java:432)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null7(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
    ... 48 more

问题似乎是 fx:controller 在您的 VBox1 和 VBox2 fxml 文件定义中指定的。

让我提出以下解决方案:

sample.fxml

  • fx:include 两个选项并更改 VBox 定义
  • 为这两个VBox指定fx:id
  • 将第二个 visiblemanaged 属性设置为 false VBox

代码:

<?import javafx.scene.layout.VBox?>
<VBox fx:id="optionsVBox" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
  prefHeight="515.0" prefWidth="515.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
  fx:controller="sample.Controller">
<!-- controller set in top level VBox-->

    <!-- both vboxes added as children, removed fx:controller from their definition -->
    <fx:include source="optionsVBox.fxml" fx:id="options"/>

    <!-- visible and managed is set to FALSE in the beginning -->
    <!-- visible - whether user could see the control -->
    <!-- managed - whether control occupies space in the layout -->
    <fx:include source="changeVBox.fxml" fx:id="change" visible="false" managed="false"/>
</VBox>

optionsVbox.fxml

  • 指定 fx:id 为您希望在点击时发生某些事情的按钮
  • 从顶级 VBox
  • 中删除 fx:controller
  • 从按钮中删除 onAction - 这将在稍后连接到代码中

代码:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.Font?>
<VBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      prefHeight="400.0" prefWidth="600.0">
    <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
        <!-- add fx:id to this button! -->
        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Options" fx:id="btnOptions">
            <font>
                <Font name="Century Gothic" size="20.0"/>
            </font>
        </Button>
    </HBox>
    <HBox alignment="CENTER" VBox.vgrow="ALWAYS">
        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Re-Play?">
            <font>
                <Font name="Century Gothic" size="20.0"/>
            </font>
        </Button>
    </HBox>
</VBox>

changeVbox.fxml

与 optionsVbox.fxml

相同
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<VBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity"
      prefHeight="370.0" prefWidth="270.0">
    <HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="150.0" text="Log out?">
            <font>
                <Font name="Century Gothic" size="20.0"/>
            </font>
        </Button>
        <Button mnemonicParsing="false" prefHeight="70.0" prefWidth="100.0"
                text="Back..." HBox.hgrow="ALWAYS" fx:id="btnChange">
            <HBox.margin>
                <Insets left="5.0"/>
            </HBox.margin>
            <font>
                <Font name="Century Gothic" size="20.0"/>
            </font>
        </Button>
    </HBox>
</VBox>

Controller.java

  • 通过 fx:ids
  • 连接 VBoxes
  • 使用 lookup 函数按 fx:id 查找 Button 并设置适当的 onAction 回调
  • 在回调中切换 visiblemanaged 属性

代码:

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;

public class Controller {
    @FXML
    public VBox change, options;

    @FXML
    void initialize() {
        Button btnOptions = (Button) options.lookup("#btnOptions");
        btnOptions.setOnAction(this::goToOptions);
        Button btnChange = (Button) change.lookup("#btnChange");
        btnChange.setOnAction(this::goBack);
    }

    @FXML
    public void goBack(ActionEvent actionEvent) {
        change.setVisible(false);
        change.setManaged(false);
        options.setVisible(true);
        options.setManaged(true);
    }
    public void goToOptions(ActionEvent actionEvent) {
        change.setVisible(true);
        change.setManaged(true);
        options.setVisible(false);
        options.setManaged(false);
    }
}