


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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Below are the various geometric shapes you can draw using JavaFX Lines - A line is a geometric structure that connects two points. javafx.scene.shape. The Line class represents a line in the XY plane. Rectangle - A rectangle is a four-sided polygon with two pairs of parallel and concurrent sides, and all interior angles are right angles. javafx.scene. The Rectangle class represents a rectangle in the XY plane. Circle - A circle is a line forming a closed loop, with each point on it being a fixed distance from the center point. javafx.scene. The Circle class represents a circle in the XY plane. Ellipse - An ellipse is defined by two points, each point is called a focus. If you take any point on the ellipse, the sum of the distances to the focus

Use the new JavaFXWebView component in Java13 to display web content. With the continuous development of Java, JavaFX has become one of the main tools for building cross-platform graphical interfaces. JavaFX provides a wealth of graphics libraries and components, allowing developers to easily create a variety of user interfaces. Among them, the JavaFXWebView component is a very useful component that allows us to display web content in JavaFX applications. In Java13, J

Laravel is a very popular PHP framework that provides a large number of features and libraries that make web application development easier and more efficient. One of the important features is multi-language support. Laravel achieves multi-language support through its own language package mechanism and third-party libraries. This article will introduce how to use Laravel to implement multi-language support and provide specific code examples. Using Laravel's language pack function Laravel comes with a language pack mechanism that allows us to easily implement multilingualism

Basic configuration guide for using Qt to develop graphical interface applications under Linux 1. Introduction Qt is a set of cross-platform C++ libraries. It provides a rich set of graphical interface development components and many other functional modules, suitable for developing cross-platform graphical interfaces. app. This article will introduce how to develop Qt under Linux system, and give some basic configuration guidelines and code examples. 2. Install the Qt development environment and download Qt. Download the appropriate Qt from the Qt official website (http://www.qt.io/)

How to use JavaFX to build a responsive UI interface in Java9 Introduction: In the development process of computer applications, the user interface (UI) is a very important part. A good UI can improve the user experience and make the application more attractive. JavaFX is a graphical user interface (GUI) framework on the Java platform. It provides a rich set of tools and APIs to quickly build interactive UI interfaces. In Java 9, JavaFX has become a JavaSE

How to use JavaFX and WebSocket to implement a graphical interface for real-time communication in Java9 Introduction: With the development of the Internet, the need for real-time communication is becoming more and more common. In Java9, we can use JavaFX and WebSocket technology to implement real-time communication applications with graphical interfaces. This article will introduce how to use JavaFX and WebSocket technology to implement a graphical interface for real-time communication in Java9, and attach corresponding code examples. Part One: Ja

Nowadays, with the continuous development of Internet technology, more and more websites and applications need to support multi-language and internationalization. In web development, using frameworks can greatly simplify the development process. This article will introduce how to use the Webman framework to achieve internationalization and multi-language support, and provide some code examples. 1. What is the Webman framework? Webman is a lightweight PHP-based framework that provides rich functionality and easy-to-use tools for developing web applications. One of them is internationalization and multi-

JavaFX is a framework for building rich client applications, but during use, you may encounter some JavaFX graphics errors, which will affect the normal operation of the application. This article explains how to deal with and avoid JavaFX graphics errors. 1. Types of JavaFX graphics errors There are many types of JavaFX graphics errors, including the following aspects: 1. Thread error: JavaFX needs to be executed on the UI thread. If the JavaFX code is executed on the background thread, a thread error will occur.
