• DocumentEvent事件的应用


    将用户在一个文本区输入的单词按字典序排好后放入另一个文本区,并添加菜单

     

    example002类

    package example;
    
    public class example002 { 
       public static void main(String[] args) {
          WindowDocument win=new WindowDocument();
           win.setBounds(100,100,590,500);
           win.setTitle("DocumentEvent事件的处理--排序单词");
       }
    }

    WindowDocument类

    package example;
    
    import java.awt.*;
     
    import javax.swing.*;
     
     
    public class WindowDocument extends JFrame {
        JTextArea inputText, showText;
        JMenuBar menubar; //菜单条
        JMenu menu;  //菜单
        JMenuItem itemCopy,itemCut,itemPaste; //菜单选项,复制,剪切,粘贴
        TextListener textChangeListener;//inputText的监视器
        HandleListener handleListener;//itemCopy,itemCut,itemPaste的监视器
        WindowDocument() {
            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);//文本区以单词为界自动换行
            //设置换行方式(如果文本区要换行)。如果设置为 true,
            //则当行的长度大于所分配的宽度时,将在单词边界(空白)处换行。如果设置为 false,
            //则将在字符边界处换行。此属性默认为 false。
            menubar = new JMenuBar();
            menu = new JMenu("编辑");
            itemCopy = new JMenuItem("复制(C)");
            itemCut = new JMenuItem("剪切(T)");
            itemPaste = new JMenuItem("粘贴(P)");
            itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));//设置快捷方式
            itemCut.setAccelerator(KeyStroke.getKeyStroke('t'));//设置快捷方式
            itemPaste.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);
        }
    }

     

    HandleListener类

    package example;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JTextArea;
    
    public class HandleListener implements ActionListener{
    JTextArea inputText,showText;
    public void setInputText(JTextArea text) {
        inputText=text;
    }
    public void setShowText(JTextArea text) {
        showText=text;
    }
        @Override
        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();
        }
    
    }

    TextListener类

    package example;
    
    import java.util.Arrays;
    
    import javax.swing.JTextArea;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    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);//split()方法用于把一个字符串分割成字符串数组
        Arrays.parallelSort(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);
    }
    }
    天晴了,起飞吧
  • 相关阅读:
    正则表达式的先行断言(lookahead)和后行断言(lookbehind)
    正则表达式里的捕获组
    JavaScript高级程序设计8.pdf
    JavaScript高级程序设计7.pdf
    JavaScript高级程序设计6.pdf
    JavaScript高级程序设计5.pdf
    JavaScript高级程序设计4.pdf
    产品思维|腾讯工作心得:原型该画到什么程度?
    提名推荐!15个2019年最佳CSS框架
    腾讯工作心得:原型该画到什么程度?
  • 原文地址:https://www.cnblogs.com/jianqiao123/p/10781297.html
Copyright © 2020-2023  润新知