无法解析方法 getChildren.addAll();

Cannot resolve method getChildren.addAll();

好的,我不确定我问的地方是否正确,但我希望这里有人可以帮助我。所以,我是 Java 的初学者,我正在尝试制作 JavaFX 应用程序,但我的布局 1 "getChildren.addAll(label1, button1);" 被标记为错误。该错误是:

无法解析方法 'addAll(java.awt.Label, javafx.scene.control.Button)'

非常感谢对问题的任何建议或帮助。谢谢你,如果你读到这里。


package sample;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;

import java.awt.*;

  public class Main extends Application {

Stage window;
Scene scene1, scene2;

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage primaryStage) {
    window = primaryStage;

    //Button 1
    Label label1 = new Label("Welcome to the first scene!");
    Button button1 = new Button("Go to scene 2");
    button1.setOnAction(e -> window.setScene(scene2));

    //Layout 1 - children laid out in vertical column
    VBox layout1 = new VBox(20);
    layout1.getChildren().addAll(label1, button1);
    scene1 = new Scene(layout1, 200, 200);

    //Button 2
    Button button2 = new Button("Back to Scene 1");
    button2.setOnAction(e -> window.setScene(scene1));

    //Layout 2
    StackPane layout2 = new StackPane();
    layout2.getChildren().add(button2);
    scene2 = new Scene(layout2, 450, 500);

    window.setScene(scene1);
    window.setTitle("The Title");
    window.show();
}


 }

请确保您没有从 java.awt.* 导入任何 类。

在你的例外情况下,它已经说你正在使用 java.awt.Label。您需要 JavaFX 版本:

import javafx.scene.control.Label;