如何更改java JMenuItem(ctrl + s)中快捷方式文本的颜色/字体

时间:2014-12-18 07:32:38

标签: java menu colors jmenuitem

正如标题所示,试图找到一种方法来设置此快捷方式文本的颜色:

enter image description here

任何想法都会受到赞赏。

奖励积分如果还有一种方法可以改变悬停时的颜色。 (类似于我的期望:)

UIManager.put("MenuItem.selectionBackground", Color.GREEN);

编辑:......

    //SET DEFAULT COLOR STYLE FOR MENU
    //UIManager.put("TabbedPane.selected", Color.GREEN);
    UIManager.put("MenuItem.selectionBackground", bgMenuHover);
    UIManager.put("MenuItem.selectionForeground", fgMenuHover);
    UIManager.put("MenuItem.background", bgMenu);
    UIManager.put("MenuItem.foreground", fgMenu);
    UIManager.put("Menu.selectionBackground", bgMenuHover);
    UIManager.put("Menu.selectionForeground", fgMenuHover);
    UIManager.put("Menu.background", bgMenu);
    UIManager.put("Menu.foreground", fgMenu);
    UIManager.put("MenuBar.selectionBackground", bgMenuHover);
    UIManager.put("MenuBar.selectionForeground", fgMenuHover);
    UIManager.put("MenuBar.background", bgMenu);
    UIManager.put("MenuBar.foreground", fgMenu);
    UIManager.put("PopupMenu.border", BorderFactory.createLineBorder(bgMenu, 1));
    //UIManager.put("PopupMenu.forground", Color.CYAN);


    //END DEFAULT COLOR STYLE FOR MENU
...

//create menu bar
    JMenuBar menuBar = new JMenuBar();
    menuBar.setOpaque(true);

    menuBar.setBorder(null);
    menuBar.setFont(font2);

    // build first menu
    JMenu menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("NOTHING HERE YET");
    menu.setOpaque(true);

    menu.setBorder(null);
    menu.setFont(font2);
    menuBar.add(menu);


    //a group of JMenuItems
    JMenuItem menuItem = new JMenuItem("Save", KeyEvent.VK_S);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("THIS DOES NOTHIGN YET");
    menuItem.setOpaque(true);

    menuItem.setFont(font2);
    menu.add(menuItem);

    ...

1 个答案:

答案 0 :(得分:1)

更改nimbus外观中jmenuitem的快捷方式文本颜色,您可以覆盖

MenuItem:MenuItemAccelerator[MouseOver].textForeground属性

更改鼠标悬停背景颜色覆盖

MenuItem[MouseOver].backgroundPainter

示例

enter image description here

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
    if ("Nimbus".equals(info.getName())) {
        javax.swing.UIManager.setLookAndFeel(info.getClassName());
         UIManager.getLookAndFeelDefaults().put("MenuItem:MenuItemAccelerator[MouseOver].textForeground",Color.yellow);
         UIManager.getLookAndFeelDefaults().put("MenuItem[MouseOver].backgroundPainter",
                new mypainter(Color.RED));

        break;
    }
}

和班级mypainter

class mypainter implements Painter<JComponent> {

    private Color color=null;

    public mypainter(Color c) {
        color = c;
    }

    @Override
    public void paint(Graphics2D g, JComponent object, int width, int height) {
        g.setColor(color);
        g.fillRect(0, 0, width, height);
    }

}

..........................更新.................... .................

更改金属外观中的menuitem快捷方式文本,您可以覆盖属性 MenuItem.acceleratorForeground

尝试运行以下类

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

public class myframe extends JFrame{

    public myframe()  {
        super("Java Menubar Example");
        JMenuBar menuBar = new JMenuBar();

        JMenu fileMenu = new JMenu("File");
        JMenuItem openMenuItem = new JMenuItem("save");
        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        fileMenu.add(openMenuItem);
        menuBar.add(fileMenu);
        setJMenuBar(menuBar);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        UIManager.getLookAndFeelDefaults().put("MenuItem.acceleratorForeground", Color.red);
        myframe myframe = new myframe();

    }
}