日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java实用教程——组件及事件处理——DocumentEvent事件

發(fā)布時間:2023/12/4 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实用教程——组件及事件处理——DocumentEvent事件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

DocumentEvent事件源:
文本區(qū)Document的維護(hù)
注冊監(jiān)視器:
使用addDocumentListener(DocumentListener listen)為事件源添加監(jiān)視器
DocumentListener接口:
接口中有三個方法:
public void changUpdate(DocumentEvent e);
public void removeUpdate(DocumentEvent e);
public void insertUpdate(DocumentEvent e);

在下面的例子9 (運(yùn)行效果如圖9.12所示)將用戶排序單詞一個文本區(qū)輸入的單詞按字典序排序后放入另一個文編輯區(qū),實現(xiàn)如下功能:
用戶在窗口(WindowDocument類負(fù)責(zé)創(chuàng)建)中的一個文本區(qū)inputArea內(nèi)編輯單詞,
觸發(fā)DocumentEvent事件,監(jiān)視器textListener(TextListener類負(fù)責(zé)創(chuàng)建)通過處理該事件將該文本區(qū)的單詞排序,并將排序結(jié)果放入另一個文本區(qū)showTextArea 中,
即隨著文本區(qū)inputArea內(nèi)容的變化,另一個文本區(qū)showTextArea不斷地更新排序。

1.用戶選擇名字為“復(fù)制?”copy的菜單項觸發(fā)ActionEvent 事件,監(jiān)視器handie(Handlel istener類負(fù)責(zé)創(chuàng)建)將用戶在showTextArea選中的文本復(fù)制到剪貼板。

2.用戶選擇名字為“剪切(T)”的菜單項觸發(fā)ActionEvent事件,監(jiān)視器hande( HandleListener類負(fù)責(zé)創(chuàng)建)將用戶在showTextArea選中的文本剪切到剪貼板。

3.用戶選擇名字為“粘貼§” 的菜單項的按鈕觸發(fā)ActionEvent事件,監(jiān)視器handle(HandleListener類負(fù)責(zé)創(chuàng)建)將剪貼板的內(nèi)容粘貼到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; //文本輸入?yún)^(qū),文本展示區(qū)JMenuBar menubar;//菜單條JMenu menu;//菜單項JMenuItem itemCopy,itemCut,itemPaste;//菜單 TextListener textChangeListener; //inputText的監(jiān)視器HandleListener handleListener; //itemCopy,itemCut,itemPaste的監(jiān)視器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); //文本區(qū)以單詞為界自動換行Font font = new Font("宋體",Font.PLAIN,25);inputText.setFont(font);showText.setFont(font);menubar=new JMenuBar(); menu=new JMenu("編輯"); itemCopy=new JMenuItem("復(fù)制(C)");itemCut=new JMenuItem("剪切(T)");itemPaste=new JMenuItem("粘貼(P)");itemCopy.setAccelerator(KeyStroke.getKeyStroke('c'));//設(shè)置快捷方式itemCut.setAccelerator(KeyStroke.getKeyStroke('t'));itemPaste.setAccelerator(KeyStroke.getKeyStroke('p'));//設(shè)置快捷方式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();//監(jiān)視器handleListener = new HandleListener(); //監(jiān)視器textChangeListener.setView(this); handleListener.setView(this);Document document =inputText.getDocument();//獲取輸入?yún)^(qū)的文檔,放入Document中document.addDocumentListener(textChangeListener);//向文檔注冊監(jiān)視器itemCopy.addActionListener(handleListener); //向菜單項注冊監(jiān)視器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(); //空格、數(shù)字和符號(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)組成的正則表達(dá)式: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);} }

總結(jié)

以上是生活随笔為你收集整理的java实用教程——组件及事件处理——DocumentEvent事件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。