2017-02-23 108 views
0

所以基本上,挑戰是在標題。這是非常基本的,但我認爲我是最重要的。我遇到的一個問題是卡片的圖片來自哪裏。我知道通常你有他們來自你的電腦,但我正在做的地方圖片來自網址。這是我迄今爲止,但我收到了一系列錯誤(InvocationTargetException,運行時和IndexOutOfBounds)任何幫助表示讚賞。顯示3隨機卡片JavaFX

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 
public class Exercise14_03 extends Application{ 
    @Override 
    public void start(Stage primaryStage){ 
     ArrayList<String> cards = new ArrayList<>(); 

     for(int i = 0; i < 52; i++){ 
      cards.add(String.valueOf(i + 1)); 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     } 
    } 

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

(例外)

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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1 
    at java.util.ArrayList.rangeCheck(ArrayList.java:653) 
    at java.util.ArrayList.get(ArrayList.java:429) 
    at Exercise14_03.start(Exercise14_03.java:29) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191) 
+1

你能發佈精確的異常消息嗎? –

+0

爲什麼不修復縮進等,然後錯誤將是顯而易見的。 –

回答

1

人使用風格指南縮進等原因,在於它降低了出錯的可能性。如果你把你的代碼:

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 
public class Exercise14_03 extends Application{ 
    @Override 
    public void start(Stage primaryStage){ 
     ArrayList<String> cards = new ArrayList<>(); 

     for(int i = 0; i < 52; i++){ 
      cards.add(String.valueOf(i + 1)); 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
     } 
    } 

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

正確縮進它(大多數IDE會爲你做這個,例如在Eclipse中,您可以選擇用CTRL-A的一切,然後用CTRL-SHIFT-F格式),你得到

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 

public class Exercise14_03 extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     ArrayList<String> cards = new ArrayList<>(); 

     for (int i = 0; i < 52; i++) { 
      cards.add(String.valueOf(i + 1)); 

      java.util.Collections.shuffle(cards); 

      ImageView view1 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
      ImageView view2 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
      ImageView view3 = new ImageView(
        new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

      HBox root = new HBox(); 

      root.getChildren().add(view1); 
      root.getChildren().add(view2); 
      root.getChildren().add(view3); 

      Scene scene = new Scene(root); 

      primaryStage.setTitle("Exercise14_03"); 
      primaryStage.setScene(scene); 
      primaryStage.show(); 
     } 
    } 

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

現在的問題是顯而易見的:幾乎整個start(...)方法是你for循環內。在循環的第一次迭代中,添加第一個元素,然後嘗試訪問元素0,1和2,從而導致IndexOutOfBoundsException

只需推動碼出的for循環:

import java.util.ArrayList; 
import javafx.application.Application; 
import javafx.stage.Stage; 
import javafx.scene.Scene; 
import javafx.scene.image.ImageView; 
import javafx.scene.image.Image; 
import javafx.scene.layout.HBox; 

public class Exercise14_03 extends Application { 
    @Override 
    public void start(Stage primaryStage) { 
     ArrayList<String> cards = new ArrayList<>(); 

     for (int i = 0; i < 52; i++) { 
      cards.add(String.valueOf(i + 1)); 
     } 

     java.util.Collections.shuffle(cards); 

     ImageView view1 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(0) + ".png")); 
     ImageView view2 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(1) + ".png")); 
     ImageView view3 = new ImageView(
       new Image("https://liveexample.pearsoncmg.com/book/image/card/" + cards.get(2) + ".png")); 

     HBox root = new HBox(); 

     root.getChildren().add(view1); 
     root.getChildren().add(view2); 
     root.getChildren().add(view3); 

     Scene scene = new Scene(root); 

     primaryStage.setTitle("Exercise14_03"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

它也是有用的學習how to read the stack trace

+0

Idk爲什麼我沒有看到哈哈,是的,我認爲我仍然習慣於複製和粘貼的stackoverflow,因爲它看起來像你在我的末尾發佈相同。感謝您的幫助! – Jmpollock56

+0

@ Jmpollock56您沒有看到它,因爲它沒有正確縮進,並且因爲您沒有仔細閱讀錯誤消息。 –