FXML,脚本标记和初始化方法 [英] FXML, script tag and initialize method

查看:106
本文介绍了FXML,脚本标记和初始化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试并发现JavaFX.

在JavaFX中关于控制器的FXML文档,据说如果控制器具有public void initialize()方法,则在加载FXML图后将调用该方法.

是否可以通过脚本方式从FXML文件中执行类似的操作?我尝试过类似的方法,但是initialize()根本没有被调用.

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

<?language javascript?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns:fx="http://javafx.com/fxml">

    <fx:script>
    importClass(java.lang.System);

    function initialize() {
        System.out.println('hello');
    }  
    </fx:script>

    <Button text="Button" />

</AnchorPane>

我会错过什么吗,还是无法从FXML文件中做到这一点?

是否有一种解决方法,以便FXML文件可以嵌入一些在加载后自动执行的代码(无需使用外部Java控制器文件)?

解决方案

我认为fxml中的脚本将像HTML中那样直接内联执行.因此,您不要将脚本语句放置在任何函数中.

有关示例,请参见 fxml + JavaScript节拍器应用程序. /p>

I am testing and discovering JavaFX.

In JavaFX FXML documentation about controllers, it is said that if the controller has a public void initialize() method, it is called once the FXML graph is loaded.

Is it possible to do something similar, but from the FXML file in a script way? I tried something like that, but initialize() is not called at all.

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

<?language javascript?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns:fx="http://javafx.com/fxml">

    <fx:script>
    importClass(java.lang.System);

    function initialize() {
        System.out.println('hello');
    }  
    </fx:script>

    <Button text="Button" />

</AnchorPane>

Do I miss something, or is just not possible to do that from the FXML file?

Is there a kind of workaround so the FXML file can embed some codes that will automatically execute after it is loaded (without using an external Java controller file)?

解决方案

I think the scripts in the fxml will just be executed inline just like they are in HTML. So you don't place the script statements to be executed inside any function.

See this fxml+JavaScript metronome application for a sample.

这篇关于FXML,脚本标记和初始化方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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