gpt4 book ai didi

java - getChildren().addAll() 上的异常

转载 作者:行者123 更新时间:2023-12-02 10:08:09 32 4
gpt4 key购买 nike

我正在尝试制作一个创建棋盘的 javafx 程序。但是,当我尝试运行我的程序时,它会在这一行中抛出异常:optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton);以下是异常(exception)情况:

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.IllegalArgumentException: Children: cycle detected: parent = HBox@ca2959, node = HBox@ca2959 at javafx.scene.Parent$2.onProposedChange(Parent.java:445) at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234) at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103) at DD_CheckerBoard.start(DD_CheckerBoard.java:40) 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 more

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class DD_CheckerBoard extends Application {

Scene scene1, scene2; //2 scenes created to pass from a scene to another scene when create a checkerboard is clicked

@Override
public void start(Stage primaryStage) throws Exception{

primaryStage.setTitle("My Checkerboard");

//Scene 1
HBox optionsPane = new HBox(10);
optionsPane.setAlignment(Pos.CENTER); //sets alignment
TextField n_input = new TextField(); //gets n from the user
TextField grid_display = new TextField(); //only displays n x n
grid_display.setEditable(false);
Label label = new Label("Enter a single number to customize grid size.");
Button createButton = new Button("Create New Checkerboard"); //button to create the new grid for checkerboard
createButton.setOnAction(e-> primaryStage.setScene(scene2)); //calls checkerboard to the scene
optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton); //add components
scene1 = new Scene(optionsPane, 300,250); //create scene 1 for optionsPane

//Scene 2
getCheckerBoard(n_input); //create the checkerboard using the input from the user

//SET SCENE
primaryStage.setScene(scene1); // Place in scene in the stage, first scene is for the option pane
primaryStage.show(); // Display the stage;
}

/**
* And this method creates the checkerboard
* @param input n by the user
*/
public void getCheckerBoard(TextField input) {
GridPane checkerboardPane = new GridPane(); //create a grid pane
int n = Integer.parseInt(input.getText()); //parse input n to int
int count = 0; //keep count of rectangles
double s = 70; // side of rectangle
for (int i = 0; i < n; i++) { //create the grid and rectangles
count++; //increment count
for (int j = 0; j < n; j++) {
Rectangle r = new Rectangle(s, s, s, s);
if (count % 2 == 0) r.setFill(Color.BLACK); //put rectangles to assigned colors in order
else r.setFill(Color.WHITE);
checkerboardPane.add(r, j, i); //add components to checkerboard
count++; //increment count
} }
scene2 = new Scene(checkerboardPane); //Create scene 2
}

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

最佳答案

您正在尝试将 optionsPane 作为子项添加到自身:

optionsPane.getChildren().addAll(optionsPane, n_input, grid_display, label, createButton);

这会导致您收到异常。要解决此问题,只需从子列表中删除 optionsPane 即可:

optionsPane.getChildren().addAll(n_input, grid_display, label, createButton);

但是您也会收到 NumberFormatException 因为您的 Textfield 默认为空:

java.lang.NumberFormatException: For input string: ""

所以你应该为你的TextField设置一个默认值:

TextField n_input = new TextField("0");

关于java - getChildren().addAll() 上的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55190060/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com