swing里默认的菜单字体和按钮字体都是不知名的字体,而且还是粗体,现在我想要宋体14号大小,我有没什么办法可以不用一个个的菜单项、按钮的设置?我在网上找到方法是这样的
Font font = new Font("宋体",Font.PLAIN,15);
UIManager.put("JMenuBar.font", font);
事实证明这个方法是骗人的,根本没有反应

解决方案 »

  1.   

    可以看一下BasicLookAndFeel的代码,里面的属性是没有"JMenuBar.font",而是"Menu.font"。
    下面是对Button的字体的测试代码。
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;@SuppressWarnings("serial")
    public class FontTestFrame extends JFrame {

    public FontTestFrame()
    {
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(new JButton("测试"));
    this.pack();
    this.setVisible(true);
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Font font = new Font("宋体",Font.PLAIN,15);
    UIManager.put("Button.font", font); 
    new FontTestFrame();
    }}
      

  2.   

    我还是不行啊,我是这样写的
    Font font = new Font("宋体",Font.PLAIN,15);
    UIManager.put("Menu.font", font);
    运行后还是既不报错也不没变化,就像这条语句不存在似的
      

  3.   


    【应该没J的吧,UIManager.put("MenuBar.font", font); ,这个方法是可行的,给个例子】【方法: //统一程序字体
      private static void InitGlobalFont(Font font) {
        FontUIResource fontRes = new FontUIResource(font);
        for (Enumeration<Object> keys = UIManager.getDefaults().keys();
             keys.hasMoreElements(); ) {
          Object key = keys.nextElement();
          Object value = UIManager.get(key);
          if (value instanceof FontUIResource) {
            UIManager.put(key, fontRes);
          }
        }

    【调用: InitGlobalFont(new Font("宋体", Font.PLAIN, 12));】
    我毕业设计中用过,实验证明是可行的
      

  4.   

    【我把uimanager的部分属性截给你看看,楼主自行摸索】
    Menu.acceleratorFont 
    Menu.acceleratorForeground 
    Menu.acceleratorSelectionForeground 
    Menu.arrowIcon 
    Menu.background 
    Menu.border 
    Menu.borderPainted 
    Menu.checkIcon 
    Menu.consumesTabs 
    Menu.disabledForeground 
    Menu.font 
    Menu.foreground 
    Menu.margin 
    Menu.selectedWindowInputMapBindings 
    Menu.selectionBackground 
    Menu.selectionForeground 
    MenuBar.background 
    MenuBar.border 
    MenuBar.font 
    MenuBar.foreground 
    MenuBar.windowBindings 
    MenuBarUI 
    MenuItem.acceleratorDelimiter 
    MenuItem.acceleratorFont 
    MenuItem.acceleratorForeground 
    MenuItem.acceleratorSelectionForeground 
    MenuItem.arrowIcon 
    MenuItem.background 
    MenuItem.border 
    MenuItem.borderPainted 
    MenuItem.checkIcon 
    MenuItem.disabledForeground 
    MenuItem.font 
    MenuItem.foreground 
    MenuItem.margin 
    MenuItem.selectionBackground 
    MenuItem.selectionForeground 
    MenuItemUI 
    MenuUI 
      

  5.   

    在swing里面关闭默认皮肤的粗体显示,用下面的代码:UIManager.put("swing.boldMetal", Boolean.FALSE);设置统一字体的话,看看3楼的代码。
      

  6.   

    如下:我本地测试是可以的,Menu和MenuItem都可以变成宋体,如果还是不行,先确认本机是不是有宋体
    import java.awt.Font;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.UIManager;@SuppressWarnings("serial")
    public class FontTestFrame extends JFrame {

    public FontTestFrame()
    {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menubar = new JMenuBar();
    JMenu m = new JMenu("文件");
    m.add(new JMenuItem("打开"));
    menubar.add(m);
    this.setJMenuBar(menubar);
    this.add(new JButton("测试 "));
    this.pack();
    this.setVisible(true);
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Font font = new Font("宋体",Font.PLAIN,15);
    UIManager.put("Button.font", font); 
    UIManager.put("Menu.font", font);
    UIManager.put("MenuItem.font", font);
    new FontTestFrame();
    }}
      

  7.   

    你的UIManager设置的代码写得位置不对吧?
      

  8.   

    呵呵,我的问题解决了,就是位置放错了哈!先写设置,再NEW,搞定,谢谢8楼大虾了