JEdi​​torPane,JTextPane的JTextArea中的HTML [英] HTML in JTextArea of JEditorPane, JTextPane

查看:268
本文介绍了JEdi​​torPane,JTextPane的JTextArea中的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对,我已经知道JTextArea或JTextField不支持HTML.

Right I already worked out that JTextArea or JTextField don't support HTML.

我想将文本添加到屏幕"(如JTextArea),然后再继续向其添加文本.

I want to add text to a "screen" like a JTextArea and later keep appending text to it.

我尝试了使用JTextArea的效果很好,但是它似乎不支持格式化...

I tried with JTextArea which worked wonderfully, but it does not support formatting it seems...

所以我尝试使用JEditorPane的子类JTextPane,但是此子类没有附加函数...

So I tried using JEditorPane's subclass JTextPane, but this one does not have it's append function...

有人可以在正确的方向上指导我如何轻松地将文本附加到JTextPane或格式化JTextArea.

Can someone guid me in the right direction how I easily can append text to a JTextPane or format a JTextArea.

或者如果还有其他更好的组件,请告诉我:)

Or if there is any other component better for this please tell me :)

由一个对象调用update方法,该对象对多个对象执行此操作.这只是给出了一堆字符串,这些字符串经过格式化然后放在一个不错的框架中以向用户显示.

The update method is called by a subject which does this for multiple objects. This just gives a bunch of strings which are formatted and then put in a nice frame to show the user.

@Override
public void update(String channel, String sender, String message) {

    if(channel.equals(this.subject) || sender.equals(subject)){
        StringBuffer b = new StringBuffer();
        b.append("<html>");
        b.append("<b>");
        b.append("</b>");
        b.append("[");
        b.append(sender);
        b.append("] : ");
        b.append("</b>");
        b.append(message);
        b.append("</html>");
        b.append("\n");

        chatArea.append(b.toString());
    }

推荐答案

有人可以在正确的方向上指导我如何轻松地将文本附加到JTextPane中

Can someone guid me in the right direction how I easily can append text to a JTextPane

Document doc = textPane.getDocument();
doc.insertString(doc.getLength(), "some text", green);

并使用属性而不是HTML,这要容易得多.例如,您可以将绿色"属性定义为:

And use attributes instead of HTML, its much easier. For example you could define the "green" attributes to be:

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setForeground(green, Color.GREEN);

这篇关于JEdi​​torPane,JTextPane的JTextArea中的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆