gpt4 book ai didi

java - 保持文本检索的格式

转载 作者:行者123 更新时间:2023-11-30 07:28:37 25 4
gpt4 key购买 nike

我正在制作一个具有聊天功能的网络应用程序。在聊天中我有一个 JTextPane用于显示消息,还有一个用于输入。然后我有一些按钮可以在输入文本上添加样式(粗体、斜体、字体大小、颜色)。文本在输入 Pane 中的格式正确,尽管当移动到显示 Pane 时(一旦按下正确的 JButton)它只有最后一个字符的格式。如何在保持其原始格式的同时移动文本?例如,如果我在输入上写“Hello Worl d”,显示屏会显示“Hello Worl d”

textPane 是输入面板

在哪里设置:

final SimpleAttributeSet set = new SimpleAttributeSet();

将输入文本加粗的代码(与添加其他样式相同):

bold.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
StyledDocument doc = textPane.getStyledDocument();
if (StyleConstants.isBold(set)) {
StyleConstants.setBold(set, false);
bold.setSelected(false);
} else {
StyleConstants.setBold(set, true);
bold.setSelected(true);
}
textPane.setCharacterAttributes(set, true);
}
});

将文本从输入 Pane 移动到显示 Pane 的代码:

getInput.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String input = textPane.getText();
textPane.setText("");
if(!input.endsWith("\n")){
input+="\n";
}
StyledDocument doc = displayPane.getStyledDocument();
int offset = displayPane.getCaretPosition();
try {
doc.insertString(offset, input, set);
} catch (BadLocationException ex) {
Logger.getLogger(ChatComponent.class.getName()).log(Level.SEVERE, null, ex);
}
}
});

最佳答案

使用示例合并两个文档 http://java-sl.com/tip_merge_documents.html

关于java - 保持文本检索的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9040294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com