是否可以将类导入FXML< fx:script>块(JavaFX-2) [英] Is It Possible To Import Classes into FXML <fx:script> block (JavaFX-2)

查看:155
本文介绍了是否可以将类导入FXML< fx:script>块(JavaFX-2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试JavaFX-2,据说其中一项功能是我们可以使用FXML文件中的Javascript创建事件处理程序功能.让我烦恼的一件事是我无法立即访问System类.需要完全引用它,例如"java.lang.System".当我们在控制台上需要一个简单的输出时,这变得很丑陋,我的意思是,"System.out.println"丑陋到足以打印出某些东西的程度. 而且,即使我的FXML具有所有的<?import java.lang.*?>语句,显然不会影响< fx:script>内部的内容标签.

I'm trying out JavaFX-2 and one of the features is said to be that we can create event handler functions using Javascript in our FXML file. One thing that bugged me is I can't access the System class right away. It needs to be fully referenced, like "java.lang.System". That becomes ugly when we need a simple output on the console, I mean come on, "System.out.println" is ugly enough to get something printed. And, even though my FXML has all the <?import java.lang.*?> statements, apparently it doesn't affect inside of the <fx:script> tags.

示例代码:(请注意<?language javascript?>指令)

Sample code: (Notice the <?language javascript?> directive)

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

<?language javascript?>

<?import java.lang.*?>
<?import java.net.*?>
<?import java.util.*?>
<?import javafx.collections.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane fx:id="rootPane">
  <fx:script>
    function buttonAction(event){
      java.lang.System.out.println("finally we get to print something.");
      // System.out.println("This wouldn't work even if it wasn't a comment line, anyway");
    }
  </fx:script>
  <children>
    <Button id="close" mnemonicParsing="false" onAction="buttonAction(event)" text="">
      <graphic>
        <ImageView pickOnBounds="true">
          <image>
            <Image url="@../resources/close.png" preserveRatio="true" smooth="true" />
          </image>
        </ImageView>
      </graphic>
    </Button>
  </children>
</AnchorPane>

所以我的问题是:是否可以将类导入到< fx:script>中?我记得我在Actionscript3.0中确实很容易做到:import flash.events.MouseEvent ...

So my question is: Is there any way to import classes into <fx:script>? I remember I was doing this in Actionscript3.0 really easy: import flash.events.MouseEvent ...

推荐答案

使用javascript内置importPackageimportClass函数.

Use javascript build-in importPackage or importClass functions.

 <fx:script>
    importPackage(java.lang);
    function buttonAction(event){
        System.out.println("finally we get to print something.");
    }
</fx:script>

有关更多详细信息,请参见: http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsimport

For more details, see: http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsimport

这篇关于是否可以将类导入FXML&lt; fx:script&gt;块(JavaFX-2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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