【问题标题】:Does Java Mac OSX native look and feel respect UIManager font changes?Java Mac OSX 原生外观和感觉是否尊重 UIManager 字体更改?
【发布时间】:2011-02-22 23:28:05
【问题描述】:

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

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

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

有谁知道本机 mac 外观是否忽略了这些?

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

【问题讨论】:

    标签: java macos swing size fonts


    【解决方案1】:

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

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

    【讨论】:

    • 我不知道。很好的例子!
    【解决方案2】:

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

    附录:如果您在启动后尝试更改设置,请参阅Changing the Look and Feel After Startup 下的How to Set the Look and Feel

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

    【讨论】:

    • 使用 FontUIResource 代替 Font 可能更安全。请参阅提供的演示。
    • 我正在做一个 JApplet,我在 init 中设置它,然后不改变它。它适用于 Metal Look and Feel,但我通过创建一个 metallookand feel 子类来实现这一点。由于两个人认为即使使用 mac 本机外观和感觉也可以使用,我会再试一次。谢谢
    • 它适用于上述JApplet 中的所有L&F,以@camickr 提出的观点为准。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 2012-07-18
    • 2019-06-11
    • 2010-09-23
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多