【问题标题】:JavaFX Image Error [duplicate]JavaFX 图像错误 [重复]
【发布时间】:2018-03-23 03:31:49
【问题描述】:

我正在尝试制作一个简单的程序,它随机选择 1 到 52 之间的一个数字,并根据该数字抽一张牌。该卡片将由 JavaFX 显示。但是,每当我运行任何东西时,都会遇到一大堆错误。

到目前为止,我的主要想法是我的项目找不到图像。我创建了一个名为“resources”的文件夹,它们存储在那里,并直接在桌面上的文件夹中引用它们。还是没有运气。

代码如下:

(Imports removed for brevity)...

 public class Draw5 extends Application {
  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) throws IOException {
    // Create a list of card numbers
    ArrayList<Integer> cards = getCards();
    // Create a HBox pane
    HBox pane = new HBox(5);
    pane.setPadding(new Insets(5, 5, 5, 5));

    // Add nodes to pane
    for (int i = 0; i < 5; i++) {

        pane.getChildren().add(new ImageView(new Image("C:\\Users\\Matthew\\Desktop\\card\\" +
                cards.get(i) + ".png")));

    }

    // Create a scene and place it in the stage
    Scene scene = new Scene(pane);
    primaryStage.setTitle("Exercise_14_03"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
}

/** Returns a list with numbers 1-52 stored in random order */
private ArrayList<Integer> getCards() {
    ArrayList<Integer> cards = new ArrayList<>();
    for (int i = 0; i < 52; i++) {
        cards.add(i + 1);
    }
    java.util.Collections.shuffle(cards);
    return cards;
}


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

}

就像我说的,我觉得这个项目找不到卡片的 52 个 .png 图像,这很有趣。如果有人有任何建议,请告诉我。

为了记录,卡片是随机选择的,因此为什么 "cards.get(i) + ".png" "

这是我在启动时遇到的错误:

Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.base/java.lang.reflect.Method.invoke(Unknown Source)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)


Caused by: java.lang.RuntimeException: Exception in Application start method
 at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
 at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
 at java.base/java.lang.Thread.run(Unknown Source)



Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
at javafx.graphics/javafx.scene.image.Image.validateUrl(Unknown Source)
at javafx.graphics/javafx.scene.image.Image.<init>(Unknown Source)
at thibodeau14.Draw5.start(Draw5.java:31)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
... 1 more
Caused by: java.net.MalformedURLException: unknown protocol: c
at java.base/java.net.URL.<init>(Unknown Source)
at java.base/java.net.URL.<init>(Unknown Source)
at java.base/java.net.URL.<init>(Unknown Source)
... 12 more

【问题讨论】:

    标签: java image javafx imageview resources


    【解决方案1】:

    Image 类构造函数需要一个表示 URL 的字符串,当用一个用于文件的字符串表示 URL 时,将“file///”添加到 URL。

    所以这个:

    pane.getChildren().add(new ImageView(new Image("C:\\Users\\Matthew\\Desktop\\card\\" +
                   cards.get(i) + ".png")));
    

    变成:

    pane.getChildren().add(new ImageView(new 
    Image("file:\\\C:\\Users\\Matthew\\Desktop\\card\\" +
                    cards.get(i) + ".png")));`
    

    【讨论】:

    • 您的 ecode sn-p 中的字符串未正确转义前 3 个反斜杠。此外,我很惊讶 JavaFX 可以使用反斜杠而不是斜杠来处理 URL。此外,以这种方式将任意文件路径转换为 ​​URL 是不安全的:例如文件名的某些部分包含一个空格,这可能会失败。更好地使用new File(filePath).toURI().toURL().toExternalForm()...
    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 2016-04-17
    相关资源
    最近更新 更多