Java automatically adjusts the font size to Windows 7

In Windows 7, if you change the font size through the control panel – > appearance and personalization – > display "make text and other items larger or smaller", it can not only adjust the menu size, but also adjust the text content size of Notepad, WordPad, Firefox and other applications

Is there a way for Java to automatically scale fonts without having to scale them manually?

Solution

There are two parts:

>Get your components, fonts, etc. scale > get your layout scale

The first part is easy for swing – it all starts with a phone call

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

On windows, this will match your small / large font (DPI) settings

Here are two screenshots of a quick test application that show how they look on my machine in Windows 7 @ 96dpi (normal font) and @ 144dpi (150%),

First, an example of the default font size:

Now larger (150%) font size settings:

There is no code change between runs, so you can only log out & back to the new DPI setting I set a fixed frame size to prove that my container did not scale, which caused my label to be pushed down to fit

This is my source code – cut & paste and run:

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class SwingFontTest
{
    private static void createGUI()
    {
        JButton button = new JButton("my button with Some Text");
        JLabel label = new JLabel("and a label");

        JPanel panel = new JPanel(new FlowLayout());
        panel.add(button);
        panel.add(label);

        JFrame frame = new JFrame("Title!");
        frame.setContentPane(panel);
        frame.setSize(300,125);
        frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        SwingUtilities.invokelater(new Runnable() {
            @Override
            public void run()
            {
                createGUI();
            }
        });
    }

}

Appearance & feel provides the default size, but the GUI author uses extensible units in the layout It does take effort (scalable layout is also a pain on Web pages), but it is absolutely achievable

I recommend using layouts like formlayout, which allows you to define layouts in dialog units (DLUs) because these use dpi for scaling This will enable you to downsize your container and should help limit the behavior of moving the label to the next line due to resizing If the dialog unit is used to determine the size of the frame, it can make it look the same, with only a larger one

It's late now – that's it now

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