如何使用控制器类切换场景,将ActionEvent作为参数?

问题描述 投票:0回答:2

我想制作一个可以在场景之间切换的基本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>
java javafx scenebuilder
2个回答
0
投票

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

import javafx.event.ActionEvent;

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.