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

时间:2019-04-13 01:56:23

标签: java

JavaFX的初学者,请帮助我,我的代码不断抛出此错误。另外,我的buttonClick方法引发警告“由于缺少'requirestransive',客户端可能无法访问模块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)

2 个答案:

答案 0 :(得分:0)

与您的TextField关联的一个名为“ getText”的“活动中”事件。删除它后,您的程序应该可以正常运行。

答案 1 :(得分:0)

参加聚会可能会有点晚,但遇到了同样的问题并已解决。问题在于,在FXML文件中,您已将#buttonclick声明为一个函数,但是在您的代码中,该函数并未像您的服装上那样用nesseccary @FXML批注进行标记。因为没有@FXML批注就找不到函数,这会在启动时中断FXML-loader。