问题描述
我正在尝试使用 JTextPane
制作一个小的HTML-wysiwyg,但我无法获得 BackgroundAction
上班。我在 JTextPane
的 StyledDocument
上使用 setCharacterAttributes
但是这似乎有问题。视图没问题,但文档
不是。
I am trying to make a small HTML-wysiwyg with a JTextPane
but I can't get the BackgroundAction
to work. I am using setCharacterAttributes
on the StyledDocument
of the JTextPane
but it seems problematic. The view is ok but the Document
is not.
这是一个显示问题的小型演示代码。有2 JTextPane
:
Here is a small demo code showing the problem. There are 2 JTextPane
:
- 我在文本中设置了背景颜色第一个
- 我检索第一个
JTextPane
的文本并将其设置在第二个
- I set the background color of my text in the first one
- I retrieve the text of the first
JTextPane
and set it on the second one
- >虽然它们具有相同的文字但它们不会显示相同的内容。
--> They don't show the same thing although they have the same text.
有没有办法在当前选定的文本上设置背景颜色并让 JTextPane
报告更新的HTML文本?
Is there a way to set the background color on the current selected text and have the JTextPane
report an updated HTML-text?
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
):
The output result (the black border are around each JTextPane
):
推荐答案
以下是可以设置背景颜色的Action的代码:
Here is the code for an Action that can set the background color:
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);
}
}
我在设置背景颜色方面遇到了很多麻烦。但最后,我设法破解了它。
对不起,我忘了发布子程序。在这里:
I had lot of trouble setting background color. But finally, I have managed to crack it.`Sorry I forgot to post the subroutine. Here you go:
/**
* 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();
}
这篇关于JTextPane文本背景颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!