问题是 getInputAttribute 反映了当前插入符号位置的字符属性。
- 您选择“234”(插入符号在 4 之后)--> InputAttributes 反映“234”的那些
- 您在“234”和当前 InputAttributes (应该是相同的)上应用红色样式前景
- 在 "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();
}
});
}
}