java - 182. JavaFX Hello World Program -- Try 3 -- java: cannot find symbol symbol: class GridPane location: class sample.Main
问题描述
- 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);
}
}
解决方案
推荐阅读
- python - 如何从字典中添加按钮,但前提是字典声明它已启用?
- html - 如何使图像居中显示:网格
- java - 来自 Netlogo 的 JavaObject 使用 py4j 没有长度?
- azure - 使用 Azure 从本地应用程序设置电子邮件通知的最佳实践
- r - 在 R 中拟合瑞利
- flutter - 店铺一览
到 SharedPreferences - php - laravel 表单请求的自定义错误代码
- webview2 - WebView2 边缘扩展功能
- scala - Scala Play 阅读:如何展平包含要建模的数组的 Json
- react-native - React-native fontFamily 无法正常工作