2016-05-01 104 views
0

我的代码:使用JavaFX-无法添加全部(文本字段,文本区域,字符串)。我不应该能够吗?

public class Main extends Application { 

TextArea area = new TextArea(); 
TextField field = new TextField(); 
String text = ""; 
public void start(Stage primaryStage){ 

    VBox pane = new VBox(); 
    Button next = new Button("Next"); 
    next.setOnAction(e->{ 
     text+= "\n" + field.getText(); 
     area.setText(text); 
    }); 
    pane.getChildren().addAll(area,field,next); 
    Scene scene = new Scene(pane, 700, 300); 
    primaryStage.setTitle("CosmicWimpout"); 
    primaryStage.setScene(scene); 
    primaryStage.show(); 

} 

的错误是在.addAll,错误写着:

The method addAll(int, Collection<? extends Node>) in the type List<Node> is not applicable for the arguments (TextArea, TextField, String). 

所以,我只是编辑我的帖子,包括.addAll(区,场,旁边)。这些都是GUI节点,但the.addAll方法不接受这些参数。

+2

在向VBox添加String时,你会发生什么?它只接受'节点'。 – Itai

+0

编辑后使用的代码是一个按钮节点。仍然是一个问题。 –

+0

这应该工作。你可以创建一个[mcve](http://stackoverflow.com/help/mcve)并更新你的问题来包含它吗?这可能是导入错误导致的错误。 – Itai

回答

2

As @sillyfly已经指出paneVBox类型,它是Parent的子类型。方法getChildren将返回类型为NodeObservableList。因此子列表上的方法addAll将采用类型为Node的var-arg作为参数。 A String显然不是Node类型。

+0

是否还有其他可能的窗格(VBox除外)允许添加这些对象? –

+0

所有窗格都是“父”类型。也许你可以使用'Text'或'Label'节点而不是'String'。 – hotzst

+0

我编辑我的代码,该字符串参数不应该在那里,而是下一个参数(这是一个按钮)应该在那里。所以他们都是节点。 –

相关问题