在文本检索中保持正确的样式 [英] Keeping the correct style on text retrieval
问题描述
我正在制作一个类似于聊天的应用程序.为此,我有两个 JTextPanes,一个用于我编写,另一个用于显示消息.这是处理文本从输入到显示的传输的代码:
I am making an application similar to a chat. For that purpose I have two JTextPanes, one where I'm writing and one that displays messages. This is the code that handles the transferring of text from input to display :
String input = textPane.getText();
if(!input.endsWith("
")){
input+="
";
}
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);
}
问题是,如果我在输入文本的某些单词上有颜色,则输出都是彩色的.因此,当移动到显示时(在输入时正确显示),颜色会应用于所有文本.关于如何正确移动文本的任何建议?
The problem is that if i have colour on some words of the input text , the output is all coloured . So the colour is applied to all of the text when moved to display(while displayed correctly on input). Any suggestions on how i can move text properly?
请注意,其他格式如粗体、斜体等也会发生同样的情况
Notice that same happens with other format as bold , italic etc
推荐答案
通过合并两个窗格的文档模型已经解决了这个问题.解决方案在这个问题中:保持文本检索的格式
This has been solved by merging the document models of the two panes. The solution is in this question: Keeping the format on text retrieval
这篇关于在文本检索中保持正确的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!