【问题标题】:Add the fxml file in the exported jar file在导出的jar文件中添加fxml文件
【发布时间】:2017-10-10 15:19:53
【问题描述】:

我正在尝试从我在 Ideallij 中创建的 javafx 项目中导出 jar 文件。我通过在输出中添加我所有的 jar 库来为工件设置属性。然后,我构建工件并创建 jar 文件。但是,当我尝试使用 java - jar name.jar 从终端运行 jar 时,我收到以下错误:

应用程序启动方法中的异常,java.lang.reflect.InvocationTargetException

fxml 文件中的错误点。如何在压缩的jar文件中添加fxml文件?

FXML 加载:

public void start(Stage primaryStage) throws Exception {

    FXMLLoader fxmlLoader = new FXMLLoader(EchoClient.class.getResource("name.fxml"));
    Parent p = fxmlLoader.load();
    Scene scene = new Scene(p);
    primaryStage.setScene( scene );
    primaryStage.setTitle("FX Echo Client");
    primaryStage.setWidth( 320 );
    primaryStage.setHeight(568);
    primaryStage.show();
}

错误位于Parent p = fxmlLoader.load();。收到的错误如下:

编辑:经过一番研究,我得出的结论是,我的问题与following post 是理想的。因此,我从 FXML 文件 中删除了删除 fx:controller 属性,并且通常在没有应用程序功能的情况下调用 jar。如何将控制器功能添加回代码?

我的 fxml 文件如下:

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="services.EchoClientController">
    <children>
        <VBox spacing="10.0" VBox.vgrow="SOMETIMES">
            <children>
                <Label text="Host" />
                <TextField fx:id="tfHost" text="localhost" />
                <Label text="Port" />
                <TextField fx:id="tfPort" text="10000" />
                <HBox spacing="4.0">
                    <children>
                        <Button fx:id="btnConnect" mnemonicParsing="false" onAction="#connect" text="Connect" />
                        <Button fx:id="btnDisconnect" mnemonicParsing="false" onAction="#disconnect" text="Disconnect" />
                    </children>
                </HBox>
            </children>
            <VBox.margin>
                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
            </VBox.margin>
        </VBox>
        <Separator prefWidth="200.0" />
        <VBox spacing="10.0" VBox.vgrow="ALWAYS">
            <children>
                <Label text="Message To Send" />
                <TextField fx:id="tfSend" />
                <Button fx:id="btnSend" mnemonicParsing="false" onAction="#send" text="Send" />
                <Label text="Message Received" />
                <TextField fx:id="tfReceive" editable="false" />
            </children>
            <VBox.margin>
                <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
            </VBox.margin>
        </VBox>
        <Separator prefWidth="200.0" />
        <VBox VBox.vgrow="NEVER">
            <VBox.margin>
                <Insets bottom="10.0" left="20.0" right="20.0" top="10.0" />
            </VBox.margin>
            <children>
                <HBox fx:id="hboxStatus" spacing="10.0">
                    <children>
                        <ProgressBar fx:id="piStatus" prefWidth="150.0" progress="0.0" />
                        <Label fx:id="lblStatus" text="Label" />
                    </children>
                </HBox>
            </children>
        </VBox>
    </children>
</VBox>

【问题讨论】:

  • 至少告诉我们你是如何通过代码加载fxml文件的。
  • 我更新了代码。
  • 嗯,你仍然需要向我们展示你的项目设置,你的文件所在的位置,你的包等等。
  • “fxml 文件中的错误点。” 如果是这样,则 fxml 文件存在问题,而不是将其添加到 jar 中...跨度>
  • 代码正在运行,我正在正确加载 fxml 文件并描绘了整个场景。

标签: java javafx


【解决方案1】:

当 .fxml 文件中有错字时,我通常会收到此错误。我认为文件本身会流向 .jar 文件,其中有一些无法解决的问题。您可以通过临时删除大部分 fxml 内容来证明这一点(确保 Java 代码不会引用任何丢失的 GUI 元素)来查看错误是否消失。

【讨论】:

  • 有没有可能与fxml文件的路径有关?
  • 如果舞台有正确的 FXML 链接,那么我不这么认为。试试我建议的解决方案来排除这种可能性。
【解决方案2】:

错误只能来自services.EchoClientController,缺失:

@FXML
private void connect() {
}

@FXML
private void disconnect() {
}

@FXML
private void send() {
}

因为不可能那么简单,看构造函数并考虑使用:

@Override
public void initialize(URL url, ResourceBundle rb) {
    // Maybe constructor code here.
}    

代替

FXMLLoader fxmlLoader = 
        new FXMLLoader(EchoClient.class.getResource("name.fxml"));
Parent p = fxmlLoader.load();

我做了(不应该有所作为)

Parent p = FXMLLoader.load(EchoClient.class.getResource("name.fxml"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-23
    • 1970-01-01
    • 2013-03-08
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多