首页 > 解决方案 > 如何在 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 များ

标签: javajavafxencoding

解决方案


推荐阅读