Chapter 10 (Web hosting company) Strings and Characters 577 17 //

Chapter 10 Strings and Characters 577 17 // set up GUI and event handling 18 public TokenTest() 19 { 20 super( “Testing Class StringTokenizer” ); 21 22 Container container = getContentPane(); 23 container.setLayout( new FlowLayout() ); 24 25 promptLabel = 26 new JLabel( “Enter a sentence and press Enter” ); 27 container.add( promptLabel ); 28 29 inputField = new JTextField( 20 ); 30 31 inputField.addActionListener( 32 33 // anonymous inner class 34 new ActionListener() { 35 36 // handle text field event 37 public void actionPerformed( ActionEvent event ) 38 { 39 String stringToTokenize = 40 event.getActionCommand(); 41 StringTokenizer tokens = 42 new StringTokenizer( stringToTokenize ); 43 44 outputArea.setText( “Number of elements: ” + 45 tokens.countTokens() + “nThe tokens are:n” ); 46 47 while ( tokens.hasMoreTokens() ) 48 outputArea.append( tokens.nextToken() + “n” ); 49 } 50 51 } // end anonymous inner class 52 53 ); // end call to addActionListener 54 55 container.add( inputField ); 56 57 outputArea = new JTextArea( 10, 20 ); 58 outputArea.setEditable( false ); 59 container.add( new JScrollPane( outputArea ) ); 60 61 setSize( 275, 260 ); // set the window size 62 show(); // show the window 63 } 64 65 // execute application 66 public static void main( String args[] ) 67 { 68 TokenTest application = new TokenTest(); 69 Fig. 10.20 Tokenizing strings with a StringTokenizerobject (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply