保持文本检索的正确风格

我正在做一个类似聊天的应用程序。 为此,我有两个JTextPanes,一个是我正在写的,一个是显示消息。 这是处理从input到显示的文本传输的代码:

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); } 

问题是,如果我对input文本的某些单词有颜色,输出​​将全部着色。 所以当移动到显示时,颜色被应用于所有的文本(当input正确显示时)。 任何build议如何我可以正确地移动文本?

请注意,其他格式如粗体,斜体等也会发生同样的情况

 The text is already formatted on the input JTextPane . What i need to do is transfer it as it is on a different JTextPane without having to check the different style options 

 import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class Fonts implements Runnable { private String[] fnt; private JFrame frm; private JScrollPane jsp; private JTextPane jta; private int width = 450; private int height = 300; private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); private StyledDocument doc; private MutableAttributeSet mas; private int cp = 0; private Highlighter.HighlightPainter cyanPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.cyan); private Highlighter.HighlightPainter redPainter = new DefaultHighlighter.DefaultHighlightPainter(Color.red); private Highlighter.HighlightPainter whitePainter = new DefaultHighlighter.DefaultHighlightPainter(Color.white); private int _count = 0; private int _lenght = 0; public Fonts() { jta = new JTextPane(); doc = jta.getStyledDocument(); jsp = new JScrollPane(jta); jsp.setPreferredSize(new Dimension(height, width)); frm = new JFrame("awesome"); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setLayout(new BorderLayout()); frm.add(jsp, BorderLayout.CENTER); frm.setLocation(100, 100); frm.pack(); frm.setVisible(true); jta.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); fnt = ge.getAvailableFontFamilyNames(); mas = jta.getInputAttributes(); new Thread(this).start(); } @Override public void run() { for (int i = 0; i < fnt.length; i++) { StyleConstants.setBold(mas, false); StyleConstants.setItalic(mas, false); StyleConstants.setFontFamily(mas, fnt[i]); StyleConstants.setFontSize(mas, 16); dis(fnt[i]); try { Thread.sleep(75); } catch (Exception e) { e.printStackTrace(); } StyleConstants.setBold(mas, true); dis(fnt[i] + " Bold"); try { Thread.sleep(75); } catch (Exception e) { e.printStackTrace(); } StyleConstants.setItalic(mas, true); dis(fnt[i] + " Bold & Italic"); try { Thread.sleep(75); } catch (Exception e) { e.printStackTrace(); } StyleConstants.setBold(mas, false); dis(fnt[i] + " Italic"); try { Thread.sleep(75); } catch (Exception e) { e.printStackTrace(); } } jta.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } public void dis(String s) { _count++; _lenght = jta.getText().length(); try { doc.insertString(cp, s, mas); doc.insertString(cp, "\n", mas); } catch (Exception bla_bla_bla_bla) { bla_bla_bla_bla.printStackTrace(); } if (_count % 2 == 0) { try { jta.getHighlighter().addHighlight(1, _lenght - 1, cyanPainter); } catch (BadLocationException bla_bla_bla_bla) { } } else if (_count % 3 == 0) { try { jta.getHighlighter().addHighlight(1, _lenght - 1, redPainter); } catch (BadLocationException bla_bla_bla_bla) { } } else { try { jta.getHighlighter().addHighlight(1, _lenght - 1, whitePainter); } catch (BadLocationException bla_bla_bla_bla) { } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Fonts fs = new Fonts(); } }); } } 

在没有良好的SSCCE的情况下,我真的不知道如何给JTextPane上的文本添加颜色,但希望这种方法可以为您解决问题:

 import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; private void appendToTextPane(String name, Color c, String f) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); aset = sc.addAttribute(aset, StyleConstants.FontFamily, f); int len = Client.nPane.getDocument().getLength(); textPane.setCaretPosition(len); textPane.setCharacterAttributes(aset, true); textPane.replaceSelection(name); } 

有了这个,你可以给每个String literal赋予不同的ColorFont ,这些String literal将会被添加到你的JTextPane中。

希望这个新的代码可能以某种方式帮助你:

 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.text.AttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; public class TextPaneTest extends JFrame { private JPanel topPanel; private JPanel bottomPanel; private JTextPane tPane1; private JTextPane tPane2; private JButton button; public TextPaneTest() { topPanel = new JPanel(); topPanel.setLayout(new GridLayout(1, 2)); bottomPanel = new JPanel(); bottomPanel.setBackground(Color.BLACK); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, Color.BLUE); aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console"); tPane1 = new JTextPane(); tPane1.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY)); tPane1.setCharacterAttributes(aset, false); // Add these settings to the other JTextPane also tPane2 = new JTextPane(); tPane2.setCharacterAttributes(aset, false); // Mimic what the other JTextPane has button = new JButton("ADD TO THE TOP JTEXTPANE"); button.setBackground(Color.DARK_GRAY); button.setForeground(Color.WHITE); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { tPane1.setText(tPane2.getText()); } }); add(topPanel, BorderLayout.CENTER); add(bottomPanel, BorderLayout.PAGE_END); topPanel.add(tPane1); topPanel.add(tPane2); bottomPanel.add(button); pack(); tPane2.requestFocusInWindow(); setVisible(true); } public static void main(String... args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new TextPaneTest(); } }); } } 

问候

这已经通过合并两个窗格的文档模型来解决。 解决方法是在这个问题: 保持文本检索的格式