Using Unicode characters with JavaFX

为君一笑 提交于 2019-12-31 01:50:27

问题


I've been playing around with swing for some time and decided to check out FX now. So far I'm finding it a lot easier and more fun to work with as compared to swing but I've run into a small speed bump and after hours of looking around I just can't find a solution.

I am unable to use \u when I try to add it through the fxml file It works fine if I don't use the fxml but I want to use the scene builder as it is more convenient. Here's the small piece of code:

<?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>

The error I keep getting is

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

Not sure if relevant, but I'm using jdk1.8.0_131 & netbeans 8.2

If anyone could point me in the right direction here I'd really appreciate it.


回答1:


FXML is an XML, and so you need to use XML escaping:

<?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>

That being said, if you are able to input the characters, you can just insert them as is.

See also: https://www.w3.org/International/questions/qa-escapes



来源:https://stackoverflow.com/questions/49016202/using-unicode-characters-with-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!