插入字符串后重置文档中的属性

发布于 2024-11-06 10:20:27 字数 2089 浏览 3 评论 0原文

我有一个 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.
        }
    }
}

有没有办法把属性恢复到原来的样子?

I have a JTextComponent in which a user can enter text in two ways:

  • He can type text directly into it.
  • Using a second control, he can indirectly insert text into it. This is done by programmatically calling insertString().

The font used in the text inserted in the second way will be different than the font that is typed in directly. The font of the text typed in will be the default font of the JTextComponent.

Here is the code. The TODO is what I don't know how to do.

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.
        }
    }
}

Is there a way to reset the attributes back to what they originally were?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏の忆 2024-11-13 10:20:27

你的问题没有意义。

首先,您声明“如果用户输入...”,这意味着用户做了某事。

然后你说你想要“做这样的事情......”,这意味着你正在代码中做一些不受用户控制的事情,因为你正在手动调用 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();
            }
        }
    }
}

Your question doesn't make sense.

First you state "if the user types into ..." which implies the user does something.

Then you say you want "to do something like this...", which implies you are doing something in the code which is not under user control because you are manually invoking the insertString() method.

Post your SSCCE that demonstrates the problem and describe the exact steps to duplicate the problem.

Is there a way to reset the attributes back to what they originally were?

Attributes are reset every time the caret changes position so you need to handle this with a CaretListener. Something like:

// <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();
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文