182. JavaFX Hello World程序-尝试3-java:找不到符号符号:类GridPane位置:类sample.Main

问题描述

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

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

java:找不到符号

符号:GridPane类

位置:类示例。主要

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:找不到符号

符号:标签

位置:类示例。主要

代码

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,275));
        primaryStage.show();
    }
 
 
    public static void main(String[] args) {
        launch(args);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)