更改菜单栏和菜单项的字体 [英] Change menu bar and menu items font

查看:412
本文介绍了更改菜单栏和菜单项的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使菜单栏项目和项目的项目越来越小,我已经在这里找到了一些(所以不要把这个标记为重复请),而且我没有任何帖子发现工作。



我已经尝试了下面的代码没有成功:

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



 <$ c)c> menuBar.setFont(new Font(menuBar.getFont()。getFontName(),menuBar.getFont()。getStyle(),12)); 

这是我在编辑字体的代码:

  private class foo {
private JMenu mnArchivo;
私人JMenuBar menuBar;
menuBar = new JMenuBar();
frmAdministracinHospital.setJMenuBar(menuBar);

JRadioButtonMenuItem rdbtnmntmGrande = new JRadioButtonMenuItem(Grande);
rdbtnmntmGrande.addActionListener(new MiGrandeActionListener());
rdbtnmntmGrande.setIcon(new ImageIcon(PrincipalWindow.class.getResource(/ presentacion / fontbig.png)));
buttonGroup.add(rdbtnmntmGrande);
mnTamanoFuente.add(rdbtnmntmGrande);

private class MiGrandeActionListener implements ActionListener {
public void actionPerformed(ActionEvent e){

Font f = new Font(menuBar.getFont()。getFontName(), menuBar.getFont()。getStyle(),12);
UIManager.put(Menu.font,f);








解决方案

这是因为



更改字体后:

,下一次,请贴出一个有效的,与我一样,所以我们可以复制粘贴,我没有使用 actionListener ,因为问题与动作无关,而是字体。或图标因为它不相关,我做了一个完整的示例代码,你可以复制粘贴,看看它是如何工作的,而不需要修改任何东西,这就是你所要求的。

对于 MenuBar 字体,您需要调用:

  UIManager.put(Menu.font,f); 

对于 MenuItem

  UIManager.put(MenuItem.font,f); 


I'm trying to make the menu bar items and the items of the items bigger and smaller, I've seached here a bit before (so don't mark this as repeated please) and none of the posts I've found are working.

I've tried the following codes without success:

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

And

menuBar.setFont(new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12));

And this is my code where I'm trying to edit the font:

private class foo{
        private JMenu mnArchivo;
        private JMenuBar menuBar;
        menuBar = new JMenuBar();
        frmAdministracinHospital.setJMenuBar(menuBar);

    JRadioButtonMenuItem rdbtnmntmGrande = new JRadioButtonMenuItem("Grande");
            rdbtnmntmGrande.addActionListener(new MiGrandeActionListener());
            rdbtnmntmGrande.setIcon(new ImageIcon(PrincipalWindow.class.getResource("/presentacion/fontbig.png")));
            buttonGroup.add(rdbtnmntmGrande);
            mnTamanoFuente.add(rdbtnmntmGrande);

    private class MiGrandeActionListener implements ActionListener {
            public void actionPerformed(ActionEvent e) {

                Font f = new Font(menuBar.getFont().getFontName(), menuBar.getFont().getStyle(), 12);
                UIManager.put("Menu.font", f);
            }
        }

Any clue please?

解决方案

That's because there's not "menuBar.font" key in the UIManager class, it should be:

UIManager.put("MenuBar.font", f);

i.e. Caps are important or:

UIManager.put("MenuItem.font", f);

for each JMenuItem's font

Here's a list of these properties

Also related: Changing a JMenuBar's font


EDIT: Added mcve

I don't see where it's not working, if I run this code, it works fine for me

import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class UIManagerFontChangeExample {

    private JFrame frame;
    private JLabel label;
    private JMenuItem item1, item2;
    private JMenu menu;
    private JMenuBar bar;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Font f = new Font("sans-serif", Font.PLAIN, 12);
                UIManager.put("Menu.font", f);
                UIManager.put("MenuItem.font", f);
                UIManagerFontChangeExample example = new UIManagerFontChangeExample();
                example.createAndShowGui();
            }
        });
    }

    public void createAndShowGui() {
        frame = new JFrame("Font changing example");
        label = new JLabel("This is a label");
        bar = new JMenuBar();
        menu = new JMenu("Menu");
        item1 = new JMenuItem("Item1");
        item2 = new JMenuItem("Item2");

        menu.add(item1);
        menu.add(item2);
        bar.add(menu);

        frame.add(label);
        frame.setJMenuBar(bar);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

Before changing font:

After changing font:

The code I added above is called: Minimal, Complete and Verifiable Example, next time, please post a valid one, the same I did, so we can copy-paste, I didn't use an actionListener because the question isn't related to the actions, but the font. Or Icon because it's not related either, I did a full example code that you can copy-paste and see how it works, without modifying anything, that's what you were asked for.

For the MenuBar font you need to call:

UIManager.put("Menu.font", f);

And for the MenuItem

UIManager.put("MenuItem.font", f);

这篇关于更改菜单栏和菜单项的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆