插入String后重置Document中的属性

时间:2011-05-10 19:23:07

标签: java swing document

我有一个JTextComponent,用户可以通过两种方式输入文本:

  • 他可以直接在其中输入文字。
  • 使用第二个控件,他可以间接地在其中插入文本。这是通过以编程方式调用insertString()。
  • 来完成的

以第二种方式插入的文本中使用的字体将与直接输入的字体不同。键入的文本字体将是JTextComponent的默认字体。

这是代码。 TODO是我不知道该怎么做的。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class ResetAttributesInDocument extends JApplet {

    private ButtonListener bl = new ButtonListener();
    private JTextPane myJTextComponent;

    public void init() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        myJTextComponent = new JTextPane();
        contentPanel.add(myJTextComponent, BorderLayout.CENTER);
        JButton insertTextButton = new JButton("Insert text");
        insertTextButton.addActionListener(bl);
        contentPanel.add(insertTextButton, BorderLayout.SOUTH);
        getContentPane().add(contentPanel);
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            final Document doc = myJTextComponent.getDocument();
            final int caretPosition = myJTextComponent.getCaretPosition();

            SimpleAttributeSet set = new SimpleAttributeSet();
            StyleConstants.setFontFamily(set, "Courier New");
            // Possibly add more attributes to set here.

            try {
                doc.insertString(caretPosition, "text in Courier New", set);
            } catch (BadLocationException e1) {
                e1.printStackTrace();
            }

            // TODO Reset the attributes back to what they originally were so
            // that any new text the user enters after the inserted text is in
            // the original font.
        }
    }
}

有没有办法将属性重置回原来的状态?

1 个答案:

答案 0 :(得分:1)

你的问题没有意义。

首先说明“如果用户键入...”,这意味着用户会做某事。

然后你说你想“做这样的事......”,这意味着你在代码中做了一些不受用户控制的事情,因为你手动调用了insertString()方法。

发布演示此问题的SSCCE并说明复制问题的确切步骤。

  

有没有办法将属性重置回原来的状态?

每次插入符更改位置时都会重置属性,因此您需要使用CaretListener处理此属性。类似的东西:

// <applet code="ResetAttributesInDocument.class" width="400" height="400"></applet>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class ResetAttributesInDocument extends JApplet implements CaretListener
{
    private ButtonListener bl = new ButtonListener();
    private JTextPane myJTextComponent;
    private SimpleAttributeSet set;

    public void init() {
        JPanel contentPanel = new JPanel(new BorderLayout());
        myJTextComponent = new JTextPane();
        myJTextComponent.addCaretListener( this );
        contentPanel.add(myJTextComponent, BorderLayout.CENTER);
        JButton insertTextButton = new JButton("Insert text");
        insertTextButton.addActionListener(bl);
        contentPanel.add(insertTextButton, BorderLayout.SOUTH);
        getContentPane().add(contentPanel);

        set = new SimpleAttributeSet();
        StyleConstants.setFontFamily(set, "Courier New");
        StyleConstants.setForeground(set, Color.GREEN);
    }

    public void caretUpdate(CaretEvent e)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                String styleFamily = StyleConstants.getFontFamily( set );

                AttributeSet attributes = myJTextComponent.getCharacterAttributes();
                String attributeFamily = attributes.getAttribute(StyleConstants.FontFamily).toString();

                if (! styleFamily.equals(attributeFamily))
                {
                    StyledEditorKit k = (StyledEditorKit)myJTextComponent.getEditorKit();
                    k.getInputAttributes().removeAttributes(set);
                }
            }
        });
    }

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            final Document doc = myJTextComponent.getDocument();
            final int caretPosition = myJTextComponent.getCaretPosition();

            try
            {
                doc.insertString(caretPosition, "text in Courier New", set);
            }
            catch (BadLocationException e1) {
                e1.printStackTrace();
            }
        }
    }
}