无法使用GridPane在java中加载fxml文件

时间:2017-01-27 22:45:16

标签: java javafx scenebuilder

我正在使用场景构建器查看一些javafx教程。我创建了我的fxml文件但是当我尝试加载它时出现错误:

Jan 27, 2017 10:38:39 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.111 by JavaFX runtime of version 8.0.101
java.lang.ClassCastException: javafx.scene.layout.AnchorPane cannot be cast to javafx.scene.layout.GridPane
    at application.Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)

我检查了SO并发现this thread但我仍然没有得到我做错的事情,因为我甚至没有使用Anchor窗格。无论如何,这里是主要的课程:

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;


public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            //BorderPane root = new BorderPane();
            GridPane grid = FXMLLoader.load(getClass().getResource("ui.fxml"));
            Scene scene = new Scene(grid,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

我尝试使用BorderPane,但结果相同。如果我删除加载fxml功能它全部工作并显示一个空的阶段,这是我所期待的。 知道它有什么问题吗? 编辑:好的我已经添加了FXML文件,现在是的我可以在那里看到anchorPane(抱歉,但我从来没有使用过JavaFX,所以我还在学习。):

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
   <children>
      <GridPane prefHeight="396.0" prefWidth="425.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints maxHeight="295.0" minHeight="10.0" prefHeight="295.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="124.0" minHeight="10.0" prefHeight="54.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="75.0" minHeight="10.0" prefHeight="47.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <TableView prefHeight="200.0" prefWidth="200.0" GridPane.columnSpan="3">
               <columns>
                  <TableColumn prefWidth="75.0" text="Column X" />
                  <TableColumn prefWidth="75.0" text="Column X" />
                  <TableColumn prefWidth="75.0" text="Column X" />
               </columns>
            </TableView>
         </children>
      </GridPane>
   </children>
</AnchorPane>

不完全确定为什么这个被标记为重复,因为所指出的问题通常涵盖ClassCastException,而我指的是引起ClassCastException的特定问题。无论如何,我已经将违规行更改为: AnchorPane pane = FXMLLoader.load(getClass().getResource("ui.fxml"));并且它有效。当我按照教程学习时,我甚至不知道我应该在看一个AnchorPane。

0 个答案:

没有答案