[英]JTextPane text background color does not work
我试图用JTextPane
制作一个小的HTML所见即所得,但是我无法让BackgroundAction
起作用。 我使用setCharacterAttributes
在StyledDocument
的的JTextPane
,但似乎有问题。 视图是好的,但Document
不是。
这是一个显示问题的小示例代码。 有2个JTextPane
:
JTextPane
的文本并将其设置在第二个JTextPane
上 ->尽管它们具有相同的文本,但它们不会显示相同的内容。
有没有一种方法可以设置当前所选文本的背景颜色,并使JTextPane
报告更新的HTML文本?
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
public class TestDifferentStyles {
private void initUI() {
JFrame frame = new JFrame(TestDifferentStyles.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JTextPane textPane = new JTextPane();
final JTextPane textPane2 = new JTextPane();
textPane2.setEditable(false);
textPane.setContentType("text/html");
textPane2.setContentType("text/html");
textPane.setText("<html><head></head><body><p>Hello world</p></body></html>");
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.GREEN);
StyleConstants.setBackground(set, Color.BLACK);
((StyledDocument) textPane.getDocument()).setCharacterAttributes(0, textPane.getDocument().getLength(), set, false);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
panel.add(textPane, gbc);
panel.add(textPane2, gbc);
frame.add(panel);
frame.setSize(500, 400);
frame.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
System.err.println(textPane.getText());
textPane2.setText(textPane.getText());
}
});
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestDifferentStyles().initUI();
}
});
}
}
输出结果(每个JTextPane
周围的黑色边框):
这是可以设置背景颜色的Action的代码:
public class BackgroundColorAction extends StyledEditorKit.StyledTextAction {
private Color color;
public BackgroundColorAction(Color color) {
super(StyleConstants.Background.toString());
this.color = color;
}
@Override
public void actionPerformed(ActionEvent ae) {
JEditorPane editor = getEditor(ae);
if (editor == null) {
return;
}
//Add span Tag
String htmlStyle = "background-color:" + Util.getHTMLColor(color);
SimpleAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(HTML.Attribute.STYLE, htmlStyle);
MutableAttributeSet outerAttr = new SimpleAttributeSet();
outerAttr.addAttribute(HTML.Tag.SPAN, attr);
//Next line is just an instruction to editor to change color
StyleConstants.setBackground(outerAttr, this.color);
setCharacterAttributes(editor, outerAttr, false);
}
}
我在设置背景颜色时遇到很多麻烦。 但是最后,我设法破解了。`对不起,我忘记发布子例程了。 干得好:
/**
* Convert a Java Color to equivalent HTML Color.
*
* @param color The Java Color
* @return The String containing HTML Color.
*/
public static String getHTMLColor(Color color) {
if (color == null) {
return "#000000";
}
return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.