⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intlpanels.java

📁 程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件
💻 JAVA
字号:
package examples.i18n;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.text.MessageFormat;


/** An example class used to demonstrate the basics of 
  * internationalizing a program by separating all text
  * from code, by placing strings in a properties file
  * that is accessed through Resource Bundle object.
  */

public class IntlPanels extends JFrame {
   private JPanel upper, middle, lower;
   private JTextField text;
   private JButton black, red, green;
   private JCheckBox disable;
   private ResourceBundle rb;
   /** Class constructor method
     * @param rb The resource bundle for the frame
     */
   public IntlPanels( ResourceBundle inputBundle ) {
      super( );
      try { 
         rb = inputBundle;
         setTitle( rb.getString( "window.title" ) ) ;
         addWindowListener( new WindowAdapter( ) {
            /** End the program when the user
              * closes the window
              */
               public void
               windowClosing( WindowEvent e ) {
                  IntlPanels.this.dispose( );
                  System.exit( 0 );
               }
            }
         );
         upper = new JPanel();
         upper.setBorder(
            BorderFactory.createTitledBorder(
               rb.getString( "panels.upper.title" ) ) );
         upper.setLayout( new BorderLayout() );
         text = new JTextField( 
            rb.getString( "textfield.sample") );
         upper.add( text, BorderLayout.CENTER );
         middle = new JPanel();
         middle.setBorder(
            BorderFactory.createTitledBorder( 
               rb.getString( "panels.middle.title") ) );
         middle.setLayout( new FlowLayout( 
            FlowLayout.CENTER ) );
         black = new JButton(  
            rb.getString( "buttons.black.label"),
            new ColorIcon( Color.black ) );
         black.addActionListener(
            new ButtonListener( Color.black ) );
         middle.add( black );
         red = new JButton(  
            rb.getString( "buttons.red.label"),
            new ColorIcon( Color.red ) );
         red.addActionListener(
            new ButtonListener( Color.red ) );
         middle.add( red );
         green = new JButton(  
            rb.getString( "buttons.green.label"),
            new ColorIcon( Color.green ) );
         green.addActionListener(
            new ButtonListener( Color.green ) );
         middle.add( green );
         lower = new JPanel();
         lower.setLayout( new FlowLayout( 
            FlowLayout.RIGHT ) );
         disable = new JCheckBox(  
            rb.getString( "checkboxes.disable.label" ) );
         disable.addItemListener( new ItemListener() {
            /** Disable and enable the buttons
              */
               public void
               itemStateChanged( ItemEvent e ) {
                  boolean enabled
                     = ( e.getStateChange()
                     == ItemEvent.DESELECTED );
                  black.setEnabled( enabled );
                  red.setEnabled( enabled );
                  green.setEnabled( enabled );
               }
            }
         );
         lower.add( disable );
         Container cp = getContentPane();
         cp.add( upper, BorderLayout.NORTH );
         cp.add( middle, BorderLayout.CENTER );
         cp.add( lower, BorderLayout.SOUTH );
         pack();
         setVisible( true );
      }  catch ( MissingResourceException mrx ) {
         String message;
         try { 
            message = rb.getString (
               "messages.missing_resource" );
         } catch ( MissingResourceException ignore ) {
            message = 
               "Counld not find resource \"{0}\"";
         }
         MessageFormat mf =
            new MessageFormat( message );
         Object[] messageArgs = { mrx.getKey() };
         System.out.println (
            mf.format( messageArgs ) );
         System.exit( 0 );
      }            
   }

   /** The class representing the button event
     * listeners
     */
   class ButtonListener implements ActionListener {
      private Color c;
      /** Class constructor
        * @param c the color for this button
        */
      public ButtonListener( Color c ) {
         this.c = c;
      }
      /** Respond to the action events
        * @param e The click event
        */
      public void actionPerformed( ActionEvent e ) {
         text.setForeground( c );
      }
   }

   /** The class representing the colored icons on
     * the buttons
     */
   class ColorIcon implements Icon {
      private Color c;
      private static final int DIAMETER = 10;
      /** Class constructor
        * @param c the color for this button
        */
      public ColorIcon( Color c ) {
         this.c = c;
      }
      /** Paint the color icon with a black border
        * @param cp the component holding the icon
        * @param g the graphics context for the icon
        * @param x the x draw start position
        * @param y the y draw start position
        */
      public void paintIcon( Component cp, Graphics g,
                             int x, int y ) {
         g.setColor( c );
         g.fillOval( x, y, DIAMETER, DIAMETER );
         g.setColor( Color.black );
         g.drawOval( x, y, DIAMETER, DIAMETER );
      }
      /** Get the icon's height
        * @return the height of the icon
        */
      public int getIconHeight() {
         return DIAMETER;
      }
      /** Get the icon's width
        * @return the width of the icon
        */
      public int getIconWidth() {
         return DIAMETER;
      }
   }
   /** The test method for the class
     * @param args not used
     */
   public static void main( String[] args ) {
      String language, country;
      language = args.length < 1 ? "default" : args[0];
      country = args.length < 2 ? "default" : args[1];
      Locale.setDefault( new Locale( language,
         country ) );
      ResourceBundle rb =
         ResourceBundle.getBundle( "IntlPanels" );
      IntlPanels myWindow = new IntlPanels( rb );     
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -