【发布时间】: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