java--对于字符串的字典排序--GUI
2013-11-11 22:08
609 查看
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 将字符串数组变为字典顺序排序后的字符串数组
- JAVA基础算法——将字符串按照字典倒序排序并输出
- JAVA基础算法——将字符串按照字典倒序排序并输出
- java 给定一个字符串数组。按照字典顺序进行从小到大的排序。
- JAVA---字符串按照字典排序
- JAVA:字符串的字典排序
- java字符串排序
- Java_String_对字符串排序持一种宽容的心态
- java字符串基础小知识,字符串去除空格,排序,反转
- java正则表达式 校验邮件, ip排序, 字符串处理实例
- java字符串替换排序实例
- c++ 从文件中读取字符串 按字典排序 并统计个数
- java中字符串集合排序
- Java的字符串排序
- 题目1135:字符串排序 Java/c++
- 给定一个字符串数组,将其元素按字典序列排序
- java list 字典顺序排序
- Java字符串排序
- Java-字符串-coder的排序
- java中对于复杂对象排序的模型及其实现