JavaFX 启动屏幕按钮 onClick 后出现错误

标签 java javafx fxml

我正在使用 JavaFX 制作一个桌面应用程序,我制作了一个仅显示 5 秒的启动屏幕,5 秒后它显示一个带有按钮的新阶段,当我单击该按钮时,它会发送一个错误:

    Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1765)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
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:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3471)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3399)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3767)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3486)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2495)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:350)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$350(GlassViewEventHandler.java:385)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$$Lambda$158/361466793.get(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:404)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:384)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:927)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)

这里是 JavaFX 类

RootForm.java(启动画面)

    package Forms;

    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.animation.PauseTransition;
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.scene.image.Image;
    import javafx.scene.paint.Color;
    import javafx.stage.Stage;
    import javafx.stage.StageStyle;
    import javafx.util.Duration;


    public class RootForm extends Application {


@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("RootForm.fxml"));

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.show();
    scene.setFill(Color.TRANSPARENT);
    stage.getIcons().add(new Image("/Images/8-512.png"));

    PauseTransition delay = new PauseTransition(Duration.seconds(5));
    delay.setOnFinished(event -> {
        try {
            Stage mainStage = new Stage();
            Parent parent = FXMLLoader.load(getClass().getResource("Error.fxml"));
            mainStage.setScene(new Scene(parent));
            mainStage.show();
            mainStage.getIcons().add(new Image("/Images/8-512.png"));
            stage.hide();
        } catch (IOException ex) {
            Logger.getLogger(RootForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
    delay.play();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

RootForm.fxml(启动画面 FXML)

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

    <?import java.lang.*?>
    <?import java.net.*?>
    <?import java.util.*?>
    <?import javafx.scene.co

ntrol.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="Splash" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2">
  <children>
    <Label layoutX="57.0" layoutY="247.0" prefHeight="21.0" prefWidth="202.0" text="Software is loading...">
      <font>
        <Font size="15.0" />
      </font>
    </Label>
  </children>
  <stylesheets>
    <URL value="@style.css" />
  </stylesheets>
</AnchorPane>

Error.fxml(带有按钮的第二阶段)

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

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane"  prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="Forms.ErrorController">
  <children>
    <Button fx:id="btnMain" onMouseClicked="#btnMainActEvent" layoutX="107.0" layoutY="65.0" mnemonicParsing="false" prefHeight="112.0" prefWidth="101.0" text="Button" textFill="RED" />
  </children>
  <stylesheets>
    <URL value="@style.css" />
  </stylesheets>
</AnchorPane>

ErrorController.java(应该出现的第二阶段)

package Forms;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

/**
 * FXML Controller class
 *
 * @author User
 */
public class ErrorController implements Initializable {

    @FXML
    private Button btnMain;

    public void btnMainActEvent(ActionEvent event) throws Exception{

        System.out.println("U Shtyp!");

    }
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

}

以下是屏幕截图: Splash Screen

Second Screen with The Button

单击第二个屏幕上的按钮后,我收到该错误。 谁能帮我解决这个问题吗?

最佳答案

问题出在您在 fxml 中使用的 onMouseClicked 上。它发送 MouseEvent 而不是 ActionEvent

您需要在 fxml 中使用 onAction 来设置要在按钮的 fire(点击)事件 上执行的操作。

<Button fx:id="btnMain" onAction="#btnMainActEvent" layoutX="107.0" layoutY="65.0" mnemonicParsing="false" prefHeight="112.0" prefWidth="101.0" text="Button" textFill="RED" />

关于JavaFX 启动屏幕按钮 onClick 后出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29947754/

相关文章:

java - 我可以在 FXML 中设置 BorderPane 元素的宽度吗?

java - 在 ContextLoaderListener 中获取服务器名称

Java AWT 线程

javascript - 浏览器端代码编辑器组件

JavaFX:tableView.getSelectionModel().select(0)不起作用的任何可能原因?

Javafx 在 ScrollPane 中禁用鼠标滚轮滚动

java - 无法在 JavaFX 中加载 FXML 文件

java - SonarLint 和 IBM JVM 的 HTTPS 问题

java - 在 Android 上的两个 Activity 之间切换时出现 NullPointerException

JavaFX8 : Single Controller for 1+1popup scene