java - 如何在 JavaFX 运行时将字符编码设置为 UTF-8
问题描述
我正在我的 JavaFX(11) 应用程序上进行国际化。
在resources.languages
中,我有多个保存为UTF-8
字符编码的 .properties 文件。
但是当我使用 resourceBundle 并将它们传递给 fxml 时,它们变成squares
如下所示。
Main.java
package application;
import java.util.Locale;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import jfxtras.styles.jmetro.JMetro;
import jfxtras.styles.jmetro.Style;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Locale locale = new Locale("my_MM");
ResourceBundle bundle = ResourceBundle.getBundle("resources.languages.langs", locale);
AnchorPane root = (AnchorPane) FXMLLoader.load(getClass().getResource("Sample.fxml"), bundle);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Sample.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="760.0" prefWidth="1200.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/15.0.1" fx:controller="application.SampleController">
<children>
<VBox prefHeight="760.0" prefWidth="268.0" style="-fx-background-color: #3367D6;" styleClass="sidePanel">
<children>
<AnchorPane prefHeight="134.0" prefWidth="268.0">
<children>
<Label layoutX="41.0" layoutY="27.0" styleClass="title" text="Title" textFill="WHITE">
<font>
<Font size="33.0" />
</font>
</Label>
<Label layoutX="41.0" layoutY="68.0" styleClass="title" text="%interTitleDescription" textFill="WHITE">
<font>
<Font size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0" style="-fx-background-color: white;">
<children>
<Label alignment="TOP_LEFT" layoutX="36.0" layoutY="18.0" text="%interStudent" textFill="#3367d6">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="34.0" layoutY="17.0" text="%interTransactions" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interDonators" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interAdmins" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
<AnchorPane prefHeight="65.0" prefWidth="268.0">
<children>
<Label layoutX="36.0" layoutY="18.0" text="%interSettings" textFill="WHITE">
<font>
<Font size="20.0" />
</font>
</Label>
</children>
</AnchorPane>
</children>
</VBox>
</children>
</AnchorPane>
langs_my_MM.properties
interTitleDescription = Management System
interStudent = ကျောင်းသားများ
interTransactions = အလှူငွေထုတ်မှတ်တမ်း
interDonators = အလှူရှင်များ
interAdmins = စီမံခန့်ခွဲသူများ
interSettings = Setting များ
解决方案
推荐阅读
- java - Maven如何根据平台配置更改依赖版本(MySQL)
- c# - 从后面的代码中添加一个 x:static 命令?
- java - 使 javafx 中的画布看起来像是亮着的
- c++ - 如何在 TBB 代码中完全关闭线程
- reactjs - 如何使用 var 作为对象的键进入反应项目
- sql - 下面的 SQL Server 查询在第 4 列中返回(无列名)。如何在第 4 列中创建列名
- c++ - 在结构中重载 ostream 和 istream 时逗号分开
- kubernetes - core-dns 说 NXDOMAIN 来自 Kubernetes 外部的查询
- amazon-web-services - 哪些资源可以驻留在 Amazon VPC 中?
- python - 使用python查找Land中最近的位置坐标