文本组件的特性

一、概述

    JTextComponent是Swing中所有文本组件的超类。它具有以下几个特性:

    1、模型,即Document,管理组件内容;

    2、视图

    3、控制器,即Editor kit,读或写文本,并提供一些文本编辑的action

    4、支持有限的撤销和重复操作

    5、光标,光标过滤器和光标导航。

二、关联text action和菜单或按钮

    menu.add(getActionByName(DefaultEditorKit.cutAction) //通过文本组件的getActions方法可以获得该组件支持的全部action,并将这些action导入到HashMap中去,以便访问。

   action名是从DefaultEditorKit获取的,该类提供了实现基本文本编辑的action,并且是所有的EditorKit的基类。

   基于性能方面的考虑,按上述方法得到的action是被程序中文本组件所共享,但有时你并不想这样,可以通过自己实例化一个action实例:

   Action action = new StyledEditorKit.BoldAction();
   action.putValue(Action.NAME, "Bold");
   menu.add(action);

三、关联键盘和text action

    InputMap inputMap = textPane.getInputMap();
    KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_B,Event.CTRL_MASK);
    inputMap.put(key, DefaultEditorKit.backwardAction);

四、实现撤销和重复

    分两步走:

    (1)记录可撤销的操作,使用UndoManager:protected UndoManager undo = new UndoManager();

     在文本组件的document上注册UndoableEditListener,当有操作发生时就会通知该监听器:

    doc.addUndoableEditListener(new MyUndoableEditListener());
    protected class MyUndoableEditListener
              implements UndoableEditListener {
        public void undoableEditHappened(UndoableEditEvent e) {
            //Remember the edit and update the menus
            undo.addEdit(e.getEdit());
            undoAction.updateUndoState();
            redoAction.updateRedoState();
        }
    } 

    (2)实现undo和redo action,并添加到菜单

    JMenu menu = new JMenu("Edit");
   //Undo and redo are actions of our own creation
   undoAction = new UndoAction();
   menu.add(undoAction);
   redoAction = new RedoAction();
   menu.add(redoAction);

    UndoAction 和RedoAction都继承自AbstractAction ,具体实现大概如下:

    public void actionPerformed(ActionEvent e) {
    try {
        undo.undo();
    } catch (CannotUndoException ex) {
        System.out.println("Unable to undo: " + ex);
        ex.printStackTrace();
    }
    updateUndoState();
    redoAction.updateRedoState();
   }

五、Document

   Document将文本组件的内容和表现相分离,该对象至少提供以下服务:1、包含文本组件的内容;2、通过remove 和insertString提供对文本的编辑;3、将

文本的变化通知给document listeners和undoable edit listeners;4、管理位置对象;5、从文本获取信息,比如文本长度等

   有以下几种document:PlainDocument  DefaultSyledDocument  HTMLDocument

六、Document过滤器

   StyledDocument styledDoc = textPane.getStyledDocument();
   if (styledDoc instanceof AbstractDocument) {
    doc = (AbstractDocument)styledDoc;
    doc.setDocumentFilter(new DocumentSizeFilter(MAX_CHARACTERS));
}

七、监听Document的变化

  doc.addDocumentListener(new MyDocumentListener());

  切记,千万不要在DocumentListener中试图改变文本内容,这样会导致死锁,如想改变文本内容,可以使用过滤器。

八、监听光标或选择变化

   通过注册光标监听器,可以实时显示光标的位置或选择的范围。textPane.addCaretListener(caretListenerLabel);

  当光标发生移动或选择变化,会调用监听器中的caretUpdate方法。

  注意该监听器仅能反映光标或选择返回的相关变化或信息,但不能改变光标或选择,如想改变可以使用NavigationFilter。

九、EditorKit

   文本组件就是通过EditorKit将不同组件的不同部分整合在一起。其提供了view factory,actions,document,caret。它可以对document

进行读和写。有三种EditorKit:

     (1)DefaultEditorKit:对plain document进行读写,提供基本的编辑命令集,是其他EditorKit的超类

     (2)StyledEditorKit:对带格式文本进行读写,为格式文本提供了一组action,默认由JTextPane使用

     (3)HTMLEditorKit:对HTML进行读写编辑是StyledEditorKit的子类。

     以上的三个Editorkit均被注册到JEditorPane中,并与所编辑的文本格式相关联。当读入一个文件时,会根据文件的格式选择相应的Editorkit。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值