该程序可以实现:输入一串句子,将句子分解为单个的语言单元

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("Testing Class 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 tokens=new StringTokenizer(event.getActionCommand());
						outputArea.setText("Number of elements: "+tokens.countTokens()+"\nThe tokens are:\n");
						while(tokens.hasMoreTokens())
							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);
	}
}

StringTokenizer类 实用小程序

相关文章:

  • 2022-12-23
  • 2021-11-09
  • 2021-12-12
  • 2022-12-23
  • 2021-06-11
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-24
  • 2021-11-21
  • 2022-02-07
相关资源
相似解决方案