通过一个按钮,javafx,controller,fxml,fxmlloader"/>

首页 > 解决方案 > 有没有办法改变源路径通过一个按钮

问题描述

我正在尝试通过按钮更改 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")));
}

ps所有的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$1.handle(BehaviorSkinBase.java:96)
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.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.access$1500(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$handleMouseEvent$353(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$null$147(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

标签: javafxcontrollerfxmlfxmlloader

解决方案


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

让我提出以下解决方案:

示例.fxml

  • fx:include选项和更改VBox定义
  • fx:id为这两个VBoxes指定
  • 第二次将属性设置visiblemanagedfalseVBox

代码:

<?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>

选项Vbox.fxml

  • 指定fx:id您希望在单击时发生某些事情的按钮
  • fx:controller从顶层 VBox中删除
  • 从按钮中删除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>

改变Vbox.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>

控制器.java

  • VBox由他们fx:id的 s连接
  • 使用函数Button通过他们的 s找到s并设置适当的回调fx:idlookuponAction
  • 回调中的切换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);
    }
}

推荐阅读