当前位置: 首页 > 知识库问答 >
问题:

为什么在替换FXML文件时JavaFx项目不运行

刘丰羽
2023-03-14

我使用Netbeans和JavaFx Scene Builder构建了一个JavaFx项目。

在我完成复制粘贴之前,一切工作都很顺利。

这些文件是:

Sample.FXML             -> The orriginal GUI
SampleController.java   -> The Controller class

Home.FXML               -> The FXML file whose structure is to be copied to orriginal Sample.FXML

以下是文件:

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

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="wakiliproject.SampleController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;

public class SampleController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
}
<?xml version="1.0" encoding="UTF-8"?>

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

<AnchorPane prefHeight="624.0" prefWidth="900.0" styleClass="AnchorPane" xmlns:fx="http://javafx.com/fxml" fx:controller="wakiliproject.SampleController">
  <children>
    <Pane id="wrapperPane" prefHeight="600.0" prefWidth="700.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
      <children>
        <Pane fx:id="top" layoutX="-1.0" prefWidth="879.0" styleClass="appNav">
          <children>
            <HBox id="HBox" alignment="CENTER" layoutX="12.0" layoutY="6.0" spacing="10.0">
              <children>
                <Label prefHeight="32.0" prefWidth="32.0" styleClass="titleBarHome" />
                <Label prefHeight="32.0" prefWidth="32.0" styleClass="titleBarPublisher" />
                <Label prefHeight="32.0" prefWidth="32.0" styleClass="titleBarTools" />
              </children>
            </HBox>
            <HBox id="HBox" alignment="CENTER" layoutX="667.0" layoutY="9.0" prefHeight="32.0" prefWidth="197.9998779296875" spacing="5.0">
              <children>
                <TextField prefWidth="200.0" styleClass="searchTextField" />
              </children>
            </HBox>
          </children>
        </Pane>
        <Pane id="leftNav" layoutX="14.0" layoutY="52.0" prefHeight="511.999977929685" prefWidth="652.0" styleClass="centerPane">
          <children>
            <VBox id="VBox" alignment="CENTER" layoutX="38.0" layoutY="21.0" spacing="5.0">
              <children>
                <Label styleClass="centerPaneNavLeft" text="Accounts" />
                <Label styleClass="centerPaneNavLeft" text="Cases" />
                <Label styleClass="centerPaneNavLeft" text="Callender" />
                <Label styleClass="centerPaneNavLeft" text="Matters" />
              </children>
            </VBox>
            <VBox alignment="CENTER" layoutX="7.0" layoutY="110.0" spacing="5.0" styleClass="leftVBoxProfile">
              <children>
                <Label styleClass="centerPaneNavLeft" text="Profile and settings" />
                <Label styleClass="centerPaneNavLeft" text="Firm staff" />
                <Label styleClass="centerPaneNavLeft" text="Firm Interns" />
                <Label styleClass="centerPaneNavLeft" text="Affiliates Firms" />
              </children>
            </VBox>
            <Pane id="contentPane" layoutX="126.0" layoutY="21.0" prefHeight="436.0" prefWidth="525.999977929685" styleClass="centerPaneCct">
              <children>
                <HBox id="HBox" alignment="CENTER" layoutX="14.0" spacing="10.0">
                  <children>
                    <Label minHeight="32.0" minWidth="32.0" styleClass="centerMail" />
                  </children>
                </HBox>
                <Label layoutX="253.0" layoutY="8.0" styleClass="centerNotes" text="NOTES" />
                <Pane fx:id="homeContentDisplay" layoutY="32.0" prefHeight="404.0" prefWidth="526.0">
                  <children>
                    <VBox id="VBox" alignment="BASELINE_LEFT" layoutX="14.0" layoutY="15.0" prefWidth="498.0" spacing="5.0" styleClass="centerPaneCctHomeNotifierVWrapper">
                      <children>
                        <Label styleClass="centerPaneCctHomeNotifierTtl" text="Notifications" />
                        <Label styleClass="centerPaneCctHomeNotifierNone" text="none listed" />
                      </children>
                    </VBox>
                    <VBox id="VBox" alignment="BASELINE_LEFT" layoutX="14.0" layoutY="62.0" prefWidth="498.0" spacing="5.0" styleClass="centerPaneCctHomeNotifierVWrapper">
                      <children>
                        <Label fx:id="label" styleClass="centerPaneCctHomeNotifierTtl" text="Activities today" visible="false" />
                        <Label styleClass="centerPaneCctHomeNotifierNone" text="none listed" />
                      </children>
                    </VBox>
                    <HBox id="HBox" alignment="CENTER" layoutX="435.0" layoutY="342.0" spacing="5.0" styleClass="centerSocialTabsOnlineHWrapper">
                      <children>
                        <Label prefHeight="24.0" prefWidth="24.0" styleClass="centerSocialTabsOnline" />
                        <Label prefHeight="24.0" prefWidth="24.0" styleClass="centerSocialTabsBlogs" />
                        <Label prefHeight="24.0" prefWidth="24.0" styleClass="centerSocialTabsQA" />
                      </children>
                    </HBox>
                    <Label layoutX="14.0" layoutY="109.0" styleClass="centerPaneKIWI" text="KIWI" />
                    <TextField fx:id="KIWITextField" layoutX="14.0" layoutY="134.0" prefHeight="42.0" prefWidth="498.0" styleClass="HomeKIWI" />
                    <Label id="labelShows" fx:id="GoKIWI" alignment="CENTER" layoutX="421.0" layoutY="176.0" prefWidth="91.0" styleClass="centerPaneKIWIPost" text="Post KIWI" />
                    <Label layoutX="14.0" layoutY="202.0" prefWidth="498.0" styleClass="centerPaneKIWIPostStatus" text="No KIWIs posted yet" />
                    <Pane layoutY="315.0" prefHeight="89.0" prefWidth="435.0" styleClass="footerFilesPane">
                      <children>
                        <Label layoutX="14.0" layoutY="14.0" styleClass="footerFilingNone" text="No files from this firm attached to Wakili yet." />
                        <Label layoutX="14.0" layoutY="36.0" styleClass="footerFilingNew" text="Attach new file here" />
                        <Label layoutX="397.0" layoutY="30.0" prefHeight="24.0" prefWidth="24.0" styleClass="footerFilingHelp" />
                      </children>
                    </Pane>
                    <Label layoutX="57.0" layoutY="-32.0" minHeight="32.0" minWidth="32.0" styleClass="centerContacts" />
                  </children>
                </Pane>
                <Pane fx:id="page2ContentDisplay" layoutY="32.0" prefHeight="404.0" prefWidth="526.0" visible="false">
                  <children>
                    <Label layoutX="14.0" layoutY="14.0" onMouseClicked="#handleButtonActionShowHome" text="Page 2" />
                  </children>
                </Pane>
              </children>
            </Pane>
            <Pane layoutX="-5.0" layoutY="457.0" prefWidth="656.999977929685">
              <children>
                <HBox id="HBox" alignment="CENTER" layoutX="-6.0" layoutY="12.0" prefWidth="336.0" spacing="20.0">
                  <children>
                    <Label alignment="BOTTOM_CENTER" prefHeight="40.0" styleClass="footerDocuments" text="Documents" />
                    <Label alignment="BOTTOM_CENTER" prefHeight="40.0" styleClass="footerMasterTables" text="Master Tables" />
                    <Label alignment="BOTTOM_CENTER" prefHeight="40.0" styleClass="footerFiling" text="Modules" />
                  </children>
                </HBox>
              </children>
            </Pane>
            <Label alignment="CENTER" layoutX="10.0" layoutY="222.0" prefWidth="111.0" styleClass="leftPanePersonalArea" text="My personal area" />
            <VBox id="VBox" alignment="CENTER" layoutX="40.0" layoutY="250.0" spacing="5.0">
              <children>
                <Label styleClass="leftPanePersonalAreaLinks" text="My profile" />
                <Label styleClass="leftPanePersonalAreaLinks" text="Wakili forums" />
                <Label styleClass="leftPanePersonalAreaLinks" text="Shared data" />
              </children>
            </VBox>
          </children>
          <padding>
            <Insets right="10.0" />
          </padding>
        </Pane>
        <VBox id="VBox" alignment="CENTER" layoutX="710.0" layoutY="63.0" spacing="5.0" styleClass="rightPaneBillTaxVBox">
          <children>
            <Label styleClass="centerPaneNavLeft" text="Billing and Tax" />
            <Label styleClass="centerPaneNavLeft" text="Payment management" />
          </children>
        </VBox>
        <Pane layoutX="666.0" layoutY="107.0" prefHeight="404.0" prefWidth="197.99990000000253" styleClass="rightPane">
          <children>
            <Label layoutX="14.0" layoutY="14.0" styleClass="firmTitle" text="Kirundi &amp; co. Advocates" />
            <Label layoutX="14.0" layoutY="43.0" prefHeight="32.0" styleClass="rightPaneBackUp" text="Back-up firm data online" />
            <HBox id="HBox" alignment="CENTER" layoutX="12.0" layoutY="94.0" spacing="10.0" styleClass="rightPaneKSLLSKHWrapper">
              <children>
                <Label styleClass="rightPaneKSLLSK" text="LSK" />
                <Label styleClass="rightPaneKSLLSK" text="KSL">
                  <HBox.margin>
                    <Insets />
                  </HBox.margin>
                </Label>
              </children>
            </HBox>
            <Label layoutX="14.0" layoutY="120.0" styleClass="rightPaneTheCourts" text="The courts" />
            <VBox id="VBox" alignment="CENTER_LEFT" layoutX="66.0" layoutY="143.0" spacing="5.0">
              <children>
                <Label styleClass="rightPaneTheCourtsLinks" text="Announcements" />
                <Label styleClass="rightPaneTheCourtsLinks" text="Popular cases" />
              </children>
            </VBox>
            <Label layoutX="50.0" layoutY="75.0" styleClass="rightPaneFirmWebsite" text="Go to firm website" />
          </children>
        </Pane>
        <Label layoutX="746.0" layoutY="511.0" styleClass="footerCAMPORey" text="CAMP - ORey" />
      </children>
    </Pane>
  </children>
  <padding>
    <Insets bottom="10.0" left="10.0" right="10.0" top="10.0" />
  </padding>
  <stylesheets>
    <URL value="@../Styling/Buttons.css" />
    <URL value="@../Styling/Direction.css" />
    <URL value="@../Styling/Labels.css" />
    <URL value="@../Styling/Panes.css" />
    <URL value="@../Styling/Text.css" />
    <URL value="@../Styling/TextArea.css" />
  </stylesheets>
</AnchorPane>
*Identical to home above*
Controller method "handleButtonActionShowHome" not found.
file:/D:/xampp/htdocs/Jive/WakiliProject/dist/WakiliProject.jar!/wakiliproject/Home.fxml:94
  at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:492)
  at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:589)
  at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2744)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2723)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2709)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2696)
  at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2685)
  at wakiliproject.WakiliProject.start(WakiliProject.java:13)
  at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
  at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
  at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
  at java.security.AccessController.doPrivileged(Native Method)
  at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
  at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
  at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
  at java.lang.Thread.run(Thread.java:724)

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:724)
Caused by: javafx.fxml.LoadException: Controller method "handleButtonActionShowHome" not found.
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:492)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:589)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2430)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2136)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2028)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2744)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2723)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2709)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2696)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2685)
    at wakiliproject.WakiliProject.start(WakiliProject.java:13)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more
Java Result: 1
jfxsa-run:
BUILD SUCCESSFUL (total time: 52 seconds)

共有1个答案

文志学
2023-03-14

从FXML文件调用的方法必须包含在controller类的某个位置。

在本例中,HandleButtonActionShowHome()方法未包含在SampleController类中。

 类似资料:
  • 但指定的错误中的位置包含它所引用的文件。谁能解释一下这个错误的原因。是代码问题还是插件问题?

  • 我试图按照本教程在IntelliJ中设置Maven JavaFX项目https://openjfx.io/openjfx-docs/#maven(带Maven的IntelliJ非模块化)但无论我做什么,它都失败了。 我一直在尝试很多互联网建议。我尝试在pom中设置VM变量,我确保JAVA\u HOME指向我的JDK 12目录,并检查项目结构中JDK的设置是否正确。 这是我的pom文件: 这是输出:

  • 我在intelliJ中创建了一个Java /JavaFX应用程序。我创建了它作为一个Java的应用程序,后来为JavaFX导入了必要的部件。 我尝试了Izpack、Javapackager、Jpackager、Jpackage和创建胖jar中的各种安装程序/打包程序创建者。我什么都不行。 该应用程序在IntelliJ中运行良好。 为什么它不会在别处运行? 这是我的POM、代码和错误消息。 如果你能

  • 在Maven项目中运行main类时,我得到: 应用程序启动方法中的异常 据我所知,这是因为FXML文件无法加载。 这是文件系统层次结构 在Eclipse(src内部) 阅读apache的maven指南和其他statck问题,下面的代码片段似乎是创建并指向文件的正确方法。 但是这段代码将抛出前面声明的异常。 当我试图找出代码中异常

  • 我有一个标准的(非JavaFX)MavenJava项目。它应该做的第一件事是加载一个. fxml文件,但是我似乎无法使用找到它。 这是不寻常的,因为此项目是从标准Java项目导入的,该项目运行良好,没有错误。 以下是主要方法: URL总是返回为。 我有作为测试方法,看看URL对象是否返回为null。根据我所读到的关于的所有内容,它应该得到与调用类相同的包中的任何对象,这是。 以下是

  • 为什么在不能在TypeScript项目执行.ts文件? 我想要封装一个类,并且使用ts-node执行: src/index.ts test/testPerson.ts 我运行ts-node 执行 测试文件报错:不识别.ts后缀的文件: 我按照网上说的,找到package.json 删除: "type": "module", 但是又会报其他错误: 请问: 1、package.json内"type":