代码之家  ›  专栏  ›  技术社区  ›  Matthew Thibodeau

JavaFX图像错误[重复]

  •  0
  • Matthew Thibodeau  · 技术社区  · 7 年前

    我正在尝试制作一个简单的程序,随机选择一个介于1和52之间的数字,并根据该数字绘制一张卡。该卡将通过JavaFX显示。然而,每当我运行任何东西时,都会出现大量错误。

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

    代码如下:

    (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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   SteelToe    7 年前

    图像类构造函数需要一个表示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")));`