Java getting .setText error in my java code

Godwin :

Please, I'm new to java and I'm currently following a tutorial on java but am getting an error relating to setText() method it's not just working and I can't tell the reason it's not working.

This is the code i tried:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.awt.*;

public class Demo extends Application {

    Button button = new Button();

    public static void main(String args[])
    {

        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        primaryStage.setTitle("This is a title");

        button.setText("This is a button");

        StackPane stackPane = new StackPane();
        stackPane.getChildren().add(button);

        Scene scene = new Scene(stackPane,400,400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

This is the error am getting:

Error:(23, 15) java: cannot find symbol
  symbol:   method setText(java.lang.String)
  location: variable button of type java.awt.Button
Sebastian S :

You imported a java.awt.Button, while you probably meant to use a javafx.scene.control.Button.

Explanation: In Java there can be multiple classes with the same name but in different packages. You need to make sure to use the correct class by choosing the right package during import. In rare cases when you might need both classes at the same time, you can also use the fully qualified name of a class in your code, i.e. new javafx.scene.control.Button().

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=310776&siteId=1