简单的JavaFX HelloWorld不起作用 [英] Simple JavaFX HelloWorld Does Not Work

查看:95
本文介绍了简单的JavaFX HelloWorld不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然一遍又一遍地得到这个错误:解析onAction ='#sayHelloWorld'时出错,要么事件处理程序不在命名空间中,要么脚本中有错误。。我已经在互联网上搜索了一个解决方案,但没有任何作用,当然我是一个小细节,因为我是JAvaFX的新手,这是我的第一个HelloWorld应用程序。无论如何,这是我正在使用的代码:

I am still getting this error over and over again: Error resolving onAction='#sayHelloWorld', either the event handler is not in the Namespace or there is an error in the script.. I've googled in the Internet for a solution but nothing works, surely is a small detail somrwhere I'm missing since I'm new to JAvaFX, this is my first HelloWorld app. Anyways, this is the code I'm using:

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) {
    }
}

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

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

Found the problem. It was the ActionEvent class, the class declared in the import section is not a JavaFX class, so using the correct one makes it work. This is the final code:

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!!");
    }
}

无需注释。

这篇关于简单的JavaFX HelloWorld不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆