我现在正在开发一个简单的聊天程序,我想出了一个在网络上找不到的问题,也许是因为我没有用正确的词来形容它。 所以,我想简单地为我使用html附加到JTextArea的文本的一部分着色,但问题是我想要着色的文本的一部分存储在一个String变量中,我不知道如何申请它上面的HTML。
这是我在代码中尝试做的事情:
String text = new String("Hello"); String htmlText = new String("<html><font color='red'>" + text + "</font></html>");但输出是<html><font color='red'>Hello</font></html>
谢谢,抱歉我的英语不好。
编辑
我已经解决了这个问题。 这是因为我的textPane.setText()方法是("<html>"+textPane.getText+"<font color='red'>text</font></html>)
我不应该使用textPane.getText() ,我应该使用textPane.getDocument().getText(0, text.getDocument().getLength()) 。
我现在的代码是:
//chatArea is a JTextPane //mesArea is just a source where I get some text if (!mesArea.getText().trim().equals("")) { mesArea.setText(mesArea.getText().trim()); try { if (chatArea.getDocument().getText(0, chatArea.getDocument().getLength()).equals("")) { chatArea.setText("<html><font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>"); } else { System.out.println("what"); String paneText = new String(chatArea.getDocument().getText(0, chatArea.getDocument().getLength())); chatArea.setText("<html>"+paneText+"\n<font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>"); } } catch (Exception ex) { ex.printStackTrace(); } mesArea.setText(""); } else { mesArea.setText(""); }感谢所有帮助过的人!
I'm right now working on a simple chat program, and I came up with an issue I couldn't find anywhere in the web, maybe because I didn't use the correct words to describe it.
So, I want to simply color part of a text I'm appending to a JTextArea using html, but the problem is the part of the text I want to color is stored in a String variable, and I don't know how to apply the html on it.
this is what I'm trying to do in code:
String text = new String("Hello"); String htmlText = new String("<html><font color='red'>" + text + "</font></html>");But the output is <html><font color='red'>Hello</font></html>
Thanks, and sorry for my bad English.
Edit
I have solved the problem. It was because my textPane.setText() method was ("<html>"+textPane.getText+"<font color='red'>text</font></html>)
I shouldn't have used textPane.getText(), I should have used textPane.getDocument().getText(0, text.getDocument().getLength()).
my present code is:
//chatArea is a JTextPane //mesArea is just a source where I get some text if (!mesArea.getText().trim().equals("")) { mesArea.setText(mesArea.getText().trim()); try { if (chatArea.getDocument().getText(0, chatArea.getDocument().getLength()).equals("")) { chatArea.setText("<html><font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>"); } else { System.out.println("what"); String paneText = new String(chatArea.getDocument().getText(0, chatArea.getDocument().getLength())); chatArea.setText("<html>"+paneText+"\n<font color='green'>"+user.getNickname()+": </font>"+mesArea.getText()+"</html>"); } } catch (Exception ex) { ex.printStackTrace(); } mesArea.setText(""); } else { mesArea.setText(""); }最满意答案
您可以使用JTextPane / JEditorPane而不是JTextArea
String text = new String("Hello"); String htmlText = new String("<html><font color='red'>" + text + "</font></html>"); JTextPane jTextPane =new JTextPane (); jTextPane.setContentType("text/html"); jTextPane.setText(htmlText);You can use JTextPane / JEditorPane instead of JTextArea like
String text = new String("Hello"); String htmlText = new String("<html><font color='red'>" + text + "</font></html>"); JTextPane jTextPane =new JTextPane (); jTextPane.setContentType("text/html"); jTextPane.setText(htmlText);更多推荐
发布评论