Does Java Mac OSX native look and feel respect UIM

2019-08-01 02:17发布

问题:

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.

回答1:

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


回答2:

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