首页 > 解决方案 > 182. JavaFX Hello World Program -- Try 3 -- java: cannot find symbol symbol: class GridPane location: class sample.Main

问题描述

  1. JavaFX Hello World 程序 -- 尝试 3 -- java:找不到符号 符号:类 GridPane 位置:类 sample.Main 0 Paolo · 第 182 课 · 16 分钟前 第 180 课(第一个 JavaFX 项目)现在有效。

在第 182 课中,当我:注释掉 FXMLLoader 并尝试通过代码 setAlignment()、setVgap() 和 setHgap() 时,我收到以下错误:

java: 找不到符号

符号:类 GridPane

位置:类sample.Main

package sample;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class Main extends Application {
 
    @Override
   public void start(Stage primaryStage) throws Exception{
//        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
 
        GridPane root = new GridPane();
        root.setAlignment(Pos.CENTER);
        root.setVgap(10);
        root.setHgap(10);
 
        primaryStage.setTitle("Hello JavaFX!");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }
 
 
    public static void main(String[] args) {
        launch(args);
    }
}
 
----------------------------------------------
Controller.java:
 
package sample;
 
public class Controller {
}
 
----------------------------------------------
 
sample.fxml:
 
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
 
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<GridPane fx:controller="sample.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
</GridPane>
 
 

当我重新激活 FXMLLoader 并尝试添加“标签问候语”时,我收到以下错误:

java: 找不到符号

符号:类标签

位置:类sample.Main

源代码:

package sample;
 
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
 
public class Main extends Application {
 
    @Override
   public void start(Stage primaryStage) throws Exception{
       Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
/*
        GridPane root = new GridPane();
        root.setAlignment(Pos.CENTER);
        root.setVgap(10);
        root.setHgap(10);
*/
        Label greeting = new Label("Welcome to JavaFX!");
        root.getChildren().add(greeting);
 
        primaryStage.setTitle("Hello JavaFX!");
        primaryStage.setScene(new Scene(root, 300, 275));
        primaryStage.show();
    }
 
 
    public static void main(String[] args) {
        launch(args);
    }
}

标签: javajavafx-11

解决方案


推荐阅读