使用 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节点,但是.addAll方法不接受这些参数。

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

您要导入哪些类?对我来说,当我导入时发生了错误:

import java.awt.Label;

而不是:

import javafx.scene.control.Label;

当IDE自动为您导入类时,通常会发生这种情况。有时您应该考虑手动输入它们。

最新更新