因此,基本上挑战在标题中。这是非常基本的,但我认为我有大部分。我遇到的一个问题是,卡片的图片来自。我知道通常您会将它们来自计算机,但是我正在做照片来自Web URL的地方。这就是我到目前为止的目标,但是我会遇到许多错误(InvocationTargetException,Runtime和IndexoutofBounds)。
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)
人们使用样式指南作为凹痕等的原因是,它减少了错误的可能性。如果您采用代码:
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);
}
}
学习如何读取堆栈跟踪也很有用。