Java – change fonts of different lines in jtextarea

I want to add different font lines in jtextarea, but the last font seems to overwrite another font

Please help

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

@SuppressWarnings("serial")
public class test extends JFrame {

private static JTextArea referenceTextArea = new JTextArea(10,10);
private JPanel panel = new JPanel();

public test() {
    this.add(panel);
    panel.add(referenceTextArea);
}

public static void texttest() {
    referenceTextArea.setFont(new Font("Serif",Font.BOLD,15));
    referenceTextArea.append("line1");
    referenceTextArea.append("\n");

    referenceTextArea.setFont(new Font("Serif",Font.ITALIC,30));
    referenceTextArea.append("line2");
    referenceTextArea.append("\n");
}

public static void main(String[] args) {
    test frame = new test();
    frame.setVisible(true);
    frame.setSize(400,400);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloSEOperation(EXIT_ON_CLOSE);

    texttest();
}
}

Solution

Try using jeditorpane / jtextpane

http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html

These support HTML format The normal jtextarea SetFont method only sets the font of the entire textarea

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>