我可以使用以下代码创建新行
StringBuilder sb = new StringBuilder();
sb.append("<span style=\"color:black\">--------------</span> <br>");
sb.append("<span style=\"color:red\">Error." + e.toString() + "</span> <br>");
sshoutput.setContentType("text/html");
sshoutput.setText(sb.toString());但是,当我用另一个文本再做一次时,它只显示第二个文本,而不是在这个之后。
“+ e.toString()
对不起,我的英语不太好。我希望你能理解。
发布于 2013-03-09 16:02:46
我现在正在与JTextPane合作,我所做的就是:
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();因此,我可以在任何地方插入字符串,使用:
doc.insertString(STRING POSITION, STRING, null);我对这种方法没有例外。还有一种简单的方法可以使用:
SimpleAttributeSet set = new SimpleAttributeSet();
//Here you modify set. Set is collection of
//various style instructions
//(letters color, bolded, italic, background color etc.)
//You modify set using StyleConstants class.
doc.setCharacterAttributes(START, LENGTH, set, true);编辑:一个示例,它创建文本窗格并在其中写入样式为“Hello”:
JTextPane pane = new JTextPane();
StyledDocument doc = pane.getStyledDocument();
doc.insertString(0, "Hello", null);
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstans.setForeground(set, Color.RED);
doc.setCharacterAttributes(0, 5, set, true);
doc.insertString(5, "World!", null);
SimpleAttributeSet set = new SimpleAttributeSet();
StyleConstans.setForeground(set, Color.BLUE);
doc.setCharacterAttributes(5, 6, set, true);将其添加到带有JPanel (1,1)的GridLayout中,您将看到文本窗格中有红色字符串"Hello“和蓝色字符串"World”。
https://stackoverflow.com/questions/15312521
复制相似问题