Java swing on high DPI screen

I have a Java Swing program that uses the system look and feel:

UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());

The problem is that on high DPI systems, the font in the frame is too small How can I make the text on my frame readable without changing the font of all frames? My program is written in Java 6, and there are too many frameworks to modify

Solution

You can physically modify the font settings for the look and feel

import java.awt.EventQueue;
import java.awt.Font;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new test();
    }

    public test() {
        EventQueue.invokelater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | illegalaccessexception | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                setDefaultSize(24);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JLabel("Happy as a pig in a blanket"));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static void setDefaultSize(int size) {

        Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
        Object[] keys = keySet.toArray(new Object[keySet.size()]);

        for (Object key : keys) {

            if (key != null && key.toString().toLowerCase().contains("font")) {

                System.out.println(key);
                Font font = UIManager.getDefaults().getFont(key);
                if (font != null) {
                    font = font.deriveFont((float)size);
                    UIManager.put(key,font);
                }

            }

        }

    }

}

We use a similar approach to increase / decrease the font size of the running application to test the layout (and eventually allow the user to have some additional control over the font size)

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
分享
二维码
< <上一篇
下一篇>>