2014-01-15 139 views
4

因此,我正在尝试为JavaFX制作教程,并且正在研究FXML示例。但是每当我在.fxml文件的GridPane中添加一些东西时,程序就会崩溃。如果没有其他东西放在里面,它会打开一个正常的GridPane。无法在JavaFX中加载FXML文件

代码对于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(); 
} 

}

到底是什么导致它崩溃?

回答

0

我正在尝试相同的事情,并跟踪到“Insets”条目的问题。在我输出的顶部有一个“Insets不是有效类型”的错误(在我理清了命名之后)。这可能取决于所使用的NetBeans版本。 (我使用7.2.1,但希望能得到今天下午升级至7.4)

为了解决这个问题,我只是注释掉“插图”条目:

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

上面提到的线你给FXML文件名称的错误名称(fxml_example.fxml)。

<GridPane fx:controller="fxmlexample.FXMLExampleController"

在这里我可以看到fxmlexample是你FXML文件的名称。只需通过删除_来修复它,并且您的代码很好。