java - 如何解决JavaFX使用弹出通知的问题

标签 java javafx

我正在学习 JavaFX,我的 IDE 是 NetBeans IDE 8.2。 当我想运行一个项目时遇到问题。 我的示例项目是通过场景生成器将图片拖放到 ImageView 上。 错误是:

Executing C:\Users\Mohammad Sadeghi\Documents\NetBeansProjects\DragDrop\dist\run1057050476\DragDrop.jar using platform C:\Program Files\Java\jdk1.8.0_212\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$159(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassCastException: javafx.scene.image.ImageView cannot be cast to javafx.scene.Parent
at dragdrop.DragDrop.start(DragDrop.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
... 1 more
Exception running application dragdrop.DragDrop
Java Result: 1

我的 FXMLDocument.fxml 是:

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

<?import javafx.scene.image.ImageView?>


<ImageView fx:id="ImgID" fitHeight="228.0" fitWidth="248.0" onDragDone="#ondragdone" pickOnBounds="true" preserveRatio="true" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dragdrop.FXMLDocumentController"/>

FXMLDocumentController.java:

    /*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package drag-drop;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;

/**
 *
 * @author Mohammad Sadeghi
 */
public class FXMLDocumentController implements Initializable {

    private Label label;
    @FXML
    private ImageView ImgID;

    public FXMLDocumentController() {
    }

    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

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

    @FXML
    private void ondragdone(DragEvent event) {

        List<File> fi=event.getDragboard().getFiles();

         try {
            Image img=new Image(new FileInputStream(fi.get(0)));

            ImgID.setImage(img);
        } catch (Exception e) {
              System.out.println("ERROR");
        }

    }
}

我的主应用程序()是:

public class DragDrop extends Application {

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

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
  }


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

}

我不知道这个错误是什么原因! 当我使用 Scenebuilder 时,我在另一个 JavaFX 项目上遇到了几乎相同的错误。 有人可以帮助我吗?

最佳答案

如果你看一下 FXMLLoader#load(URL) 的签名你会看到:

public static <T> T load​(URL location) throws IOException

注意 <T> ?这使得该方法变得通用,并且类型参数用作该方法的返回类型。这允许您分配调用 #load(URL) 的结果。任何类型的变量,就像你正在做的那样。

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

您可以从字面上更改 root 的类型任何其他类型,代码仍然可以编译。然而,目前上述代码有 T被推断为Parent 。在幕后,这使用了强制转换,与 #load(URL) 时您必须执行的操作相同。方法返回 Object而不是T .

Parent root = (Parent) FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

问题是您的 FXML 将根元素声明为 ImageView 。这意味着 #load(URL) 返回的对象的实际类型方法是ImageView ,不是Parent ,以及ImageView class 不是 Parent 的子类型——因此ClassCastException .

一种选择是使用ImageView root = ...; 。但是,这还不够,因为您正在尝试使用 root作为 Scene 的根这必须是 Parent 。更好的解决方案是包装 ImageViewParent在 FXML 文件中。这是使用 StackPane 的示例:

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

<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.StackPane?>

<StackPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" 
           fx:controller="dragdrop.FXMLDocumentController">
  <ImageView fx:id="ImgID" fitHeight="228.0" fitWidth="248.0" onDragDone="#ondragdone" 
             pickOnBounds="true" preserveRatio="true"/>
</StackPane>

关于java - 如何解决JavaFX使用弹出通知的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644775/

相关文章:

java - eclipse maven 循环占位符引用错误使用配置文件

java - 将 JFileChooser 与 PDFBox 一起使用时,文件不会保存为 pdf,但保存时不带扩展名

java - 如何获得准备好嵌入的 Mac JRE(在 Linux 上)?

JavaFX + maven + TestFX + monocle 不能一起工作

java - 在 FXML Controller 类中找不到可注入(inject)字段

Java regex - 从匹配文本中获取行号

java - 如何在 hibernate 中获取具有不同 HQL 的 POJO 列表?

java - 使用 Stream api 聚合嵌套列表

java - 在 REST API 中使用受检异常与未受检异常

java - 我如何将两个子字符串绑定(bind)到一个?