按钮[] []到节点 - java

时间:2016-07-20 01:07:15

标签: java javafx

我会保持快速。我正在尝试创建一个标准计算器。我想使用BorderPane所以我可以保留我的显示:

 TextField display = new TextField();
 pane.setTop(display);

但是我使用的是数组:
然后,我想使用一组按钮作为单个对象放置在窗格的中心。

 Button [][] keys = new Button [a][b];
 pane.setCenter(keys);

然而我收到错误:

Button [][] cannot be converted to Node.

我想知道无论如何我可以解析元素以成为/假设Node的值,或者我是否可以创建一个方法来执行此操作。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

center的{​​{1}}节点是单个节点。您无法在那里添加多个节点。这意味着您需要将它们添加到合适的BorderPaneParent将是合适的候选人

示例:

GridPane

至于评论中提到的问题:

@Override
public void start(Stage primaryStage) {
    BorderPane pane = new BorderPane();

    Button[][] keys = new Button[3][];
    for (int i = 0; i < 3; i++) {
        int num = (12 - i) / 3;
        Button[] column = new Button[num];
        keys[i] = column;
        for (int j = 0, key = 7 + i; j < num; j++, key -= 3) {
            Button button = new Button(Integer.toString(Math.max(0, key)));
            column[j] = button;

            // make button width resizeable
            button.setMaxWidth(Double.MAX_VALUE);

            // buttons should fill the available width
            GridPane.setFillWidth(button, Boolean.TRUE);
        }
    }

    GridPane gpane = new GridPane();

    for (int i = 0; i < keys.length; i++) {
        Button[] column = keys[i];
        gpane.addColumn(i, Arrays.copyOf(column, column.length, Node[].class));
    }

    // make 0-Button fill 3 columns
    GridPane.setColumnSpan(keys[0][3], 3);

    pane.setCenter(gpane);

    Scene scene = new Scene(pane);

    primaryStage.setScene(scene);
    primaryStage.show();
}

产生错误,因为for(int i=0; i=list.length(); i++) 是一个赋值,因此结果与赋值的目标具有相同的类型,即在这种情况下为i=list.length()。 Java期望int循环中的中间表达式具有for类型。您将赋值运算符boolean与等号运算符=混淆,但这里应该是不等式,因为只要未达到长度,循环就应该运行。此外,==括号似乎对我很可疑,因为数组中没有返回长度的方法,它是一个字段。

()