O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required