// Fig. 11.15: StaticCharMethods2.java
// Static Character conversion methods.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StaticCharMethods2 extends JFrame {
   private char c;
   private int digit, radix;
   private JLabel prompt1, prompt2;
   private JTextField input, radixField;
   private JButton toChar, toInt;

   // constructor builds GUI
   public StaticCharMethods2()
   {
      super( "Character Conversion Methods" );

      Container container = getContentPane();
      container.setLayout( new FlowLayout() );

      prompt1 = new JLabel( "Enter a digit or character " );
      input = new JTextField( 5 );
      container.add( prompt1 );
      container.add( input );

      prompt2 = new JLabel( "Enter a radix " );
      radixField = new JTextField( 5 );
      container.add( prompt2 );
      container.add( radixField );

      toChar = new JButton( "Convert digit to character" );
      toChar.addActionListener(

         new ActionListener() { // anonymous inner class

            // handle toChar JButton event
            public void actionPerformed( ActionEvent actionEvent )
            {  
               digit = Integer.parseInt( input.getText() );
               radix = Integer.parseInt( radixField.getText() );
               JOptionPane.showMessageDialog( null,
                  "Convert digit to character: " +
                  Character.forDigit( digit, radix ) );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      toInt = new JButton( "Convert character to digit" );
      toInt.addActionListener(

         new ActionListener() {  // anonymous inner class

            // handle toInt JButton event
            public void actionPerformed( ActionEvent actionEvent )
            {  
               String s = input.getText();
               c = s.charAt( 0 );
               radix = Integer.parseInt( radixField.getText() );
               JOptionPane.showMessageDialog( null,
                  "Convert character to digit: " +
                  Character.digit( c, radix ) );
            }

         } // end anonymous inner class

      ); // end call to addActionListener

      container.add( toChar );
      container.add( toInt );
      setSize( 275, 150 );  // set the window size
      setVisible( true );   // show the window
   }

   // create StaticCharMethods2 object execute application
   public static void main( String args[] )
   {
      StaticCharMethods2 application = new StaticCharMethods2();
      application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   }

} // end class StaticCharMethods2

/**************************************************************************
 * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and               *
 * Prentice Hall. All Rights Reserved.                                    *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 *************************************************************************/
