Java自动适应Windows 7字体大小调整

发布于 2024-08-17 04:35:47 字数 146 浏览 4 评论 0原文

在Windows 7中,如果您通过控制面板->外观和个性化->更改字体大小显示“放大或缩小文本和其他项目”,它不仅可以调整菜单大小,还可以调整记事本、写字板、Firefox 等应用程序的文本内容大小。

有没有办法让Java自动缩放字体而无需手动缩放?

In Windows 7, if you change the font size via the Control Panel->Appearance and Personalization -> Display "Make text and other items larger or smaller", it adjusts not only the menu sizes, but also the text content size of apps like Notepad, Wordpad, Firefox.

Is there a way to get Java to automatically scale the font without having to manually scale it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2024-08-24 04:35:47

这有两个部分:

  1. 获取组件、字体等
    缩放
  2. 让您的布局达到
    对于Swing

,第一部分很简单 - 一切都从一次调用开始。

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

在 Windows 上,这将导致它尊重您的小/大字体 (DPI) 设置。

以下是我整理的快速测试应用程序的两个屏幕截图,演示了它在 Windows 7 @ 96dpi(正常字体大小)和 @ 144dpi(150%)中的机器上的外观。

首先是默认字体大小示例:

Swing 应用程序与 Windows 使用正常字体大小

现在设置更大 (150%) 字体大小:

Swing 应用程序与 Windows 使用较大的字体大小

运行之间没有代码更改,仅注销和退出使用新的 DPI 设置返回。我故意设置了一个固定的框架尺寸,以证明我的容器尺寸没有缩放,这导致我的标签被推下以适应。

这是我的源代码 - cut &自己粘贴并运行:

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();
            }
        });
    }

}

The Look & Feel 提供默认大小,但由 GUI 作者在其布局中使用可缩放单位。这确实需要付出努力(可扩展布局在网页上也是一种痛苦!),但它绝对是可以实现的。

我建议使用像 FormLayout 这样的布局,它可以让您在对话框单元 (DLU ),因为它们随 DPI 缩放。这将使您能够缩放容器的尺寸,并有助于限制标签因尺寸调整而移动到下一行等行为。如果框架的大小是使用对话单元确定的,那么它可以看起来相同,只是更大。

已经很晚了——所以现在就这样。

There are two parts to this:

  1. Getting your components, fonts, etc
    to scale
  2. Getting your layouts to
    scale

For Swing, the first part is easy - it all starts with one call.

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

On Windows, this will cause it to respect your Small/Large fonts (DPI) setting.

Here are two screenshots from a quick test app I threw together, demonstrating how it looks on my machine in Windows 7 @ 96dpi (normal font size) and @ 144dpi (150%)

First the default font size sample:

Swing app with Windows using normal font sizes

Now with Larger (150%) font size set:

Swing app with Windows using Larger font sizes

There is no code change between runs, only logging out & back in with new DPI settings. I set a fixed frame size on purpose to demonstrate that my container is not scaling in size, which caused my label to get pushed down in order to fit.

Here is my source code - cut & paste and run it yourself:

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();
            }
        });
    }

}

The Look & Feel supplies the default sizing, but it is up to the GUI author to use scalable units in their layouts. It does take effort (scalable layouts are a pain on webpages too!), but it's definitely attainable.

I recommend using a layout like FormLayout that will let you define your layouts in Dialog Units (DLU), as these scale with DPI. That will enable you to make your containers scale in size and should help limit behaviors like the label moving to the next line due to sizing. If the size of the frame was determined using dialog units then it could be made to look the same, only larger.

It is late - so that's it for now.

星星的軌跡 2024-08-24 04:35:47

这完全取决于您使用的 GUI 工具包。我几乎可以肯定 SWT 会自动选择新的尺寸(因为它使用本机小部件)。据我所知,AWT 也可能是这样。您能否指定您正在使用什么工具包以及您是否确定它尚未执行您想要的操作(以及可能如何执行)?

This would depend entirely on what GUI toolkit you're using. I'm almost certain that SWT for instance would automatically pick up the new sizes (since it uses native widgets). AWT might as well for all I know. Can you specify what what toolkit you're using and whether you've determined that its not already doing what you want (and possibly how)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文