我正在使用字符串构建器将文本附加到我的JTextPane,我已将内容类型设置为pane.setContentType("text/html");但我的JTextPane上实际上没有显示文本.
这是我追加的一个例子:
buildSomething.append("<b style=\"color:pink\">"+Birthday+"</span>");
Run Code Online (Sandbox Code Playgroud)
有什么我做错了吗?我该如何解决这个问题呢?
我正在制作类似于聊天的应用程序.为此,我有两个JTextPanes,一个是我正在编写的,一个是显示消息的.这是处理从输入到显示的文本传输的代码:
String input = textPane.getText();
if(!input.endsWith("\n")){
input+="\n";
}
StyledDocument doc = displayPane.getStyledDocument();
int offset = displayPane.getCaretPosition();
textPane.setText("");
try {
doc.insertString(offset, input, set);
} catch (BadLocationException ex) {
Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
}
Run Code Online (Sandbox Code Playgroud)
问题是,如果我在输入文本的某些单词上有颜色,则输出都是彩色的.因此,当移动到显示时(在输入时正确显示),颜色将应用于所有文本.关于如何正确移动文本的任何建议?
请注意,其他格式与粗体,斜体等相同
我正在实施一个拼写检查器,并想知道是否有一种简单/明显的方法可以用不同的颜色(如Red)强调某些文本.
我已经设置了所有内容并使用以下代码加下划线(也设置了文本的颜色):
private AttributeSet getAttributeSet(Color foregroundColor) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
StyleConstants.setForeground(attrs, foregroundColor);
StyleConstants.setUnderline(attrs, true);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码将单词设置为蓝色,但也用蓝色加下划线.我需要能够改变下划线和厚度.有任何想法吗?
谢谢,