Program: MenuIntl

MenuIntl (shown in Example 14-1) is a complete version of the menu code presented in Section 14.4.

Example 14-1. MenuIntl.java

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; /** This is a partly-internationalized version of MenuDemo. * To try it out, use * java MenuIntl * java -Duser.language=es MenuIntl */ public class MenuIntl extends JFrame { /** "main program" method - construct and show */ public static void main(String[] av) { // create an MenuIntl object, tell it to show up new MenuIntl( ).setVisible(true); } /** Construct the object including its GUI */ public MenuIntl( ) { super("MenuIntlTest"); JMenuItem mi; // used in various spots Container cp = getContentPane( ); cp.setLayout(new FlowLayout( )); JLabel lab; cp.add(lab = new JLabel( )); addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { setVisible(false); dispose( ); System.exit(0); } }); JMenuBar mb = new JMenuBar( ); setJMenuBar(mb); ResourceBundle b = ResourceBundle.getBundle("Menus"); String titlebar; try { titlebar = b.getString("program"+".title"); } catch (MissingResourceException e) { titlebar="MenuIntl Demo"; } setTitle(titlebar); String message; try { message = b.getString("program"+".message"); } catch (MissingResourceException e) { message="Welcome to the world of Java"; } lab.setText(message); JMenu fm = mkMenu(b, "file"); fm.add(mi = mkMenuItem(b, "file", "open")); // In finished code there would be a call to // ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.