在java字符串变量上使用HTML(Using HTML on java string variables)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
在java字符串变量上使用HTML(Using HTML on java string variables) java

我现在正在开发一个简单的聊天程序,我想出了一个在网络上找不到的问题,也许是因为我没有用正确的词来形容它。 所以,我想简单地为我使用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);

更多推荐

本文发布于:2023-04-17 09:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f96a619452879e73115eddc91b88cd19.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   变量   java   variables   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!