javafx.fxml.LoadException:解析 onAction='#getText' 时出错,事件处理程序不在命名空间中或脚本中存在错误



JavaFX初学者,请帮助我,我的代码不断抛出此错误。此外,我的 buttonClick 方法会抛出警告"由于缺少'需要传递',客户端可能无法访问模块 javafx.base 中的 ActionEvent 类型"。

我尝试导入不同的ActionEvent和EventHandler

我的FXML代码

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.SampleController">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="360.0" minWidth="10.0" prefWidth="301.0" />
    <ColumnConstraints hgrow="SOMETIMES" maxWidth="345.0" minWidth="10.0" prefWidth="299.0" />
  </columnConstraints>
  <rowConstraints>
      <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Text fx:id="celsius" strokeType="OUTSIDE" strokeWidth="0.0" text="Celsius:" textAlignment="CENTER" wrappingWidth="300.611328125">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </Text>
      <Text fx:id="fahrenheit" strokeType="OUTSIDE" strokeWidth="0.0" text="Fahrenheit:" textAlignment="CENTER" wrappingWidth="302.3359375" GridPane.rowIndex="1">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </Text>
      <Text fx:id="kelvin" strokeType="OUTSIDE" strokeWidth="0.0" text="Kelvin:" textAlignment="CENTER" wrappingWidth="302.490234375" GridPane.rowIndex="2">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </Text>
      <TextField fx:id="textBox" alignment="CENTER" onAction="#getText" text="0" GridPane.columnIndex="1">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </TextField>
      <Text fx:id="fahrenheitNumber" strokeType="OUTSIDE" strokeWidth="0.0" text="0" textAlignment="CENTER" wrappingWidth="300.97265625" GridPane.columnIndex="1" GridPane.rowIndex="1">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </Text>
      <Text fx:id="kelvinNumber" strokeType="OUTSIDE" strokeWidth="0.0" text="0" textAlignment="CENTER" wrappingWidth="299.97265625" GridPane.columnIndex="1" GridPane.rowIndex="2">
         <font>
            <Font name="Britannic Bold" size="36.0" />
         </font>
      </Text>
      <Button fx:id="button" mnemonicParsing="false" onAction="#buttonClick" prefHeight="25.0" prefWidth="576.0" text="Convert Now!" translateX="10.0" GridPane.columnSpan="2" GridPane.rowIndex="3">
         <font>
            <Font name="Britannic Bold" size="39.0" />
         </font>
      </Button>
   </children>
</GridPane>

我的控制器类

package application;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
public class SampleController {
    @FXML
    private Button button;
    @FXML
    private Text celsius;
    @FXML
    private Text fahrenheit;
    @FXML
    private Text kelvin;
    @FXML
    private Text fahrenheitNumber;
    @FXML
    private Text kelvinNumber;
    @FXML
    private TextField textBox;
    public void buttonClick(ActionEvent event) {
        int x = Integer.parseInt(textBox.getText());
        fahrenheitNumber.setText(Integer.toString(x * (9 / 5) + 32));
        kelvinNumber.setText(Double.toString(x + 273.15));
    }
}

应该显示一个窗口,但相反,这是抛出的。

javafx.fxml.LoadException:解析 onAction='#getText' 时出错,要么事件处理程序不在命名空间中,要么脚本中存在错误。/C:/Users/user/eclipse-workspace/JavaFXSceneBuilder/bin/application/Sample.fxml:42

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)
at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:618)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:778)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at hellofxmlcode/application.Main.start(Main.java:14)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

有一个名为"getText"的"on action"事件与您的TextField相关联。删除它时,程序应该可以正常工作。

参加聚会可能有点晚了,但遇到了同样的问题并得到了修复。问题是,在您的 FXML 文件中,您已将 #buttonclick 声明为一个函数,但是在您的代码中,该函数没有像您在服装上那样标有 nesseccary @FXML注释。这会在启动时中断 FXML 加载器,因为它在没有@FXML注释的情况下找不到该函数。

相关内容

最新更新