2

我使用了一个 JButton,当单击该按钮时,运行这些代码:

StyledDocument doc = (StyledDocument) textPane.getDocument(); 
MutableAttributeSet attr = new SimpleAttributeSet(); 
int p0=textPane.getSelectionStart();
int p1=textPane.getSelectionEnd();
if(p0!=p1){
   StyleConstants.setForeground(attr, Color.RED); 
   doc.setCharacterAttributes(p0,p1-p0, attr,false);
}
textPane.getInputAttributes().addAttributes(attr);

从 p0 到 p1 的字符确实变成了红色

但是我输入的字符仍然是黑色的,而不是红色的:(

例子:

我输入“12345”并选择“234”。

新的5仍然是黑色的。

在此处输入图像描述

但是如果我输入“1234”并选择“234”。

新的 5 将是红色的。

即使没有“textPane.getInputAttributes().addAttributes(attr);”

在此处输入图像描述

4

1 回答 1

5

问题是 getInputAttribute 反映了当前插入符号位置的字符属性。

  1. 您选择“234”(插入符号在 4 之后)--> InputAttributes 反映“234”的那些
  2. 您在“234”和当前 InputAttributes (应该是相同的)上应用红色样式前景
  3. 在 "5"-->InputAttributes 更新并反映 "5" 之后移动插入符号

现在,这里有一些代码可以让自己受到启发(这不是真正可操作的),您可能必须听取插入符号的移动(如果不知道您要实现什么就很难说出来):

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class TestTextPane {

    protected void initUI() {
        JFrame frame = new JFrame(TestTextPane.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTextPane textPane2 = new JTextPane();
        textPane2.setText("12345");
        frame.add(textPane2);
        JButton button = new JButton("Make it red");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final SimpleAttributeSet set = new SimpleAttributeSet();
                StyleConstants.setForeground(set, Color.RED);
                int p0 = textPane2.getSelectionStart();
                int p1 = textPane2.getSelectionEnd();
                if (p0 != p1) {
                    StyledDocument doc = textPane2.getStyledDocument();
                    doc.setCharacterAttributes(p0, p1 - p0, set, false);
                }
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        textPane2.getCaret().setDot(textPane2.getText().length());
                        MutableAttributeSet inputAttributes = textPane2.getInputAttributes();
                        inputAttributes.addAttributes(set);
                    }
                });

            }
        });
        button.setFocusable(false);
        frame.add(button, BorderLayout.SOUTH);
        frame.setSize(600, 400);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestTextPane t = new TestTextPane();
                t.initUI();
            }
        });
    }

}
于 2012-11-27T15:40:55.090 回答