java--对于字符串的字典排序--GUI

package ytu.jsj.com;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class WindowsDocument extends JFrame{
	JTextArea inputText,showText;
	JMenuBar menubar;
	JMenu menu;
	JMenuItem itemCopy,itemCut,itemPaste;
	TextListener textChangeListener;		//input的监视器
	HandleListener handleListener;		//itemCopy,itemCut,itemPaste的监视器
	WindowsDocument(){
		init();
		setLayout(new FlowLayout());
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	void init(){
		inputText=new JTextArea(15,20);
		showText=new JTextArea(15,20);
		showText.setLineWrap(true);		//	文本自动回行
		showText.setWrapStyleWord(true);	//文本区以单词为界,自动换行
		menubar=new JMenuBar();
		menu=new JMenu("编辑");
		itemCopy=new JMenuItem("复制(C)");
		itemCut=new JMenuItem("剪切(T)");
		itemPaste=new JMenuItem("粘贴(P)");
		//设置快捷方式
		itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));
		itemCopy.setAccelerator(KeyStroke.getKeyStroke('t'));
		itemCopy.setAccelerator(KeyStroke.getKeyStroke('p'));
		itemCopy.setActionCommand("copy");
		itemCut.setActionCommand("cut");
		itemPaste.setActionCommand("paste");
		//往面板上添加
		menu.add(itemCopy);
		menu.add(itemCut);
		menu.add(itemPaste);
		menubar.add(menu);
		setJMenuBar(menubar);
		add(new JScrollPane(inputText));
		add(new JScrollPane(showText));
		
		textChangeListener=new TextListener();
		handleListener=new HandleListener();
		textChangeListener.setInputText(inputText);
		textChangeListener.setShowText(showText);
		handleListener.setInputText(inputText);
		handleListener.setShowText(showText);
		//向文档注册监视器
		(inputText.getDocument()).addDocumentListener(textChangeListener);
		//向菜单项注册监视器
		itemCopy.addActionListener(handleListener);
		itemCut.addActionListener(handleListener);
		itemPaste.addActionListener(handleListener);
	}
	
}



TextListener监视类


package ytu.jsj.com;

import java.awt.event.*;
import java.io.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.*;

public class TextListener implements DocumentListener{
	JTextArea inputText,showText;
	public void setInputText(JTextArea text){
		inputText=text;
	}
	public void setShowText(JTextArea text){
		showText=text;
	}
	public void changedUpdate(DocumentEvent e){
		String str=inputText.getText();
		//空格、数字和符号组成的正则表达式
		String regex="[\\s\\d\\p{Punct}]+";
		String[] words=str.split(regex);
		//按照字典的顺序排序
		Arrays.sort(words);
		showText.setText(null);
		for(int i=0;i<words.length;i++)
			showText.append(words[i]+",");
	}
	public void removeUpdate(DocumentEvent e){
		changedUpdate(e);
	}
	public void insertUpdate(DocumentEvent e){
		changedUpdate(e);
	}

}

HandleListener类

package ytu.jsj.com;

import java.awt.event.*;
import javax.swing.*;

public class HandleListener implements ActionListener{
	JTextArea inputText,showText;
	public void setInputText(JTextArea text){
		inputText=text;
	}
	public void setShowText(JTextArea text){
		showText=text;
	}
	public void actionPerformed(ActionEvent e){
		String str=e.getActionCommand();
		if(str.equals("copy"))
			showText.copy();
		else if(str.equals("cut"))
			showText.cut();
		else if(str.equals("paste"))
			inputText.paste();
	}

}


测试类:

package ytu.jsj.com;

public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		WindowsDocument win=new WindowsDocument();
		win.setBounds(100,100,580,500);
		win.setTitle("排序单词");
	}

}

运行结果:

java--对于字符串的字典排序--GUI_第1张图片


你可能感兴趣的:(java--对于字符串的字典排序--GUI)