在 JavaFX 中使用 Unicode 字符

Using Unicode characters with JavaFX

我玩 swing 有一段时间了,现在决定看看 FX。到目前为止,我发现与 swing 相比,使用它更容易、更有趣,但我 运行 遇到了一个小减速带,环顾四周数小时后,我就是找不到解决方案。

当我尝试通过 fxml 文件添加它时无法使用 \u 如果我不使用 fxml 但我想使用场景,它工作正常builder,因为它更方便。 这是一小段代码:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label layoutX="145.0" layoutY="129.0" text="\u0644\u0627\u062B\u0627\u0646\u0649" />
    </children>
</AnchorPane>

我不断收到的错误是

Caused by: javafx.fxml.LoadException: Invalid escape sequence.

不确定是否相关,但我正在使用 jdk1.8.0_131 & netbeans 8.2

如果有人能在这里指出正确的方向,我将不胜感激。

FXML 是一个 XML,所以你需要使用 XML 转义:

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.131" fx:controller="baitform.designDocController">
    <children>
        <Button fx:id="button" layoutX="126" layoutY="90" onAction="#handleButtonAction" text="Click Me!" />
        <Label layoutX="145.0" layoutY="129.0" text="&#x0644;&#x0627;&#x062B;&#x0627;&#x0646;&#x0649;" />
    </children>
</AnchorPane>

话虽这么说,如果你能输入字符,你可以直接插入它们。

另请参阅:https://www.w3.org/International/questions/qa-escapes