在 java 字符串变量上使用 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33067906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:08:42  来源:igfitidea点击:

Using HTML on java string variables

javahtmlstring

提问by Ido Fang Bentov

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.

所以,我想简单地使用 html 为我附加到 JTextArea 的文本的一部分着色,但问题是我想要着色的文本部分存储在一个 String 变量中,我不知道如何应用上面的html。

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>

但输出是 <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>)

我已经解决了这个问题。那是因为我的textPane.setText()方法是 ("<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()).

我不应该用textPane.getText(),我应该用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("");
}

回答by Vishnu Katpure

You can use JTextPane / JEditorPaneinstead of JTextArea like

您可以使用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);