⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 documentlistenerdemo1.java

📁 这是一个见听程序4
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;                     //接口Document在swing.text类包中import javax.swing.event.*;//使用implements命令引入文档接收器public class DocumentListenerDemo1 extends JFrame implements DocumentListener {  JPanel contentPane;                          //声明容器类,用以获取窗口底层容器  JTextArea Document = new JTextArea();        //声明文档编辑器  JTextArea displayArea = new JTextArea();     //声明信息显示框  //声明标签,转行变量及按钮  JLabel jLabel1 = new JLabel();  JLabel jLabel2 = new JLabel();  String newline="\n";  JButton jButton1 = new JButton();  //创建DocumentListenerDemo1类,并显示该类  public static void  main(String[] args){    DocumentListenerDemo1 DLFrame=new DocumentListenerDemo1();    DLFrame.setVisible(true);  }  //DocumentListenerDemo1类创造器  public DocumentListenerDemo1() {      jbInit();  }  //类创造器的主要方法  private void jbInit(){    //设置窗口的布局,标题及大小    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(null);    this.setSize(new Dimension(438, 400));    this.setTitle("文档接收器实际演示");    //设置文本编辑器的位置,大小及加入文本接收器    Document.setBounds(new Rectangle(7, 37, 196, 253));    Document.getDocument().addDocumentListener(this);    //使信息显示框不可编辑与设置该显示框的位置,大小    displayArea.setEditable(false);    displayArea.setBounds(new Rectangle(208, 37, 213, 253));    //设置标签的标题,位置及大小    jLabel1.setText("文本编写框");    jLabel1.setBounds(new Rectangle(9, 17, 164, 19));    jLabel2.setToolTipText("");    jLabel2.setText("信息显示框");    jLabel2.setBounds(new Rectangle(226, 15, 114, 19));    //设置按钮的位置,大小及加入动作接收器    jButton1.setBounds(new Rectangle(207, 305, 214, 32));    jButton1.setText("清空内容");    jButton1.addActionListener(new java.awt.event.ActionListener() {      public void actionPerformed(ActionEvent e) {        displayArea.setText("");      }    });    //向底层容器加入各种控件    contentPane.add(Document, null);    contentPane.add(displayArea, null);    contentPane.add(jLabel1, null);    contentPane.add(jLabel2, null);    contentPane.add(jButton1, null);  }  //窗户关闭的处理事件  protected void processWindowEvent(WindowEvent e) {    if (e.getID() == WindowEvent.WINDOW_CLOSING) {      System.exit(0);    }  }  //当向文本编辑器插入字符时,该方法激活  public void insertUpdate(DocumentEvent e) {      updateLog(e, "插入");  }  //当文本编辑器中的字被删除时,该方法被激活  public void removeUpdate(DocumentEvent e) {      updateLog(e, "删除");  }  //当字符的风格改变时执行该事件,在没有风格设置的文本编辑器中该事件不会被激活  public void changedUpdate(DocumentEvent e) {      updateLog(e,"改变");  }  //显示信息的方法  public void updateLog(DocumentEvent e, String action) {      Document doc=(Document)e.getDocument();      //从DocumnetEven类中取得文档类      int changeLength = e.getLength();            //输入或者删除字符的个数      displayArea.append(          "  "+changeLength + " 字符" +((changeLength == 1) ? " " : "串 ")          +action+ "." + newline          //doc.getLength()指文本框的总字符个数          +"  字符总长度 = " + doc.getLength() + newline          //显示被改变字符串的前一个不被改变的字符的位置          +"  前一个没有改变字符的位置="+e.getOffset() +"  "+newline          //显示事件的类型          +"  事件="+e.getType()+newline          );  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -