java 更改 JTextArea 中特定行的字体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7323448/
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-10-30 19:32:04  来源:igfitidea点击:

change font of specific lines in JTextArea

javaswingfontsjtextarea

提问by quartaela

hi there ? am working on a chat application and i want that user can change the font which he/she is writing. there is a setFont()function but it changes font of all strings in the TextArea. so i just want to change only my font.i appreciated if you can help me.

你好呀 ?我正在开发一个聊天应用程序,我希望该用户可以更改他/她正在编写的字体。有一个setFont()函数,但它会更改 TextArea 中所有字符串的字体。所以我只想改变我的字体。如果你能帮助我,我很感激。

回答by camickr

well then i guess i must learn a litte HTML

那么我想我必须学习一点 HTML

I wouldn't use HTML. I find it easier to just use attributes when dealing with a text pane. Attributes are much easier to change then trying to manipulate HTML.

我不会使用 HTML。我发现在处理文本窗格时使用属性更容易。与尝试操作 HTML 相比,更改属性要容易得多。

SimpleAttributeSet green = new SimpleAttributeSet();
StyleConstants.setFontFamily(green, "Courier New Italic");
StyleConstants.setForeground(green, Color.GREEN);

//  Add some text

try
{
    textPane.getDocument().insertString(0, "green text with Courier font", green);
}
catch(Exception e) {}

回答by Serhiy

You should work with JTextPane. JTextPane allows you to use HTML. Check the following example:

您应该使用 JTextPane。JTextPane 允许您使用 HTML。检查以下示例:

this.text_panel = new JTextPane();
this.text_panel.setContentType("text/html");
this.text_panel.setEditable(false);
this.text_panel.setBackground(this.text_background_color);
this.text_panel_html_kit = new HTMLEditorKit();
this.text_panel.setEditorKit(text_panel_html_kit);
this.text_panel.setDocument(new HTMLDocument());

Here you are enabling HTMLEditorKit, which will allow you to use HTML in your TextPane. Here is another peice of code, where you can add colored text to the panel:

在这里,您将启用 HTMLEditorKit,这将允许您在 TextPane 中使用 HTML。这是另一段代码,您可以在其中向面板添加彩色文本:

public void append(String line){
    SimpleDateFormat date_format = new SimpleDateFormat("HH:mm:ss");
    Date date = new Date();

    line = "<div><font size=3 color=GRAY>[" + date_format.format(date) + "]</font><font size=3 color=BLACK>"+ line + "</font></div>";

    try {
        this.text_panel_html_kit.insertHTML((HTMLDocument) this.text_panel.getDocument(), this.text_panel.getDocument().getLength(), line, 0, 0, null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}


Hope this helps,
Serhiy.


希望这会
有所帮助,Serhiy。

回答by Ernest Friedman-Hill

You can't do that with JTextArea, but you can do it with its fancier cousin, JTextPane. It's unfortunately not trivial; you can learn about this class here.

你不能用JTextArea来做到这一点,但你可以用它更漂亮的表亲来做到这一点,JTextPane。不幸的是,这并非微不足道。你可以在这里了解这门课

回答by Andrew Thompson

A variety of Swing components will render basic HTML (version 3.2), including JLabel& JEditorPane. For further details see How to Use HTML in Swing Componentsin the Java Tutorial.

各种 Swing 组件将呈现基本的 HTML(3.2 版),包括JLabel& JEditorPane。有关更多详细信息,请参阅Java 教程中的如何在 Swing 组件使用 HTML

Here is a simple example using the latter.

这是一个使用后者的简单示例。

Fonts rendered in HTML

以 HTML 呈现的字体

import java.awt.*;
import javax.swing.*;

class ShowFonts {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GraphicsEnvironment ge = GraphicsEnvironment.
                    getLocalGraphicsEnvironment();
                        String[] fonts = ge.getAvailableFontFamilyNames();
                String pre = "<html><body style='font-size: 20px;'><ul>";
                StringBuilder sb = new StringBuilder(pre);
                for (String font : fonts) {
                    sb.append("<li style='font-family: ");
                    sb.append(font);
                    sb.append("'>");
                    sb.append(font);
                }
                JEditorPane ep = new JEditorPane();
                ep.setContentType("text/html");
                ep.setText(sb.toString());


                JScrollPane sp = new JScrollPane(ep);
                Dimension d = ep.getPreferredSize();
                sp.setPreferredSize(new Dimension(d.width,200));

                JOptionPane.showMessageDialog(null, sp);
            }
        });
    }
}