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("排序单词");
	}

}

运行结果:



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值