簡體   English   中英

無法在JavaFX中加載FXML文件

[英]Failed to load FXML file in JavaFX

因此,我正在嘗試為JavaFX做教程,並正在研究FXML示例。 但每當我在.fxml文件中向GridPane添加內容時程序崩潰。 如果沒有其他內容,它會打開一個普通的GridPane。

代碼對於FXML文件:

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

<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane fx:controller="fxmlexample.FXMLExampleController" 
    xmlns:fx="http://javafx.com/fxml" alignment="center" hgap="10" vgap="10">
<padding><Insets top="25" right="25" bottom="10" left="25"/></padding>
<Text text="Welcome" 
        GridPane.columnIndex="0" GridPane.rowIndex="0"
        GridPane.columnSpan="2"/>

    <Label text="User Name:"
        GridPane.columnIndex="0" GridPane.rowIndex="1"/>

    <TextField 
        GridPane.columnIndex="1" GridPane.rowIndex="1"/>

    <Label text="Password:"
        GridPane.columnIndex="0" GridPane.rowIndex="2"/>

    <PasswordField fx:id="passwordField" 
        GridPane.columnIndex="1" GridPane.rowIndex="2"/>
</GridPane>

主類代碼:

package fxmlexample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class FXMLExample extends Application {

public static void main(String[] args) {
    Application.launch(FXMLExample.class, args);
}

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

    Scene scene = new Scene(root, 300, 275);

    stage.setTitle("FXML Welcome");
    stage.setScene(scene);
    stage.show();
}

}

究竟是什么導致它崩潰?

我正在嘗試同樣的事情並追蹤到“Insets”條目下的問題。 在輸出的頂部有一個“Insets is not valid type”錯誤(在我理順了命名之后)。這可能取決於正在使用的NetBeans版本。 (我使用7.2.1但希望今天下午升級到7.4。)

要修復它,我只是注釋掉了“Insets”條目:

Parent root = FXMLLoader.load(getClass().getResource("fxml_example.fxml"));

上面提到的行你給出了錯誤的FXML文件名( fxml_example.fxml )。

<GridPane fx:controller="fxmlexample.FXMLExampleController"

在這里,我可以看到fxmlexample是您的FXML文件的名稱。 只需通過刪除_修復它,你的代碼就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM