📄 tokentest.java
字号:
//TokenTest.java
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;
public TokenTest(){
super( "测试StringTokenizer类" );
Container container = getContentPane();
container.setLayout( new FlowLayout() );
promptLabel = new JLabel( "输入一个句子,然后按回车" );
container.add( promptLabel );
inputField = new JTextField( 20 );
inputField.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent event ){
//调用StringTokenizer构造函数以JTextField字段中的字符串
//(event.getActionCommand())建立对象tokens
StringTokenizer tokens =
new StringTokenizer( event.getActionCommand() );
//调用StringTokenizer类的方法countTokens
outputArea.setText( "Number of elements: " +
tokens.countTokens() + "\nThe tokens are:\n" );
//调用StringTokenizer类的方法hasMoreTokens方法
while ( tokens.hasMoreTokens() )
//调用StringTokenizer类的方法nextToken方法
outputArea.append( tokens.nextToken() + "\n" );
}
}
);
container.add( inputField );
outputArea = new JTextArea( 10, 20 );
outputArea.setEditable( false );
container.add( new JScrollPane( outputArea ) );
setSize( 275, 240 );
setVisible( true );
}
public static void main( String args[] ){
TokenTest application = new TokenTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -