DocumentEvent事件源:
文本区Document的维护
注册监视器:
使用addDocumentListener(DocumentListener listen)为事件源添加监视器
DocumentListener接口:
接口中有三个方法:
public void changUpdate(DocumentEvent e);
public void removeUpdate(DocumentEvent e);
public void insertUpdate(DocumentEvent e);
在下面的例子9 (运行效果如图9.12所示)将用户排序单词一个文本区输入的单词按字典序排序后放入另一个文编辑区,实现如下功能:
用户在窗口(WindowDocument类负责创建)中的一个文本区inputArea内编辑单词,
触发DocumentEvent事件,监视器textListener(TextListener类负责创建)通过处理该事件将该文本区的单词排序,并将排序结果放入另一个文本区showTextArea 中,
即随着文本区inputArea内容的变化,另一个文本区showTextArea不断地更新排序。
1.用户选择名字为“复制©”copy的菜单项触发ActionEvent 事件,监视器handie(Handlel istener类负责创建)将用户在showTextArea选中的文本复制到剪贴板。
2.用户选择名字为“剪切(T)”的菜单项触发ActionEvent事件,监视器hande( HandleListener类负责创建)将用户在showTextArea选中的文本剪切到剪贴板。
3.用户选择名字为“粘贴§” 的菜单项的按钮触发ActionEvent事件,监视器handle(HandleListener类负责创建)将剪贴板的内容粘贴到inputArea,
public class Example9_9 {
public static void main(String args[]) {
WindowDocument win=new WindowDocument();
win.setBounds(100,100,890,400);
win.setTitle("排序单词");
}
}
;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;
public class WindowDocument extends JFrame {
/**
*
*/
private static final long serialVersionUID = -4158929136133563216L;
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(10,28);
showText = new JTextArea(10,28);
showText.setLineWrap(true); //文本自动回行
showText.setWrapStyleWord(true); //文本区以单词为界自动换行
Font font = new Font("宋体",Font.PLAIN,25);
inputText.setFont(font);
showText.setFont(font);
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.setView(this);
handleListener.setView(this);
Document document =inputText.getDocument();//获取输入区的文档,放入Document中
document.addDocumentListener(textChangeListener);//向文档注册监视器
itemCopy.addActionListener(handleListener); //向菜单项注册监视器
itemCut.addActionListener(handleListener);
itemPaste.addActionListener(handleListener);
}
}
import java.awt.event.*;
public class HandleListener implements ActionListener {
WindowDocument view;
public void setView(WindowDocument view) {
this.view = view;
}
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();
if(str.equals("copy"))
view.showText.copy();
else if(str.equals("cut"))
view.showText.cut();
else if(str.equals("paste"))
view.showText.paste();
}
}
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
import java.util.Arrays;
public class TextListener implements DocumentListener {
WindowDocument view;
public void setView(WindowDocument view) {
this.view = view;
}
public void changedUpdate(DocumentEvent e) {
String str=view.inputText.getText();
//空格、数字和符号(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)组成的正则表达式:
String regex="[\\s\\d\\p{Punct}]+";
String words[]=str.split(regex);
Arrays.sort(words); //按字典序从小到大排序
view.showText.setText(null);
for(int i=0;i<words.length;i++)
view.showText.append(words[i]+",");
}
public void removeUpdate(DocumentEvent e) {
changedUpdate(e);
}
public void insertUpdate(DocumentEvent e) {
changedUpdate(e);
}
}

222



被折叠的 条评论
为什么被折叠?



