我注意到当HTML JEditorPane上有一个空行时,之前设置的所有样式都会消失。例如,请参阅下面的代码示例:
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;
public class BlankLineTester {
private JEditorPane jep;
public BlankLineTester() {
String html = "<html><head></head><body>" +
"<p><b>Line 1</b></p>" +
"<p><b></b></p>" +
"<p><b>Line 3</b></p>" +
"</body></html>";
jep = new JEditorPane();
jep.setContentType("text/html");
jep.setText(html);
JFrame frame = new JFrame("Blank Line Test");
frame.getContentPane().add(jep);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("Line 3 is " + isInputAttributeBold());
jep.getCaret().setDot(8);
System.out.println("Line 2 is " + isInputAttributeBold());
}
private boolean isInputAttributeBold() {
AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
return StyleConstants.isBold(attSet);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BlankLineTester();
}
});
}
}
第2行最初设置为粗体空行,但在解析后,似乎没有保留粗体属性。另请注意,如果您自己运行,将光标放在第3行,并删除该行上的所有内容,您键入的下一个字符将不是粗体。我想,HTMLDocument树中的叶元素在它们代表的文本消失时就被删除了,但是当用户运行它时,这看起来就像是错误的行为。
有没有人有任何想法如何让样式属性解析为空行,并在样式行上的所有内容都被删除时保留?
谢谢! --Andy
答案 0 :(得分:3)
不是按照自己的方式写作,如果你这样写,那么我想你会得到你的行为,好像是 b&amp; / b ,标签是旧式的,虽然我使用的是JRE 1.7 update 3,但以下几行对此很有用:
String html = "<html><head></head><body>" +
"<p style = \"font-weight:bold\">Line 1</p>" +
"<p style = \"font-weight:bold\"></p>" +
"<p style = \"font-weight:bold\">Line 3</p>" +
"</body></html>";
尝试此代码,在运行程序时,尝试通过将光标移至开头按删除,然后它将保留颜色绿色,因为这是颜色对于 鼠标指针 触及的最后一个字符,然后尝试按 Backspace 将光标移到最后,然后它将保留Color BLUE ,因为这是 鼠标指针 触及的最后一个字符的颜色。第三次,只是尝试在两条已经提供的行之间的每一行中写一个单词,有一行将显示红色的字符,试图找到该行。我附上下面的图片是为了澄清红线情景。
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;
public class BlankLineTester {
private JEditorPane jep;
public BlankLineTester() {
String html = "<html><head></head><body>" +
"<p style = \"font-weight:bold; color: blue\">Line 1</p><br />" +
"<p style = \"font-weight:bold; color: red\"></p><br />" +
"<p style = \"font-weight:bold; color: green\">Line 3</p>" +
"</body></html>";
jep = new JEditorPane();
jep.setContentType("text/html");
jep.setText(html);
JFrame frame = new JFrame("Blank Line Test");
frame.getContentPane().add(jep);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("Line 3 is " + isInputAttributeBold());
jep.getCaret().setDot(8);
System.out.println("Line 2 is " + isInputAttributeBold());
}
private boolean isInputAttributeBold() {
AttributeSet attSet = ((HTMLEditorKit)jep.getEditorKit()).getInputAttributes();
return StyleConstants.isBold(attSet);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new BlankLineTester();
}
});
}
}