Changing a JMenuBar's font

情到浓时终转凉″ 提交于 2019-12-11 00:43:15

问题


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


来源:https://stackoverflow.com/questions/27318130/changing-a-jmenubars-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!