如何使用带有ActionEvent作为参数的控制器类来切换场景?

时间:2019-04-25 19:26:11

标签: java javafx scenebuilder

我想制作一个可以在场景之间切换的基本GUI。我知道这个问题以前曾有人问过,请相信我,我已经尝试寻找解决方案已有几个小时了,但无济于事,所以我求助于此。

以下是相关代码:

public class MainMenuController implements Initializable {

public void startNewGame(ActionEvent event) throws IOException {
    Parent tableViewParent = FXMLLoader.load(getClass().getResource("Game.fxml"));
    Scene tableViewScene = new Scene(tableViewParent);

    Stage window = (Stage)((Node)event.getSource()).getScene().getWindow();

    window.setScene(tableViewScene);
    window.show();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

}

这是主要应用程序:

public class MainApplication extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("MainMenu.fxml"));
    primaryStage.setTitle("Main Menu");
    primaryStage.setScene(new Scene(root));
    primaryStage.show();
}

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

运行此命令会出现此错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:835)
Caused by: javafx.fxml.LoadException: Error resolving onAction='#startNewGame', either the event handler is not in the Namespace or there is an error in the script.
...

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
    at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
    at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at GUI.MainApplication.start(MainApplication.java:13)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application GUI.MainApplication

所以我知道这是由处理按钮动作的方法引起的。只要没有参数ActionEvent,我就可以成功地使其他按钮具有动作。

似乎每当我添加此参数来访问该方法的操作事件时,都无法识别该方法。我认为我在设置主应用程序或控制器甚至fxml时根本犯了错。我看了这么多教程,但并不太了解,因为我有类似的东西。

这是fxml文件。起始场景(MainMenu.fxml):

    <?xml version="1.0" encoding="UTF-8"?>

    <?import javafx.scene.control.Button?>
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.AnchorPane?>
    <?import javafx.scene.text.Font?>

    <AnchorPane fx:id="ap" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="658.0" prefWidth="885.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.MainMenuController">
       <children>
          <Label fx:id="title" layoutX="322.0" layoutY="30.0" prefHeight="48.0" prefWidth="241.0" text="Text Adventure">
             <font>
                <Font size="35.0" />
             </font>
          </Label>
          <Button layoutX="384.0" layoutY="204.0" mnemonicParsing="false" onAction="#startNewGame" prefHeight="51.0" prefWidth="117.0" text="Start New Game" />
       </children>
    </AnchorPane>

将切换到(Game.fxml)的场景:

 <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="658.0" prefWidth="885.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI.GameController">
   <children>
      <RadioButton fx:id="radio1" layoutX="201.0" layoutY="337.0" mnemonicParsing="false" onAction="#radio1Click" text="RadioButton">
         <toggleGroup>
            <ToggleGroup fx:id="group" />
         </toggleGroup>
      </RadioButton>
      <RadioButton fx:id="radio2" layoutX="201.0" layoutY="402.0" mnemonicParsing="false" onAction="#radio2Click" text="RadioButton" toggleGroup="$group" />
      <RadioButton fx:id="radio3" layoutX="201.0" layoutY="469.0" mnemonicParsing="false" onAction="#radio3Click" text="RadioButton" toggleGroup="$group" />
      <Label fx:id="output" layoutX="169.0" layoutY="25.0" prefHeight="231.0" prefWidth="462.0" text="Label">
         <font>
            <Font size="17.0" />
         </font>
      </Label>
   </children>
</AnchorPane>

2 个答案:

答案 0 :(得分:0)

经过数小时的头痛和浪费时间后,答案是Netbeans导入了错误版本的ActionEvent(java.awt版本而不是javafx版本)。如果将来有人遇到这个问题,请确保以这种方式导入:

_((responseData && responseData.data) || {})
     .pick(['title', 'layout', 'slug', 'author', 'seo', 'css', 'js'])
     .mapKeys((value, key) => _.camelCase(key))
     .value()

答案 1 :(得分:-1)

从您提供的代码以及我刚刚尝试过的内容来看,一切似乎都运行良好,可能问题出在您的项目结构组织中。请尝试再次保存所有文件,因为您的IDE可能不会自动保存更改并尝试重新运行项目。否则,请查看文件是否在指定的软件包中。希望有帮助。