我正在尝试 JavaFX-2,据说其中一项功能是我们可以在 FXML 文件中使用 Javascript 创建事件处理函数。困扰我的一件事是我无法立即访问 System 类。它需要被完全引用,例如“java.lang.System”。当我们需要控制台上的简单输出时,这变得很丑陋,我的意思是,“System.out.println”丑到可以打印一些东西。而且,即使我的 FXML 具有所有 <?import java.lang.*?> 语句,显然它不会影响 <fx:script> 标记的内部。
示例代码:(注意<?language javascript?>
指令)
<?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
...