Java Mac OSX 本机外观和感觉是否尊重 UIManager 字体更改?

发布于 2024-09-02 19:53:31 字数 279 浏览 5 评论 0原文

我有一个 java 小程序,唯一可以正常工作的外观和感觉是本机 mac 小程序。我想让字体更大一点,并尝试使用标准 UIManager 方法

UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));

这不会产生任何变化。当然,它不会抛出异常。

有谁知道原生 mac 外观和感觉是否忽略这些?

我知道有一些特定的方法可以使 mac 上的控件具有不同的大小,但这些似乎只会使它们更小。您不能使控件比常规控件大。

I have a java applet and the only look and feel that works properly is the native mac one. I wanted to make the fonts a bit larger and tried using the standard UIManager methods

UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));

This produces no change. It does not throw an exception, of course.

Does anyone know if the native mac look and feel ignores these?

I know there are specific ways to make controls different sizes on mac but these only seem to make them smaller. You cannot make the controls larger than regular.

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

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

发布评论

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

评论(2

幽蝶幻影 2024-09-09 19:53:32

updateComponentTreeUI(...) 方法(在由trashgod 提供的“启动后更改LAF”链接中引用)仅适用于 FontUIResource,不适用于 Font。仅当您需要在启动后多次更改字体时,这才有意义。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;

public class ChangeFont extends JFrame
{
    private int size = 12;
    private JComponent component;

    public ChangeFont()
    {
        JTextArea textArea = new JTextArea();
        textArea.append( "updateComponentTreeUI will only work on a FontUIResource\n\n" );
        textArea.append( "1) click the FontUIResource button as many times as you want\n" );
        textArea.append( "2) after you click the Font button, neither button will work" );
        getContentPane().add(textArea, BorderLayout.NORTH);

        JButton west = new JButton( "FontUIResource" );
        west.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                update( new FontUIResource("monospaced", Font.PLAIN, size) );
            }
        });
        getContentPane().add(west, BorderLayout.WEST );

        JButton east = new JButton( "Font" );
        east.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                update( new Font("monospaced", Font.PLAIN, size) );
            }
        });
        getContentPane().add(east, BorderLayout.EAST );

        component = new JTable(5, 5);
        getContentPane().add(component, BorderLayout.SOUTH);
    }

    private void update(Font font)
    {
        UIManager.put("Table.font", font);
        UIManager.put("TextArea.font", font);
        SwingUtilities.updateComponentTreeUI( this );
        size += 2;
        pack();
    }

    public static void main(String[] args)
    {
        ChangeFont frame = new ChangeFont();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

The updateComponentTreeUI(...) method (referenced in the Changing the LAF After Startup link provided by trashgod) will only work on a FontUIResource, not a Font. This is only relevant if you need to change the Font multiple times after startup.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;

public class ChangeFont extends JFrame
{
    private int size = 12;
    private JComponent component;

    public ChangeFont()
    {
        JTextArea textArea = new JTextArea();
        textArea.append( "updateComponentTreeUI will only work on a FontUIResource\n\n" );
        textArea.append( "1) click the FontUIResource button as many times as you want\n" );
        textArea.append( "2) after you click the Font button, neither button will work" );
        getContentPane().add(textArea, BorderLayout.NORTH);

        JButton west = new JButton( "FontUIResource" );
        west.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                update( new FontUIResource("monospaced", Font.PLAIN, size) );
            }
        });
        getContentPane().add(west, BorderLayout.WEST );

        JButton east = new JButton( "Font" );
        east.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                update( new Font("monospaced", Font.PLAIN, size) );
            }
        });
        getContentPane().add(east, BorderLayout.EAST );

        component = new JTable(5, 5);
        getContentPane().add(component, BorderLayout.SOUTH);
    }

    private void update(Font font)
    {
        UIManager.put("Table.font", font);
        UIManager.put("TextArea.font", font);
        SwingUtilities.updateComponentTreeUI( this );
        size += 2;
        pack();
    }

    public static void main(String[] args)
    {
        ChangeFont frame = new ChangeFont();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}
屋檐 2024-09-09 19:53:32

它似乎可以在安装了任何 L&F 的 Mac OS X 上运行。

附录:如果您尝试在启动后更改设置,请参阅 如何设置外观启动后更改外观

public final class Laf {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
                f.add(new JLabel("Test"));
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

public final class LafApplet extends JApplet {

    @Override
    public void init() {
        UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
        this.add(new JLabel("Test"));
    }
}

It appears to work on Mac OS X with any installed L&F.

Addendum: If you are trying to change the setting after startup, see How to Set the Look and Feel under Changing the Look and Feel After Startup.

public final class Laf {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
                f.add(new JLabel("Test"));
                f.pack();
                f.setVisible(true);
            }
        });
    }
}

public final class LafApplet extends JApplet {

    @Override
    public void init() {
        UIManager.put("Label.font", new Font("Georgia", Font.PLAIN, 18));
        this.add(new JLabel("Test"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文