jtextarea设置字体大小

要在JTextArea中设置字体大小,可以使用setFont()方法。以下是一个简单的示例代码,演示如何设置JTextArea的字体大小为16像素:

import javax.swing.JFrame;
import javax.swing.JTextArea;
import java.awt.Font;
public class JTextAreaExample {
    public static void main(String[] args) {
        JFrame frame = new JFrame("JTextArea Example");
        JTextArea textArea = new JTextArea("Hello World!", 5, 20);
        textArea.setFont(new Font("SansSerif", Font.PLAIN, 16));
        frame.add(textArea);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

在这个例子中,setFont()方法被用于将文本区域的字体设置为SansSerif字体家族的普通字体,字体大小为16像素。您可以使用setFont()方法将字体设置为任何字体和大小,具体取决于您的需求。

希望这个示例能够帮助您设置JTextArea的字体大小。

  •