


How to use JavaFX to implement multi-language supported graphical interface in Java 9
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=你好!
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);
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" />
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);
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" />
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); }
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=你好!
bundle.properties
greeting=Hello!
@FxText used in FXML files Note:
<Text fx:id="greeting" text="%greeting" />
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); }
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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools