Java 更改JTextArea中特定行的字体

Java 更改JTextArea中特定行的字体,java,swing,fonts,jtextarea,Java,Swing,Fonts,Jtextarea,你好,我正在开发一个聊天应用程序,我希望用户可以更改他/她正在书写的字体。有一个setFont()函数,但它会更改文本区域中所有字符串的字体。所以我只想更改我的字体。如果你能帮助我,我将不胜感激。你不能用JTextArea来做这件事,但你可以用它更喜欢的表亲JTextPane来做。不幸的是,这不是微不足道的;您可以了解这个类您应该使用JTextPane。JTextPane允许您使用HTML。检查以下示例: this.text_panel = new JTextPane(); this.text_

你好,我正在开发一个聊天应用程序,我希望用户可以更改他/她正在书写的字体。有一个
setFont()
函数,但它会更改文本区域中所有字符串的字体。所以我只想更改我的字体。如果你能帮助我,我将不胜感激。

你不能用
JTextArea
来做这件事,但你可以用它更喜欢的表亲
JTextPane
来做。不幸的是,这不是微不足道的;您可以了解这个类

您应该使用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());
这里您正在启用HTMLEditorKit,它将允许您在文本窗格中使用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();
    }
}
public void append(字符串行){
SimpleDataFormat日期\格式=新SimpleDataFormat(“HH:mm:ss”);
日期=新日期();
line=“[”+日期\格式。格式(日期)+“]”“+行+”;
试一试{
this.text_panel_html_kit.insertHTML((HTMLDocument)this.text_panel.getDocument(),this.text_panel.getDocument().getLength(),行,0,0,null);
}捕获(例外e){
e、 printStackTrace();
}
}

希望这有帮助,

Serhiy.

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

下面是一个使用后者的简单示例

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);
            }
        });
    }
}

import java.awt.*;
导入javax.swing.*;
类ShowFonts{
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
GraphicsEnvironment ge=GraphicsEnvironment。
getLocalGraphicsEnvironment();
String[]fonts=ge.getAvailableFontFamilyNames();
字符串pre=“
    ”; StringBuilder sb=新的StringBuilder(预安装); 用于(字符串字体:字体){ 某人追加(“