This program may seem a bit silly, but it’s a good example of
configuring a variety of user interface controls from a
resource bundle. The
BusCard
program allows you to create a digital
business card (“interactive business card”) on-screen
(see Figure 14-2). The labels for all the GUI
controls, event the pull-down menu, are loaded from a
ResourceBundle
.
Example 14-2 shows the code for the
BusCard
program.
Example 14-2. BusCard.java
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; /** Display your business-card information in a Java window. * * This is a first attempt. The next version should use a GridBagLayout. */ public class BusCard extends JFrame { JLabel nameTF; JComboBox jobChoice; JButton B1, B2, B3, B4; /** "main program" method - construct and show */ public static void main(String[] av) { // create a BusCard object, tell it to show up new BusCard( ).setVisible(true); } /** Construct the object including its GUI */ public BusCard( ) { super( ); Container cp = getContentPane( ); cp.setLayout(new GridLayout(0, 1)); addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { setVisible(false); dispose( ); System.exit(0); } }); JMenuBar mb = new JMenuBar( ); setJMenuBar(mb); ResourceBundle b = ResourceBundle.getBundle("BusCard"); JMenu aMenu; aMenu = I18N.mkMenu(b, "filemenu"); mb.add(aMenu); JMenuItem mi = I18N.mkMenuItem(b, "filemenu", "exit"); aMenu.add(mi); mi.addActionListener(new ActionListener( ) { public void actionPerformed(ActionEvent e) { System.exit(0); } }); aMenu = I18N.mkMenu(b, "editmenu"); mb.add(aMenu); aMenu = I18N.mkMenu(b, "viewmenu"); mb.add(aMenu); aMenu = I18N.mkMenu(b, "optionsmenu"); mb.add(aMenu); aMenu = I18N.mkMenu(b, "helpmenu"); mb.add(aMenu); //mb.setHelpMenu(aMenu); // needed for portability (Motif, etc.). setTitle(I18N.getString(b, "card"+".company", "TITLE")); JPanel p1 = new JPanel( ); p1.setLayout(new GridLayout(0, 1, 50, 10)); nameTF = new JLabel("My Name", JLabel.CENTER); nameTF.setFont(new Font("helvetica", Font.BOLD, 18)); nameTF.setText(I18N.getString(b, "card"+".myname", "MYNAME")); p1.add(nameTF); jobChoice = new JComboBox( ); jobChoice.setFont(new Font("helvetica", Font.BOLD, 14)); // Get Job Titles ofrom the Properties file loaded into "b"! String next; int i=1; do { next = I18N.getString(b, "job_title" + i++, null); if (next != null) jobChoice.addItem(next); } while (next != null); p1.add(jobChoice); cp.add(p1); JPanel p2 = new JPanel( ); p2.setLayout(new GridLayout(2, 2, 10, 10)); B1 = new JButton( ); B1.setLabel(I18N.getString(b, "button1.label", "BUTTON LABEL")); p2.add(B1); B2 = new JButton( ); B2.setLabel(I18N.getString(b, "button2.label", "BUTTON LABEL")); p2.add(B2); B3 = new JButton( ); B3.setLabel(I18N.getString(b, "button3.label", "BUTTON LABEL")); p2.add(B3); B4 = new JButton( ); B4.setLabel(I18N.getString(b, "button4.label", "BUTTON LABEL")); p2.add(B4); cp.add(p2); pack( ); } }
Other things may need to be internationalized as well:
These are set separately on Unix/POSIX; on other operating systems,
they depend on the default Locale
.
See GregorianCalendar
and
DateFormat
in Recipe 6.0.
See java.util.NumberFormat
in Recipe 5.7.
These appear in different orders in different languages (something
the C-language printf( )
could never handle). See
java.util.MessageFormat
in Recipe 14.10.
Internationalizing your menus and push buttons is only one step. You also need to internationalize message text in dialogs as well as help files (see the JavaHelp API at http://java.sun.com/products/javahelp/).
Some items such as AWT FileDialog
use native
components; their appearance depends on the native operating system
(your application can change its own default locale, but not the
system’s; therefore, if your customer has a differently
internationalized copy of the same OS, the file dialogs will appear
differently).
A short, readable, non-Java-specific introduction to the overall topic of internationalization is The Guide to Translation and Localization, written by the staff of Lingo Systems and published by the IEEE Computer Society. For more on Java I18N, see the online documentation that ships with the JDK; start at jdk1.x/docs/guide/intl/index.html. See also the O’Reilly book Java Internationalization.
18.118.208.97