JavaFX,鼠标输入时

标签 java javafx netbeans scenebuilder

我在 NetBeans 的场景生成器中通过“鼠标输入”调用我的函数时遇到问题。我正在尝试使用“鼠标输入”事件编写简单的代码,但有一些异常(exception),我真的不知道如何解决这个问题。 我将此功能添加到按钮属性中的“鼠标输入时”,但它不起作用。

这是我的 Controller 的类:

package firstapp;
import java.awt.event.MouseEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;

public class firstAppController implements Initializable {

@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");
}

@FXML
private void fun1(ActionEvent e)
{
    System.out.println("It works");
}

@FXML //this function doesn't work with On Mouse Entered
public void fun2(MouseEvent e)
{
    System.out.println("It works too");
}

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

和 FXML:

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

 <AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" 
   xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" 
   fx:controller="firstapp.firstAppController">
   <children>
       <Button fx:id="button1" layoutX="126" layoutY="90" onAction="#fun1" 
               onMouseEntered="#fun2" text="Click Me!" />
       <Label fx:id="label1" layoutX="69.0" layoutY="129.0" text="Label" />
  </children>
 </AnchorPane>

并输出:

    Executing 
    C:\Users\Maczak\Desktop\NetBeans\firstApp\dist\run1545139292\firstApp.jar 
    using platform C:\Program Files\Java\jdk1.8.0_152\jre/bin/java
    Exception in Application start method
    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 

  com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: Error resolving onMouseEntered='#fun2', either the event handler is not in the Namespace or there is an error in the script.
file:/C:/Users/Maczak/Desktop/NetBeans/firstApp/dist/run1545139292/firstApp.jar!/firstapp/firstApp.fxml:11

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at firstapp.FirstApp.start(FirstApp.java:22)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application firstapp.FirstApp
Java Result: 1

fun2 有什么问题吗?为什么我无法使用 fun2 创建“鼠标输入”事件?

最佳答案

您使用了错误的导入。

替换

import java.awt.event.MouseEvent;

import javafx.scene.input.MouseEvent;

关于JavaFX,鼠标输入时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48020170/

相关文章:

mysql - 开发 C 和 MYSQL 应用程序时出现构建错误

netbeans - 适用于 Linux 的诺基亚 s60 模拟器

java - selenium 声明的包与预期的包不匹配

javafx - 如何为 JavaFX 编写 KeyListener

c# - 从 Visual Studio 迁移到 NetBeans 或其他一些 IDE

JavaFX:类似 mozilla 的选项卡

java - 在 JavaFx 中重新启动应用程序

java - 是否可以在eclipse中有一个java程序来编译java以外的其他语言?

java - Android:在大量小部件上动态添加 onClickListener

java - 通过 T3 协议(protocol)从 shell 调用 java 方法