1 DocumentEvent事件源
文本区调用getDocument()方法返回所维护的文档,注意这里是文本区所维护的文档能出发DocumentEvent事件。
2 注册监视器
能触发DocumentEvent事件的事件源使用addDocumentListener(DocumentListener listener)将实现DocumentListener接口的类的实例注册为事件源的监视器。
3 DocumentListener接口
该接口中有三个方法:
public void changedUpdate(DocumentEvent e)
public void removeUpdate(DocumentEvent e)
public void insertUpdate(DocumentEvent e)
下面的例子中,将用户在一个文本区输入的单词按字典序排好后放入另一个文本区。
main类
package examplee;
public class example002 {
public static void main(String args[]) {
WindowDocument win = new WindowDocument();
win.setBounds(100,100,590,500);
win.setTitle("DocumentEvent事件的处理--排序单词");
}
}
WindowDocument类
package examplee;
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);
}
}
TextListener类
package examplee;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
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}]+";
//匹配以下字符任意多个(大于等于一个)
//1. 任意空白(空格、换行等)
//2. 任意数字(0-9)
//3. p P u n c t { } (注意:此处的\\p最终代表的只是p)
String words[] = str.split(regex);//split()方法用于把一个字符串分割成字符串数组
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 examplee;
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();
}
}
博客介绍了DocumentEvent事件相关内容。文本区维护的文档可触发该事件,能触发此事件的源通过addDocumentListener方法注册监视器。还介绍了DocumentListener接口的三个方法,并给出将用户在一个文本区输入单词按字典序排好放入另一文本区的示例,涉及main、WindowDocument等类。

667

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



