【问题标题】:Simple JavaFX HelloWorld Does Not Work简单的 JavaFX HelloWorld 不起作用
【发布时间】:2015-07-20 13:57:48
【问题描述】:

我仍然一遍又一遍地收到此错误:Error resolving onAction='#sayHelloWorld', either the event handler is not in the Namespace or there is an error in the script.。我在互联网上搜索了一个解决方案,但没有任何效果,肯定是一个小细节,因为我是 JAvaFX 的新手,所以我缺少一个小细节,这是我的第一个 HelloWorld 应用程序。无论如何,这是我正在使用的代码:

sample.fxml:

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

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<GridPane alignment="center" hgap="10" vgap="10" xmlns="http://javafx.com/javafx/8"
          xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.SampleController">
    <columnConstraints>
        <ColumnConstraints />
        <ColumnConstraints />
    </columnConstraints>
    <rowConstraints>
        <RowConstraints />
        <RowConstraints />
    </rowConstraints>
   <children>
       <Button text="Load News"
               GridPane.columnIndex="1" GridPane.rowIndex="1"
               onAction="#sayHelloWorld"/>
       <Label GridPane.columnIndex="0" GridPane.rowIndex="1" fx:id="helloWorld"/>
   </children>
</GridPane>

还有SampleController.java

package sample;

import javafx.scene.control.Label;

import java.awt.event.ActionEvent;

public class SampleController {
    public Label helloWorld;

    public void sayHelloWorld(ActionEvent actionEvent) {
    }
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java javafx javafx-2


    【解决方案1】:

    发现问题。它是 ActionEvent 类,在导入部分中声明的类不是 JavaFX 类,因此使用正确的类可以使其工作。这是最终代码:

    package sample;
    
    //import java.awt.event.ActionEvent;  //This was the error!
    import javafx.event.ActionEvent;
    import javafx.scene.control.Label;
    
    public class SampleController {
        public Label helloWorld;
    
        public void sayHelloWorld(ActionEvent actionEvent) {
            helloWorld.setText("Hello World!!");
        }
    }
    

    不需要注释。

    【讨论】:

    • 感谢您发布您的答案!这也是我的问题。
    【解决方案2】:

    您缺少 @FXML 注释标记以使内容可被标记访问

        public class SampleController 
        {
            @FXML
            public Label helloWorld;
    
            @FXML
            public void sayHelloWorld(ActionEvent actionEvent) 
            {
                //....
            }
        }
    

    【讨论】:

    • 不,这不是答案。即使有注释它仍然不起作用,
    • 你能详细说明你是如何编译这个项目的吗?
    • 您好,感谢您的回答,但我已经发现了问题。请参阅下面的答案。这是一个错误的import
    【解决方案3】:

    像这样添加 fxml 注释

        package sample;
    
    import javafx.scene.control.Label;
    
    import java.awt.event.ActionEvent;
    
    public class SampleController {
        public Label helloWorld;
        @FXML
        public void sayHelloWorld(ActionEvent actionEvent) {
        }
    }
    

    没有注释是找不到的

    【讨论】:

    • 不,这不是答案。即使有注释它仍然不起作用,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 2013-12-11
    • 2013-10-14
    • 2021-02-16
    • 2021-03-11
    • 2021-05-18
    • 2018-11-14
    相关资源
    最近更新 更多