Changing a JMenuBar's font

2019-07-20 21:34发布

问题:

I have problems with setting the font of a JMenuBar. I personally don't like the bold font Java frames use by default, so I tried to change it by using something like this:

public class MyFrame extends javax.swing.JFrame {
    public MyFrame() {
        JMenuBar menuBar = new JMenuBar();
        menuBar.setFont(new Font("sans-serif", Font.PLAIN, 12));
        setJMenuBar(menuBar);
        setSize(600, 400);

        // add some menus to the menu bar
        menuBar.add(new JMenu("Foo"));
        menuBar.add(new JMenu("Bar"));
        menuBar.add(new JMenu("Baz"));
        menuBar.add(new JMenu("Qux"));

        setVisible(true);
    }
}

As far as I know, the line menuBar.setFont(...) sets the font used by the component menuBar. But when I instantiated one of these frames, the default font didn't change at all, not even when I put the font's size to 30.

I appreciate any help concerning this.

回答1:

You can either try setting the font for each JMenu or change the default:

Font f = new Font("sans-serif", Font.PLAIN, 12);
UIManager.put("Menu.font", f);