JavaFx Exception in Application start method and Exception in Application start method

huangapple go评论119阅读模式
英文:

JavaFx Exception in Application start method and Exception in Application start method

问题

我已经创建了这个JavaFX类来洗牌和以不同角度显示扑克牌。但是突然间出现了一个错误。我尝试修复它,但仍然无法使其工作。代码解释在下面的代码中给出。
这个程序应该随机选择png扑克牌图像,并在第一行显示。第二行显示三张不同角度的小丑牌。第三行显示另外三张不同角度的牌。

代码:

  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.image.Image;
  7. import javafx.scene.image.ImageView;
  8. import javafx.scene.layout.HBox;
  9. import javafx.scene.layout.VBox;
  10. import javafx.stage.Stage;
  11. public class JavaFXCards extends Application{
  12. @Override
  13. public void start(Stage stage) throws Exception {
  14. VBox vBox=new VBox();
  15. vBox.setSpacing(50);
  16. vBox.setPadding(new Insets(20, 40, 40, 60));
  17. HBox firstRowHBox=new HBox();
  18. firstRowHBox.setSpacing(50);
  19. HBox secondRowHBox=new HBox();
  20. secondRowHBox.setSpacing(50);
  21. HBox thirdRowHBox=new HBox();
  22. thirdRowHBox.setSpacing(50);
  23. ArrayList<Integer> lstUnique52Cards=new ArrayList<Integer>();
  24. Random random=new Random();
  25. while(lstUnique52Cards.size()<3) {
  26. int randomNumber=random.nextInt(52)+1;
  27. if(!lstUnique52Cards.contains(randomNumber)) {
  28. lstUnique52Cards.add(randomNumber);
  29. ImageView imgViewCard=new ImageView(new Image("c:\\Users\\Desktop\\Tempory\\card"+randomNumber+".png"));
  30. firstRowHBox.getChildren().add(imgViewCard);
  31. }
  32. }
  33. for(int i=1;i<=3;i++) {
  34. int randomNumber=random.nextInt(2)+53;
  35. ImageView imgViewCard=new ImageView(new Image("c:\\Users\\Desktop\\Tempory\\card"+randomNumber+".png"));
  36. if(i==1)
  37. imgViewCard.setRotate(45);
  38. if(i==2)
  39. imgViewCard.setRotate(90);
  40. if(i==3)
  41. imgViewCard.setRotate(135);
  42. secondRowHBox.getChildren().add(imgViewCard);
  43. }
  44. int cardNumber=1;
  45. while(lstUnique52Cards.size()<6) {
  46. int randomNumber=random.nextInt(52)+1;
  47. if(!lstUnique52Cards.contains(randomNumber)) {
  48. lstUnique52Cards.add(randomNumber);
  49. ImageView imgViewCard=new ImageView(new Image("c:\\Users\\Desktop\\Tempory\\card"+randomNumber+".png"));
  50. if(cardNumber==1)
  51. imgViewCard.setRotate(135);
  52. if(cardNumber==2)
  53. imgViewCard.setRotate(90);
  54. if(cardNumber==3)
  55. imgViewCard.setRotate(45);
  56. thirdRowHBox.getChildren().add(imgViewCard);
  57. cardNumber++;
  58. }
  59. }
  60. vBox.getChildren().add(firstRowHBox);
  61. vBox.getChildren().add(secondRowHBox);
  62. vBox.getChildren().add(thirdRowHBox);
  63. Scene scene = new Scene(vBox ,450, 450);
  64. stage.setTitle("Cards");
  65. stage.setScene(scene);
  66. stage.show();
  67. }
  68. public static void main(String[] args) {
  69. launch(args);
  70. }
  71. }

错误:

  1. ant -f c:\Users\Desktop\Tempory\card jfxsa-run
  2. init:
  3. Deleting: c:\Users\Desktop\Tempory\card\build\built-jar.properties
  4. deps-jar:
  5. Updating property file: c:\Users\Desktop\Tempory\card\build\built-jar.properties
  6. compile:
  7. Detected JavaFX Ant API version 1.3
  8. jfx-deployment:
  9. jar:
  10. Copying 12 files to c:\Users\Desktop\Tempory\card\dist\run395293642
  11. jfx-project-run:
  12. Executing c:\Users\Desktop\Tempory\card\dist\run395293642\JavaFXCards.jar using platform C:\Program Files\Java\jdk1.8.0_221\jre/bin/java
  13. Exception in Application start method
  14. java.lang.reflect.InvocationTargetException
  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  18. at java.lang.reflect.Method.invoke(Method.java:498)
  19. at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
  20. at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
  21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  24. at java.lang.reflect.Method.invoke(Method.java:498)
  25. at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  26. Caused by: java.lang.RuntimeException: Exception in Application start method
  27. at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  28. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
  29. at java.lang.Thread.run(Thread.java:748)
  30. Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
  31. at javafx.scene.image.Image.validateUrl(Image.java:1121)
  32. at javafx.scene.image.Image.<init>(Image.java:620)
  33. at JavaFXCardGUI.start(JavaFXCardGUI.java:58)
  34. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
  35. at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
  36. at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
  37. at java.security.AccessController.doPrivileged(Native Method)
  38. at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
  39. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  40. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  41. at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
  42. ... 1 more
  43. Caused by: java.net.MalformedURLException: unknown protocol: c
  44. at java.net.URL.<init>(URL.java:607)
  45. at java.net.URL.<init>(URL.java:497)
  46. at java.net.URL.<init>(URL.java:446)
  47. at javafx.scene.image.Image.validateUrl(Image.java:1115)
  48. ... 11 more
  49. Exception running application JavaFXCardGUI
  50. Java Result: 1
  51. Deleting directory c:\Users\Desktop\Tempory\card\dist\run395293642
  52. jfxsa-run:
  53. BUILD SUCCESSFUL (total time: 2 seconds)
英文:

I have created this javaFx class to shuffle and show play cards in different angles. But Suddenly I got an error. I tried to fix it but I still couldn't able to make it work. Code explanation is given with the code below.
The program supposes to pick random png card images and display on the first row. The second row shows three joker cards in different angles. The third row shows another three different cards in 3 different angles

The code:

  1. import java.util.ArrayList;
  2. import java.util.Random;
  3. import javafx.application.Application;
  4. import javafx.geometry.Insets;
  5. import javafx.scene.Scene;
  6. import javafx.scene.image.Image;
  7. import javafx.scene.image.ImageView;
  8. import javafx.scene.layout.HBox;
  9. import javafx.scene.layout.VBox;
  10. import javafx.stage.Stage;
  11. //Creating class JavaFXCardGUI which is extending Application class
  12. //Now JavaFXCardGUI class will act as an JavaFX Application
  13. public class JavaFXCards extends Application{
  14. //Overriding start method of Application class which is always a starting point of any JavaFX Application
  15. @Override
  16. public void start(Stage stage) throws Exception {
  17. //Creating a VBox object
  18. VBox vBox=new VBox();
  19. //Setting its spacing and padding
  20. vBox.setSpacing(50);
  21. vBox.setPadding(new Insets(20, 40, 40, 60));
  22. //Creating a HBox object which will store the first row cards and setting its spacing property
  23. HBox firstRowHBox=new HBox();
  24. firstRowHBox.setSpacing(50);
  25. //Creating a HBox object which will store the second row cards and setting its spacing property
  26. HBox secondRowHBox=new HBox();
  27. secondRowHBox.setSpacing(50);
  28. //Creating a HBox object which will store the third row cards and setting its spacing property
  29. HBox thirdRowHBox=new HBox();
  30. thirdRowHBox.setSpacing(50);
  31. //Creating a arraylist of unique 52 cards which will store the each card number
  32. //If that card number will be already present in the arraylist then that card will not be added
  33. ArrayList&lt;Integer&gt; lstUnique52Cards=new ArrayList&lt;Integer&gt;();
  34. //Creating a random class object
  35. Random random=new Random();
  36. //This loop will add the three cards in first row
  37. while(lstUnique52Cards.size()&lt;3) {
  38. //Generate a randomNumber between 1 and 52 both inclusive
  39. //Since random.NextInt(52) will return number from 0 to 51 Hence adding 1 to
  40. //make it from 1 to 52
  41. int randomNumber=random.nextInt(52)+1;
  42. //If the current random generated is not present in the array list lstUnique52Cards
  43. //it means that the random number is unique then add that random number in array list lstUnique52Cards
  44. //then create the correpsonding ImageView with the card number represented by randomNumber
  45. //Then add the image to the firstRowHBox
  46. if(!lstUnique52Cards.contains(randomNumber)) {
  47. lstUnique52Cards.add(randomNumber);
  48. ImageView imgViewCard=new ImageView(new Image(&quot;c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;));
  49. firstRowHBox.getChildren().add(imgViewCard);
  50. }
  51. }
  52. //This loop will add the three joker cards in second row
  53. //Here the uniqueness doesn&#39;t matter for the card
  54. for(int i=1;i&lt;=3;i++) {
  55. //Below statement random.nextInt(2) will generate two random numbers 0 and 1 only as we are having
  56. //only two joker cards. Since two joker cards images number are 53.png and 54.png.
  57. //Hence adding 53 to get that correpsonding joker card number
  58. int randomNumber=random.nextInt(2)+53;
  59. //Creating ImageView object with the given card number image
  60. ImageView imgViewCard=new ImageView(new Image(&quot;c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;));
  61. //If the value of i is 1 i.e. it is first card then set the rotation angle as 45
  62. if(i==1)
  63. imgViewCard.setRotate(45);
  64. //If the value of i is 2 i.e. it is second card then set the rotation angle as 90
  65. if(i==2)
  66. imgViewCard.setRotate(90);
  67. //If the value of i is 3 i.e. it is third card then set the rotation angle as 135
  68. if(i==3)
  69. imgViewCard.setRotate(135);
  70. //Add the imgViewCard in secondRowHBox
  71. secondRowHBox.getChildren().add(imgViewCard);
  72. }
  73. //This loop will add the three more unique cards
  74. //Initializing cardNumber with 1
  75. int cardNumber=1;
  76. while(lstUnique52Cards.size()&lt;6) {
  77. //Generate a randomNumber between 1 and 52 both inclusive
  78. //Since random.NextInt(52) will return number from 0 to 51 Hence adding 1 to
  79. //make it from 1 to 52
  80. int randomNumber=random.nextInt(52)+1;
  81. //If the current random generated is not present in the array list lstUnique52Cards
  82. //it means that the random number is unique then add that random number in array list lstUnique52Cards
  83. //then create the correpsonding ImageView with the card number represented by randomNumber
  84. //Also set the correpsonding angle to the cardNumber of this thirdRowHBox
  85. //Then add the image view card to the thirdRowHBox
  86. if(!lstUnique52Cards.contains(randomNumber)) {
  87. lstUnique52Cards.add(randomNumber);
  88. ImageView imgViewCard=new ImageView(new Image(&quot;c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;));
  89. if(cardNumber==1)
  90. imgViewCard.setRotate(135);
  91. if(cardNumber==2)
  92. imgViewCard.setRotate(90);
  93. if(cardNumber==3)
  94. imgViewCard.setRotate(45);
  95. thirdRowHBox.getChildren().add(imgViewCard);
  96. cardNumber++; //Increment cardNumber by 1
  97. }
  98. }
  99. //Add all the three HBox objects in the vBox
  100. vBox.getChildren().add(firstRowHBox);
  101. vBox.getChildren().add(secondRowHBox);
  102. vBox.getChildren().add(thirdRowHBox);
  103. //Create a scene object with the child as vBox
  104. Scene scene = new Scene(vBox ,450, 450);
  105. //Setting the stage title as Cards
  106. stage.setTitle(&quot;Cards&quot;);
  107. //Setting the scene to the stage
  108. stage.setScene(scene);
  109. //Showing the stage
  110. stage.show();
  111. }
  112. //Main method it will invoke start method of Application class
  113. public static void main(String[] args) {
  114. launch(args);
  115. }
  116. }

The error:

  1. ant -f c:\Users\Desktop\Tempory\card jfxsa-run
  2. init:
  3. Deleting: c:\Users\Desktop\Tempory\card\build\built-jar.properties
  4. deps-jar:
  5. Updating property file: c:\Users\Desktop\Tempory\card\build\built-jar.properties
  6. compile:
  7. Detected JavaFX Ant API version 1.3
  8. jfx-deployment:
  9. jar:
  10. Copying 12 files to c:\Users\Desktop\Tempory\card\dist\run395293642
  11. jfx-project-run:
  12. Executing c:\Users\Desktop\Tempory\card\dist\run395293642\JavaFXCards.jar using platform C:\Program Files\Java\jdk1.8.0_221\jre/bin/java
  13. Exception in Application start method
  14. java.lang.reflect.InvocationTargetException
  15. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  17. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  18. at java.lang.reflect.Method.invoke(Method.java:498)
  19. at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
  20. at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
  21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  24. at java.lang.reflect.Method.invoke(Method.java:498)
  25. at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
  26. Caused by: java.lang.RuntimeException: Exception in Application start method
  27. at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  28. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
  29. at java.lang.Thread.run(Thread.java:748)
  30. Caused by: java.lang.IllegalArgumentException: Invalid URL: unknown protocol: c
  31. at javafx.scene.image.Image.validateUrl(Image.java:1121)
  32. at javafx.scene.image.Image.&lt;init&gt;(Image.java:620)
  33. at JavaFXCardGUI.start(JavaFXCardGUI.java:58)
  34. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
  35. at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
  36. at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
  37. at java.security.AccessController.doPrivileged(Native Method)
  38. at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
  39. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  40. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  41. at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
  42. ... 1 more
  43. Caused by: java.net.MalformedURLException: unknown protocol: c
  44. at java.net.URL.&lt;init&gt;(URL.java:607)
  45. at java.net.URL.&lt;init&gt;(URL.java:497)
  46. at java.net.URL.&lt;init&gt;(URL.java:446)
  47. at javafx.scene.image.Image.validateUrl(Image.java:1115)
  48. ... 11 more
  49. Exception running application JavaFXCardGUI
  50. Java Result: 1
  51. Deleting directory c:\Users\Desktop\Tempory\card\dist\run395293642
  52. jfxsa-run:
  53. BUILD SUCCESSFUL (total time: 2 seconds)

答案1

得分: 3

MalformedURLException - 你提供了错误的URL字符串;你只需要在图像路径之前加上"file:",像这样 - new Image(&quot;file:c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;)。请记得始终在本地文件路径前加上file:协议。

英文:

MalformedURLException - you provided a wrong URL string; all you need to do is prepend "file:" before the path to your image, like this - new Image(&quot;file:c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;).
Remember to always prefix local file paths with the file: protocol.

答案2

得分: 2

前面的图像文件路径中缺少文件协议:

  1. ImageView imgViewCard=new ImageView(new Image("file:\\\c:\\Users\\Desktop\\Tempory\\card"+randomNumber+".png"));
英文:

The file protocol in front of the image file paths is missing:

  1. ImageView imgViewCard=new ImageView(new Image(&quot;file:\\\c:\\Users\\Desktop\\Tempory\\card&quot;+randomNumber+&quot;.png&quot;));

huangapple
  • 本文由 发表于 2020年5月30日 21:17:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/62102999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定