Java 如何在程序不返回InvocationTargetException的情况下将节点添加到GridPane?
我正在写一个下棋的小GUI程序。我偶然发现了这样一个问题:如果程序不返回调用targetException,我就无法向棋盘添加任何元素。这是我的密码:Java 如何在程序不返回InvocationTargetException的情况下将节点添加到GridPane?,java,javafx,Java,Javafx,我正在写一个下棋的小GUI程序。我偶然发现了这样一个问题:如果程序不返回调用targetException,我就无法向棋盘添加任何元素。这是我的密码: public void start(Stage primaryStage) throws Exception { GridPane chessTable = new GridPane(); chessTable.getStylesheets().add(getClass().getResource("styles.css").to
public void start(Stage primaryStage) throws Exception {
GridPane chessTable = new GridPane();
chessTable.getStylesheets().add(getClass().getResource("styles.css").toString());
Box chessBox = new Box(112 , 94, 0);
chessBox.getStyleClass().add("chess-box");
for (int h = 0; h < 8; h++) {
for (int w = 0; w < 8; w++) {
GridPane.setConstraints(chessBox, w, h);
chessTable.getChildren().add(chessBox);
}
}
primaryStage.setTitle("ChessGame");
primaryStage.setFullScreen(true);
Scene scene = new Scene(chessTable, 900, 750);
primaryStage.setScene(scene);
primaryStage.show();
}
public void start(Stage primaryStage)引发异常{
GridPane chessTable=新的GridPane();
chessTable.getStylesheets().add(getClass().getResource(“style.css”).toString());
方块棋盘=新方块(112,94,0);
chessBox.getStyleClass().add(“chessBox”);
对于(int h=0;h<8;h++){
对于(int w=0;w<8;w++){
设置约束(棋盘格,w,h);
chessTable.getChildren().add(chessBox);
}
}
初级阶段。设置标题(“棋局”);
primaryStage.setFullScreen(真);
场景=新场景(棋盘,900750);
初级阶段。场景(场景);
primaryStage.show();
}
尝试以下方法(例如,我不知道您的“styles.css”位于何处……):
public void start(Stage primaryStage)引发异常{
GridPane chessTable=新的GridPane();
chessTable.getStylesheets().add(getClass().getResource(“/styles.css”).toString());
对于(int h=0;h<8;h++){
对于(int w=0;w<8;w++){
方块棋盘=新方块(112,94,0);
chessBox.getStyleClass().add(“chessBox”);
设置约束(棋盘格,w,h);
chessTable.getChildren().add(chessBox);
}
}
请注意,它会为代码的oposite中的每个单元格创建一个新框,尝试同时将同一个框放入不同的单元格中(只能添加一次)。尝试以下内容(例如,我不知道您的“style.css”位于何处……:
public void start(Stage primaryStage)引发异常{
GridPane chessTable=新的GridPane();
chessTable.getStylesheets().add(getClass().getResource(“/styles.css”).toString());
对于(int h=0;h<8;h++){
对于(int w=0;w<8;w++){
方块棋盘=新方块(112,94,0);
chessBox.getStyleClass().add(“chessBox”);
设置约束(棋盘格,w,h);
chessTable.getChildren().add(chessBox);
}
}
请注意,它会为代码中的每个单元格创建一个新框,尝试同时将同一个框放入不同的单元格中(只能添加一次).您的导入是什么?您可能会尝试添加一个swing box而不是JavaFX box。将来,请包含完整的。调用TargetException永远不应该是根本问题;请查看由以下原因引起的
,以了解真正的错误。您的导入是什么?您可能会尝试添加swing box而不是JavaFX框。将来,请包含完整的。调用TargetException
永远不应该是根本问题;请查看由以下原因引起的,以了解真正的错误。
public void start(Stage primaryStage) throws Exception {
GridPane chessTable = new GridPane();
chessTable.getStylesheets().add(getClass().getResource("/styles.css").toString());
for (int h = 0; h < 8; h++) {
for (int w = 0; w < 8; w++) {
Box chessBox = new Box(112, 94, 0);
chessBox.getStyleClass().add("chess-box");
GridPane.setConstraints(chessBox, w, h);
chessTable.getChildren().add(chessBox);
}
}