You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX WebView中格鲁吉亚语显示为方块的问题求助

JavaFX WebView 格鲁吉亚语显示为方块的解决方法

使用javafx.scene.web.WebView加载格鲁吉亚语文本时,内容显示为方块(□),示例代码及依赖如下:

示例代码

public void start(Stage stage) throws IOException {
    FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
    Scene scene = new Scene(fxmlLoader.load(), 320, 240);
    WebView webView = (WebView) scene.lookup("#web");
    WebEngine engine = webView.getEngine();
    String html = """
        გამარჯობა, ეს არის ტესტი.
        """;
    engine.loadContent(html);

    stage.setTitle("Hello!");
    stage.setScene(scene);
    stage.show();
}

依赖配置

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx-web</artifactId>
    <version>23.0.2</version>
</dependency>

显示异常效果

显示结果

解决方法

1. 包装内容为标准HTML并指定UTF-8编码

直接加载纯文本时,WebEngine可能未正确识别字符编码,将内容包装为完整HTML文档并指定UTF-8编码即可:

String html = """
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        გამარჯობა, ეს არის ტესტი.
    </body>
    </html>
    """;
engine.loadContent(html);

2. 确保系统或HTML中指定支持格鲁吉亚语的字体

WebView依赖系统字体渲染,若系统未安装支持格鲁吉亚语的字体(如Noto Sans Georgian、Arial Unicode MS),会出现方块。可以:

  • 直接在系统中安装对应字体;
  • 或在HTML中显式指定字体:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style>
        body { font-family: 'Noto Sans Georgian', 'Arial Unicode MS', sans-serif; }
    </style>
</head>
<body>
    გამარჯობა, ეს არის ტესტი.
</body>
</html>

3. 验证JavaFX WebKit组件支持

确认使用的JavaFX版本(23.0.2)的WebKit组件对格鲁吉亚语的支持,若问题持续,可尝试升级至最新稳定版JavaFX。

内容的提问来源于stack exchange,提问作者Арсений Коротков

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:07:09