站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题在 NetBeans IDE 中对 GUI 窗体进行国际化(1)

在 NetBeans IDE 中对 GUI 窗体进行国际化(1)

摘要: The following tutorial shows you ways how to internationalize GUI forms in NetBeansTM IDE 5.0. In the first exercise, we will use the GUI Builder's string editor to set the display text for a GUI component with an internationalized string as you design the form. The second exercise shows you how to use the Internationalization wizard to internationalize hard-coded strings in multiple files at once. The last exercise shows you how to internationalize strings in a single file and how to easily insert an internationalized string in source code while you are coding.

Expected duration: 30 minutes

Prerequisites

This tutorial assumes you have some basic knowledge of, or programming experience with, the following technologies:

  • NetBeans IDE

Software Needed for the Tutorial

Before you begin, you need to install the following software on your computer:

Tutorial Exercises

About Internationalization

Internationalization permits applications to be adapted to various languages and regions without requiring engineering changes or recompilation. Internationalized programs enable textual elements, such as status messages and GUI component labels, to be stored outside the source code and retrieved dynamically rather than hard-coded in the program.

You typically store your internationalized strings in properties files, in the form of key/value pairs. The key is the identifier used by the program to retrieve the text, and the value is the actual text. You create one properties file for each locale (or language) in which you translate the program. The keys are the same in each locale - only the strings are different.

The IDE provides tools for doing each of the following:

Internationalizing a GUI Form at Design Time

In this exercise we will open the demo Java application project, which contains a well-known find dialog created using GUI Builder (Matisse). Next, we will create a new properties file which will contain internationalized strings. Then, we will use GUI Builder's string editor to change a text of components to an internationalized strings. In order to test our internationalized GUI form, we will add a new locale to properties file and run the form in the non-default locale.

Opening the Example Project

  1. Download and unzip the InternationlizeDemo.zip project to any location on your computer.
  2. Choose File > Open Project (Ctrl-Shit-O), select the InternationalizeDemo folder you created in the last step, and click Open.
  3. Expand Source Packages > Demo and double-click FindDialog.java. The sample form opens in the GUI Builder.

    sample form

    To view a demonstration of how to create this GUI form, click View Demo.

Creating a Properties File

  1. Choose File > New File or click the New File icon in the IDE toolbar.
  2. Select the Properties File template from the Other category and click Next.
  3. Type Bundle in the File Name field and specify the folder src/demo location.
  4. Click Finish.

    The IDE creates the Bundle.properties file (see the Projects window) and opens the Bundle(default language) file in the Source Editor.

Internationalizing Individual GUI Components

We will now use the GUI builder to enter internationalized strings for the JLabels and JButtons in the form.

  1. Select the appropriate GUI component (e.g. jLabel1) in the Design Area.
  2. In the Properties window, click ellipsis (...) button for the Text property. Note that you can also internationalize other properties that have String values, such as Mnemonic, Accessible Name, Accessible Descriptor, and ToolTip.
  3. Select Resource Bundle in the Select Mode combo box. The property editor switches to resource bundle mode.
  4. For the Bundle Name field, click Browse to select the src/demo/Bundle.properties file.
  5. Type Find_What: in the Key field and Find What: in the Value field.
  6. Click OK.

    Repeat the previous steps for all components so that the form looks like the following picture:

    internationalized form

    To view a demonstration of internationalizing jLabel1 , click View Demo

Adding a New Locale

  1. Right-click Bundle.properties in the Projects window and choose Add Locale.
  2. Enter es in the Language Code field.
  3. Enter ES in the Country Code field.
  4. Click OK.
  5. The new locale appears below the Bundle Properties node, as shown below:

    Properties file with multiple locales

  6. In the Projects window, right-click Bundle.properties and choose Open.
  7. Translate individual messages in the correspondent column of table to the new (e.g. Spanish) language, as shown below:

    Properties editor with multiple locales

Testing a Non-Default Locale

  1. In the Projects window, right-click the InternationalizeDemo project and choose Properties.
  2. In the Categories pane, select the Run node.
  3. Enter -Duser.language=es -Duser.country=ES in the VM Options field.

    properties dialog box

  4. Click OK
  5. Right-click the InternationalizeDemo project and choose Run Project

    The IDE runs the FindDialog dialog in the Spanish locale.

    internationalized form


↑返回目录
前一篇: 在 Mac OS X上开发 GUI
后一篇: Working with the Java DB (Derby) Database in NetBeans 5.5