当前位置: 首页 > 知识库问答 >
问题:

JavaFX启动屏幕后按钮onClick错误

戚锦
2023-03-14

我正在用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
    }    

}

在我点击第二个屏幕上的按钮后,我得到了这个错误。有人能帮我处理这件事吗?

共有1个答案

黄伟
2023-03-14

问题在于您在fxml中使用的onMouseClicked。它发送一个MouseEvent,而不是ActionEvent

您需要在fxml中使用onAction,以设置对按钮的fire(click)事件执行的操作。

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

 类似资料:
  • 问题内容: 在我的应用程序中,我有两个活动。首先是启动屏幕,它仅显示应用程序名称和其他一些信息。单击初始屏幕活动后,我正在加载主要活动。我的应用程序运行正常,但遇到一个小问题。如果我从主要活动中按返回按钮,则控件将启动初始屏幕活动。但我不想再次显示启动画面活动,我想避免在按“后退”按钮时启动画面。 可能吗?如果可以,怎么办? 问题答案: 在文件中,在初始屏幕中添加属性。

  • 我正在尝试完成此程序:1:请求用户在JOptionPane窗口中输入手机号码2:显示消息“单击确定以跟踪(输入)的GPS坐标3:用户单击确定后,应弹出启动屏幕。4:启动屏幕应完全完成,然后JOptionPane窗口应显示消息“位于GPS坐标内的地址是:“加上我输入的任何假地址。 现在,启动屏幕在其他所有操作中运行,并且所有操作都出现故障。我希望启动屏幕在单击“OK”后执行,然后完成并继续执行最后的

  • 我是swift和Xcode新手,我想知道是否有人愿意向我解释,我应该如何让我的启动屏幕显示3秒钟,然后在这段时间内稍微淡出? 我想做一个我以前构建的网页的web视图,但希望它在启动屏幕之前淡出。 我已经阅读了所有其他与该主题相关的问题,但我不理解它们。我也一直在关注一些关于这个主题的教程,但什么都没有。 有人吗,拜托?

  • 启动屏幕是一个用户对你应用的第一体验。 启动页面类型 占位 UI 品牌启动页面 启动页面类型 启动页面是用户对你应用的第一体验。 启动应用时,如果显示一个空白面板,会增加用户观察到的加载时间,考虑使用占位符 UI 或者一个品牌加载页面。 占位 UI 是最无缝的加载转换——适用于应用加载和应用内活动切换两者。 品牌启动页面提供了短暂的品牌曝光,让 UI 聚焦于内容上面。 品牌启动页面 占位 UI 占

  • 我使用一个空活动为我的应用程序创建了一个启动屏幕,该活动在背景图像中保持可见3秒钟。通常,应用程序在背景图像变为可见之前以白色屏幕启动,然而,有些应用程序已经以“真实”的初始屏幕图像启动。如何实现这一点?

  • 当我运行我的JavaFX程序时,我的第一个按钮总是被选中(见周围的蓝线):图片形式开始 当我使用箭头键将播放器选项设置为左或右时,左或右按钮将被选中: 在我按下右箭头 没有选中任何按钮我如何运行程序