changing the font in a JTextArea for different lines
I would like to append differnt lines of font to the JTextArea, however the last font seems to override the other.
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.ITAL开发者_如何学运维IC, 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();
}
}
Try using JEditorPane / JTextPane
http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html
These support HTML formatting. A normal JTextArea's setFont method will just set the font for the entire textarea.
精彩评论