如何修复线程“JavaFX应用程序线程”中的异常java.lang.IllegalArgumentException:Children:重复的子项添加:parent = Grid

时间:2018-03-01 23:53:06

标签: java button javafx gridpane

单击按钮时,我试图更改GridPane中按钮的文本。我成功地成功了。但是,我得到了这个例外。

我有这个改变文字的功能。

public void clicker(Button b) {
    if (b.getText().equals(" ")) {
        if (turn == 0) {
            b.setText("X");
            b.setFont(Font.font("Times", FontWeight.EXTRA_BOLD,
                    60));
            turn = 1;
        } else if (turn == 1) {
            Button bNew = new Button("Y");
            bNew.setFont(Font.font("Times", FontWeight.EXTRA_BOLD, 60));
            b.setText("Y");
            b.setFont(Font.font("Times", FontWeight.EXTRA_BOLD,
                    60));
            turn = 0;
        }
    }
}

这是发生错误的窗格设置和事件处理程序。

GridPane pane = new GridPane();


    b1.setMinSize(100, 100);
    b2.setMinSize(100, 100);
    b3.setMinSize(100, 100);
    b4.setMinSize(100, 100);
    b5.setMinSize(100, 100);
    b6.setMinSize(100, 100);
    b7.setMinSize(100, 100);
    b8.setMinSize(100, 100);
    b9.setMinSize(100, 100);




    GridPane.setConstraints(b1, 0, 0);
    GridPane.setConstraints(b2, 1, 0);
    GridPane.setConstraints(b3, 2, 0);
    GridPane.setConstraints(b4, 0, 1);
    GridPane.setConstraints(b5, 1, 1);
    GridPane.setConstraints(b6, 2, 1);
    GridPane.setConstraints(b7, 0, 2);
    GridPane.setConstraints(b8, 1, 2);
    GridPane.setConstraints(b9, 2, 2);


    pane.getChildren().addAll(b1, b2, b3, b4, b5, b6, b7, b8, b9);

    b1.setOnAction((event) -> {
        clicker(b1);
        pane.getChildren().add(b1);
    });

    b2.setOnAction((event) -> {
        clicker(b2);
        pane.getChildren().add(b2);
    });

任何帮助将不胜感激。谢谢

0 个答案:

没有答案