我目前的代码遇到了一些问题。我有一个按钮,它应该遍历我创建的ImageView数组列表,并在每次按下时显示4张卡。现在我可以得到我的" Play"按钮工作,它的主要功能是只显示卡1次然后被禁用。 "交易"按钮应该在每次按下时显示4张随机卡,直到所有卡都消失。现在我可以让它改变1张卡1次,然后我得到一个"复制儿童"错误。我知道这个问题与每次循环遍历相同的边框窗格有关,但我希望能找到一些方法来解决这个问题。我知道这可能涉及改变我的代码,这很好。谢谢!
import java.io.File;
import java.util.ArrayList;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.Label;
import javafx.geometry.*;
import java.util.*;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javax.swing.*;
public class War extends Application {
public static void main(String[] args) {
launch(args);
}
List<Integer> deck;
ArrayList<ImageView> imager = new ArrayList<ImageView>();
private int roundCounter;
JButton button = new JButton();
@Override
public void start(Stage primaryStage) {
loadDeck();
loadCards();
//Panes
BorderPane bPain = new BorderPane();
VBox vBox = new VBox(100);
vBox.setPadding(new Insets(5, 5, 5, 5));
HBox buttonBox = new HBox(100);
buttonBox.setPadding(new Insets(25, 25, 25, 75));
buttonBox.setStyle("-fx-background-color: blue");
BorderPane bPain4 = new BorderPane();
bPain.setLeft(new P1Pane("PLayer 1"));
bPain4.setRight(new P2Pane("PLayer 2"));
BorderPane bPain2 = new BorderPane();
bPain2.setRight(new P3Pane("PLayer 4"));
bPain2.setLeft(new P4Pane("PLayer 3"));
BorderPane bPain3 = new BorderPane();
DeckPane dPane = new DeckPane("Deck");
bPain3.setCenter(dPane);
bPain3.setMargin(dPane, new Insets(5, 5, 5, 5));
bPain3.setCenter(dPane);
//Buttons
Button pBtn = new Button("PLAY");
Button dBtn = new Button("DEAL");
Button endBtn = new Button("END GAME");
buttonBox.getChildren().addAll(pBtn, dBtn , endBtn);
vBox.getChildren().addAll(buttonBox, bPain, bPain3, bPain2, bPain4);
pBtn.setOnAction(
p -> {System.out.println("Play pressed");
loadCards();
});
pBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent p) {
bPain.getChildren().add(imager.get(10));
bPain4.getChildren().add(imager.get(20));
bPain2.getChildren().add(imager.get(30));
bPain3.getChildren().add(imager.get(40));
}
});
dBtn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent d) {
for(int i = 0; i<deck.size(); i++){
//bPain2.getChildren().add(imager.get(i));
//bPain4.getChildren().add(imager.get(i));
bPain.getChildren().add(imager.get(i));
//bPain3.getChildren().add(imager.get(i));
}
}
});
endBtn.setOnAction(
e -> {
//when pressed, popup asks if sure
//disabled after
//resets number of wins
System.out.println("End pressed");
});
//Stage set
primaryStage.setTitle("WAR!!!");
//primaryStage.setScene(new Scene(vBox2, 100, 100));
primaryStage.setScene(new Scene(vBox, 500, 500));
primaryStage.show();
}
public void loadCards() {
String path = "/users/ebrovski/Desktop/card";
String files;
File folder = new File(path);
Image[] images = new Image[100];
File[] listOfFiles = folder.listFiles();
int j = 0;
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].getName().endsWith("png")) {
System.out.println(listOfFiles[i].getAbsolutePath());
files = "file:" + listOfFiles[i].getAbsolutePath();
System.out.println(files);
images[j] = new Image(files, 200, 200, true, true);
j++;
}
}
for(int k = 0; k < deck.size(); k++) {
imager.add(new ImageView(images[deck.get(k)]));
}
}
public void loadDeck() {
deck = new ArrayList<Integer>();
for(int i =0; i < 52; i++) {
deck.add(i);
}
Collections.shuffle(deck);
}
//Player Panes
class P1Pane extends Pane {
private void add(ImageView get) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public P1Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 1, 1, 1));
// getChildren().add(imager.get(10));
}
}
class P2Pane extends Pane {
public P2Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 1, 1, 1));
//getChildren().add(imager.get(20));
}
}
class P3Pane extends Pane {
public P3Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 3, 3, 3));
//getChildren().add(imager.get(40));
}
}
class P4Pane extends Pane {
public P4Pane(String player) {
Label playerTitle = new Label(player);
getChildren().add(playerTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: green");
setPadding(new Insets(75, 3, 3, 3));
//getChildren().add(imager.get(30));
}
}
//Deck Pane
class DeckPane extends Pane {
public DeckPane(String deckName) {
Label deckTitle = new Label(deckName);
getChildren().add(deckTitle);
setStyle("-fx-border-color: black");
//setStyle("-fx-background-color: forestgreen");
setPadding(new Insets(75, 3, 3, 3));
}
}
}
现在我在我的工作计算机上,我得到一个与loadDeck()有关的不同错误;方法。这是运行时错误: 请帮忙!
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$49/28230490.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 100
at war.War.loadCards(War.java:151)
at war.War.start(War.java:50)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$64/26176767.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/11567059.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/14442382.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/30452001.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/2075313.run(Unknown Source)
... 1 more
答案 0 :(得分:0)
用e。 G。 bPain.getChildren()添加(imager.get(I))。你将一个ImageView添加到bPain。下次调用操作时,您再次添加相同的ImageView,尽管它已经在场景中。
您必须只向场景添加一次。
错误
引起:java.lang.ArrayIndexOutOfBoundsException:100
你刚发布的是因为你限制了Image [] images = new Image [100];
在达到大小限制后停止向数组添加项目或使用ArrayList。