// Fig. 11.18: TokenTest.java // StringTokenizer class. import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TokenTest extends JFrame { private JLabel promptLabel; private JTextField inputField; private JTextArea outputArea; // set up GUI and event handling public TokenTest() { super( "Testing Class StringTokenizer" ); Container container = getContentPane(); container.setLayout( new FlowLayout() ); promptLabel = new JLabel( "Enter a sentence and press Enter" ); container.add( promptLabel ); inputField = new JTextField( 20 ); inputField.addActionListener( new ActionListener() { // anonymous inner class // handle text field event public void actionPerformed( ActionEvent event ) { StringTokenizer tokens = new StringTokenizer( event.getActionCommand() ); outputArea.setText( "Number of elements: " + tokens.countTokens() + "\nThe tokens are:\n" ); while ( tokens.hasMoreTokens() ) outputArea.append( tokens.nextToken() + "\n" ); } } // end anonymous inner class ); // end call to addActionListener container.add( inputField ); outputArea = new JTextArea( 10, 20 ); outputArea.setEditable( false ); container.add( new JScrollPane( outputArea ) ); setSize( 275, 240 ); // set the window size setVisible( true ); // show the window } // execute application public static void main( String args[] ) { TokenTest application = new TokenTest(); application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // end class TokenTest /************************************************************************** * (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. * *************************************************************************/