How to adjust the font size in Java?

In Java, text display size can be adjusted by setting the font size. Here is a simple method to do so:

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;

public class Main {
    public static void main(String[] args) {
        // 创建一个 JFrame 对象
        JFrame frame = new JFrame("调整字体大小示例");

        // 创建一个标签
        JLabel label = new JLabel("Hello, World!");

        // 创建一个字体对象,设置字体名称、样式和大小
        Font font = new Font("Arial", Font.PLAIN, 24);

        // 将字体应用到标签上
        label.setFont(font);

        // 将标签添加到 JFrame 中
        frame.add(label);

        // 设置 JFrame 的大小和可见性
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

In the code above, we have created a JFrame object and a JLabel object, and set the font of the label to Arial style, regular style, and size 24. You can adjust the font’s name, style, and size as needed to adjust the display size of the text.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds