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

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

百里朝
2023-03-14

JavaFX初学者,请帮助我,我的代码不断抛出这个错误。另外,我的buttonClick方法会抛出一条警告:“由于缺少'requires transitive',客户端可能无法访问来自模块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.LoadExc0019:错误解决onAction='#getText',事件处理程序不在命名空间中,或者脚本中存在错误。/C:/用户/用户/eclipse-workspace/JavaFXSceneBuilder/bin/应用程序/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个答案

步联
2023-03-14

有一个名为“getText”的“on action”事件与您的文本字段关联。当您删除它时,您的程序应该可以正常工作。

司徒嘉祥
2023-03-14

参加聚会可能会晚一点,但有同样的问题并得到了解决。问题是,在FXML文件中,您已将#buttonclick声明为一个函数,但是在代码中,该函数没有像您在attibutes上那样标记为neseccary@FXML注释。这会在启动时中断FXML加载程序,因为它无法找到没有@FXML注释的函数。

 类似资料:
  • 我是一个完全的初学者,在玩javafx。我的第一次尝试:[FXML] 和控制器类: 这将导致以下错误: 解析onAction=“#Handle”时出错,事件处理程序不在命名空间中,或者脚本中存在错误。 虽然我的控制器类被明确设置为父锚链烷的控制器。

  • 我正在创建一个JavaFX应用程序。我可以启动应用程序并登录,但当我尝试访问客户场景时,会出现以下错误: (提前为所有代码道歉 这里是CustomerScene。fxml: 以下是我的控制器类(正在进行中): 错误说它找不到事件处理程序。我不知道为什么,因为我已将@FXML附加到函数。我还尝试将函数公开(fxml文档仍然建议这两者,在第27行显示错误-

  • 上面的代码给了我以下错误: 错误CS0234:命名空间“System.Runtime.InteropServices”中不存在类型或命名空间名称“CustomMarshalers”(是否缺少程序集引用?)

  • 得到错误“错误CS0234:类型或命名空间名称cirrious中不存在”。是否缺少程序集引用?“对于PCL。并且无法更新包。 当我们试图在Mac机器上更新Xamarin studio PCL中的包时,我们得到了错误。“无法安装包”MVVMCROSS.PortableSupport 3.2.2“。您正试图将此包安装到目标为'Portable-Profile78'的项目中,但该包不包含与该框架兼容的任

  • 问题内容: 我正在尝试分析Go中的XML: 输出: 虽然我期望它会产生: 如何获得第一个标签的内容(即没有名称空间的标签)? 问题答案: 我不认为xml解码器可以指定一个元素不应该带有struct标签的名称空间。但是我确实知道它可以为您检索有关名称空间的信息,然后可以对数据进行后期处理以得到相同的结果: http://play.golang.org/p/aDEFPmHPc0

  • 有人能帮我解决这个问题吗?