如何在JavaFX中为非英语支持添加UTF-8? [英] How to add UTF-8 for non english support in JavaFX?

查看:135
本文介绍了如何在JavaFX中为非英语支持添加UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaFX中添加UTF-8波斯字符,但是当我添加带有波斯内容的标签时,它会显示一些奇怪的结果.

我必须使用特定的插件或配置来解决此问题吗?

解决方案

波斯语是从右到左?

在这种情况下,您想使用具有

对此问题的其他评论以及有关使用和解释正确字符编码的答案也有效,因此此答案并不完全权威.

如果源文件是ascii编码的,则可以使用 \ u 值表示非ascii字符的unicode值,如上例所示.

我认为(尽管我还没有尝试过)如果源文件(类的 .java 文件或资源的属性文件)是UTF-8编码的(必须是保存为UTF-8编码,而不是其他字符集,例如如何在Maven中配置编码?

I want to add UTF-8 Farsi characters in JavaFX but when I add a label with Persian content it shows some weird results.

Do I have to use a specific plugin or configuration to handle this issue?

解决方案

Persian is right to left?

In which case you want to use a version of JavaFX with RTL support. That would be JavaFX 8 (included in JDK8). There is a preview available here.

Make sure you have loaded and are using a font which includes the glyphs for Persian characters.

Here is some sample JavaFX code (copied from Oracle JavaFX tutorials) for displaying something which looks to me like Persian script.

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class JavaFXBidiText extends Application {
    
    @Override
    public void start(Stage stage) throws Exception {
        TextFlow textFlow = new TextFlow();
        Font font = new Font("Tahoma", 48);
        
        Text text1 = new Text("He said \u0627\u0644\u0633\u0644\u0627\u0645");
        text1.setFill(Color.RED);
        text1.setFont(font);
        Text text2 = new Text(" \u0639\u0644\u064a\u0643\u0645 to me.");
        text2.setFill(Color.BLUE);
        text2.setFont(font);
        textFlow.getChildren().addAll(text1, text2);
 
        Group group = new Group(textFlow);
        Scene scene = new Scene(group, 650, 150, Color.WHITE);
        stage.setTitle("Hello Bidi Text");
        stage.setScene(scene);
        stage.show();
    }
}

Other comments on this question and answers regarding using and interpreting the correct character encoding are also valid, so this answer is not completely authoritative.

If the source file is ascii encoded, then you can use the \u values to represent unicode values of non-ascii characters, as shown in the example above.

I think (though I haven't tried), if the source file (the .java file for the class or the property file for the resource) is UTF-8 encoded (it must be saved as UTF-8 encoding, not another character set, for example setting the file encoding in the Intellij IDEA editor) then you can directly put the unicode type characters in Strings like you would do with normal ASCII characters without using the /u encoding. If you do so, the compiler or build tool must be configured to work with UTF-8 encoded files. See for example, with Maven: How to configure encoding in Maven?

这篇关于如何在JavaFX中为非英语支持添加UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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