我想在JTextArea中添加不同的字体行,但是最后一个字体似乎覆盖了另一个字体.
请帮忙…
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(); } }
解决方法
尝试使用JEditorPane / JTextPane
http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html