Home > Java > javaTutorial > body text

How to use JavaFX to implement multi-language supported graphical interface in Java 9

WBOY
Release: 2023-07-29 13:02:01
Original
887 people have browsed it

How to use JavaFX in Java 9 to implement a graphical interface with multi-language support

Introduction:
With the development of globalization, multi-language support has become an important requirement for software development. Internationalizing text in different languages ​​is a necessary step when developing graphical interfaces. In Java 9, we can use JavaFX to implement a graphical interface with multi-language support. This article will introduce how to use the Resource Bundle mechanism provided by JavaFX to achieve multi-language support, and give corresponding sample code.

1. Prepare resource files
In the project, we need to prepare a resource file for each language and achieve multi-language support by loading different resource files.

1. Create a resource folder
In the src/main/java directory of the project, create a folder named resources. This folder will be used to store resource files in different languages.

2. Create resource files
In the resources folder, create a resource file for each language. The naming rule of resource files is baseName_language.properties, where baseName is the basic name of the resource file and language is the identifier of the language. For example, we can create a resource file named bundle_zh_CN.properties to store simplified Chinese text.

3. Fill in the resource content
Open the resource file and fill in the text that needs to be internationalized according to the key-value pair. For example, we can add the following content to the bundle_zh_CN.properties file:

greeting=你好!
Copy after login
Copy after login

2. Load resource files
In JavaFX, use the ResourceBundle class to load resource files. We need to select the corresponding resource file according to the user's language in the program to load.

1. Get the default Locale
Locale is a class that describes language, country and other information. We can use the java.util.Locale.getDefault() method to get the current user's default Locale.

2. Load resource files according to Locale
Use the ResourceBundle.getBundle() method to load the corresponding resource file, and the incoming parameters are the basic name and Locale of the resource file. For example, we can load the bundle_zh_CN.properties file through the following code:

ResourceBundle bundle = ResourceBundle.getBundle("bundle", Locale.CHINA);
Copy after login

3. Obtain text content
After we load the resource file, we can obtain the corresponding text based on the key in the resource file content.

1. Obtain text content in JavaFX
In JavaFX, we can achieve text internationalization by annotating @FxText. We need to use this annotation in the FXML file and set the corresponding key. For example, we can write this in the FXML file:

<Text fx:id="greeting" text="%greeting" />
Copy after login
Copy after login

2. Set text content
In the JavaFX controller class, we can get the text content by calling the getString() method of ResourceBundle, and Apply it to the corresponding control. For example, we can write this in the initialization method of the controller class:

@FXML
private Text greeting;
bundle = ResourceBundle.getBundle("bundle", Locale.getDefault());
String greetingText = bundle.getString("greeting");
greeting.setText(greetingText);
Copy after login

4. Switching languages ​​
In order to achieve multi-language support, we need to provide users with the ability to switch languages. We can implement language switching through the trigger events provided by JavaFX.

1. Binding events
In the JavaFX controller class, we can bind a trigger event to the button or menu item that switches languages. For example, we can bind a click event to a button named changeLanguageButton:

<Button fx:id="changeLanguageButton" onAction="#changeLanguageButtonClicked" />
Copy after login

2. Processing events
In the controller class, implement the method to handle the click event. In this method, we need to change the current Locale, reload the resource file, and update the interface. For example, we can implement the changeLanguageButtonClicked() method like this:

@FXML
private void changeLanguageButtonClicked() {
    if (Locale.getDefault().equals(Locale.CHINA)) {
        Locale.setDefault(Locale.US);
    } else {
        Locale.setDefault(Locale.CHINA);
    }
    bundle = ResourceBundle.getBundle("bundle", Locale.getDefault());
    String greetingText = bundle.getString("greeting");
    greeting.setText(greetingText);
}
Copy after login
Copy after login

Conclusion:
By using the resource bundle mechanism provided by JavaFX, we can easily implement a graphical interface with multi-language support. Through the introduction of this article, I believe that everyone can master the method of using JavaFX to achieve multi-language support in Java 9. I hope this article will be helpful to everyone's study.

Reference code:
Contents in resource files in different languages:
bundle_zh_CN.properties

greeting=你好!
Copy after login
Copy after login

bundle.properties

greeting=Hello!
Copy after login

@FxText used in FXML files Note:

<Text fx:id="greeting" text="%greeting" />
Copy after login
Copy after login

Method for handling click events in the controller class:

@FXML
private void changeLanguageButtonClicked() {
    if (Locale.getDefault().equals(Locale.CHINA)) {
        Locale.setDefault(Locale.US);
    } else {
        Locale.setDefault(Locale.CHINA);
    }
    bundle = ResourceBundle.getBundle("bundle", Locale.getDefault());
    String greetingText = bundle.getString("greeting");
    greeting.setText(greetingText);
}
Copy after login
Copy after login

The above is the detailed content of How to use JavaFX to implement multi-language supported graphical interface in Java 9. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!