首页 > 解决方案 > 为什么 getItems 方法不适用于按钮

问题描述

javafx类库其实说ToolBar.getItems()适用于按钮和节点,但是当我在工具栏中添加按钮时,终端不让我编译,说不合适。这里是代码

public void setBoard(){
    ToolBar toolbar = new ToolBar();//create an empty tool bar
    Button btnnt = new Button ("Next");
    Button btnpr = new Button ("Privieous");
    Button btnst = new Button ("Start");
    Button btnps = new Button ("Pause");
    toolbar.getItems().add(btnnt,btnpr,btnst,btnps);//add all buttons
    border.setBotton(toolbar);//set bottom border
    Label Columnlb = new Label("Enter the weidth");
    Label Rowlb = new Label("Enter the height");
    TextField column = new TextField();
    column.setPreflength(20);
    columns = Integer.parseInt(column.getText());
    TextField row = new TextField();
    rows = Integar.parseInt(row.getText());
    Hbox hbox = new Hbox();
    Hbox.getItems().add(Columnlb,column,Rowlb,row);
    border.setTop(hbox);
}

和错误信息

GOL.java:29: error: no suitable method found for add(Button,Button,Button,Button)
    toolbar.getItems().add(btnnt,btnpr,btnst,btnps);//add all buttons
                      ^
    method Collection.add(Node) is not applicable
      (actual and formal argument lists differ in length)
    method List.add(Node) is not applicable
      (actual and formal argument lists differ in length)
    method List.add(int,Node) is not applicable
      (actual and formal argument lists differ in length)

这就是图书馆所说的

getItems
public final ObservableList<Node> getItems()
The items contained in the ToolBar. Typical use case for a ToolBar suggest 
that the most common items to place within it are Buttons, ToggleButtons, 
and Separators, but you are not restricted to just these, and can insert 
any Node. The items added must not be null.

标签: java

解决方案



推荐阅读