有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

运行Java FXML项目时出现异常(应用程序启动方法中出现异常)

我对FXML及其语法一无所知,因此我必须this 甲骨文关于这个主题的教程做了一些研究

我已经按照指定完成了图4-2上面的所有工作(除了使用Eclipse而不是NetBeans),但一旦我运行了项目,我在控制台上得到的就是:

Exception in Application start method

而舞台及其组成部分都没有出现

此外,此窗口将显示: enter image description here

我在互联网上做了一些研究,但找不到关于这个话题的信息。在StackOverFlow上有一些问题,但问题的原因不同

FXMLTableView.java(主要方法):

package application;

public class FXMLTableView extends Application{

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

@Override
public void start(Stage primaryStage) throws Exception {

    Pane root = (Pane) FXMLLoader.load(getClass().getResource("fxml_tableview.fxml"));

    primaryStage.setTitle("This is a title");
    primaryStage.setScene(new Scene(root, 400, 400));
    primaryStage.show();

    }
}

fxml_tableview.fxml

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

<?import javafx.scene.layout.AnchorPane?>

<GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
    xmlns:fx="http://javafx.com/fxml"
    fx:controller="fxmltableview.FXMLTableViewController">
    <padding>
        <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
    </padding>
    <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book"                
        GridPane.columnIndex="0" GridPane.rowIndex="0">
    </Label>
    <TableView fx:id="tableView" GridPane.columnIndex="0" 
        GridPane.rowIndex="1">
    </TableView>
</GridPane>

我认为没有必要显示FXMLTableViewController,因为我还没有使用它,因此它实际上是空的,但以防万一:

package application;

public class FXMLTableViewController {

}

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    已解决

    问题是因为我没有导入FXML文件中使用的每个类的库

    必须是这样的:

    <?import javafx.geometry.Insets?>
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    
    <GridPane alignment="CENTER" hgap="10.0" vgap="10.0"
       xmlns:fx="http://javafx.com/fxml"
       fx:controller="fxmltableview.FXMLTableViewController">
       <padding>
          <Insets bottom="10.0" left="10.0" right="10.0" top="10.0"/>
       </padding>
       <Label style="-fx-font: NORMAL 20 Tahoma;" text="Address Book"                
        GridPane.columnIndex="0" GridPane.rowIndex="0">
       </Label>
       <TableView fx:id="tableView" GridPane.columnIndex="0" 
           GridPane.rowIndex="1">
       </TableView>
    </GridPane>
    

    @n247s谢谢你的帮助