如何修改JMenus和JMenuItems的背景和前景色?

时间:2016-03-14 14:26:08

标签: java swing colors jmenuitem jmenubar

我用JMenuBar背景/前景颜色解决了问题,但现在我需要更改JMenus和JMenuItems的颜色。我已经广泛搜索并且还没有找到解决问题的答案。

EDIT ****** 下面是我的构造函数方法的代码。我用了一个单独的类来创建我的菜单栏。我很难编码而不是使用GUI构建器,所以它可能有点乱:

PeriodicTableFrame() {
    UIManager.put("Menu.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.blue));
    UIManager.put("MenuItem.selectionBackground", new javax.swing.plaf.ColorUIResource(Color.green));
    setLabels();
    setJMenuBar(mnuBar);
    mnuBar.setColor(backgroundBlack);
    mnuBar.setForeground(textWhite);
    mnuBar.add(file);
    file.setBackground(backgroundBlack);
    file.setForeground(textWhite);
    file.add(fileExit);
    fileExit.setForeground(textWhite);
    mnuBar.add(options);
    options.setForeground(textWhite);
    mnuBar.add(help);
    help.setForeground(textWhite);
    help.add(helpAbout);
    helpAbout.setForeground(textWhite);

    fileExit.addActionListener(this);
    helpAbout.addActionListener(this);

    fileExit.setActionCommand("Exit");
    helpAbout.setActionCommand("About");

    for (int i = 0; i < 162; i++) {
        elementButtons[i] = new JButton(elementButtonLabels[i]);
    }
    this.setLayout(new BorderLayout());
    displayPanel.setLayout(new GridLayout(9, 19, 5, 5));
    for (int i = 0; i < 162; i++) {
        displayPanel.add(elementButtons[i]);
        if (i > 0 && i < 17) {
            elementButtons[i].setVisible(false);
        }
        if (i > 19 && i < 30) {
            elementButtons[i].setVisible(false);
        }
        if (i > 37 && i < 48) {
            elementButtons[i].setVisible(false);
        }
        if (i > 125 && i < 129) {
            elementButtons[i].setVisible(false);
        }
        if (i > 143 && i < 147) {
            elementButtons[i].setVisible(false);
        }
    }
    for (int i = 0; i < 162; i++) {
        if (i == 0 || (i < 34 && i > 30) || i == 50 || i == 51 || i == 69) {
            elementButtons[i].setBackground(nonmetalRed);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 18 || i == 36 || i == 54 || i == 72 || i == 90 || i == 108) {
            elementButtons[i].setBackground(alkaliMetalGreen);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 19 || i == 37 || i == 55 || i == 73 || i == 91 || i == 109) {
            elementButtons[i].setBackground(alkalineEarthMetalGreen);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 30 || i == 49 || i == 67 || i == 68 || i == 86 || i == 87 || i == 105) {
            elementButtons[i].setBackground(metalloidViolet);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 48 || i == 66 || i == 84 || i == 85 || (i > 101 && i < 105) || (i > 119 && i < 124)) {
            elementButtons[i].setBackground(otherMetalPurple);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 34 || i == 52 || i == 70 || i == 88 || i == 106 || i == 124) {
            elementButtons[i].setBackground(halogenPink);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 17 || i == 35 || i == 53 || i == 71 || i == 89 || i == 107 || i == 125) {
            elementButtons[i].setBackground(nobleGasOrange);
            elementButtons[i].setForeground(textWhite);
        } else if ((i > 55 && i < 66) || (i > 73 && i < 84) || (i > 92 && i < 102) || (i > 110 && i < 120)) {
            elementButtons[i].setBackground(transitionMetalBlue);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 92 || (i > 128 && i < 144)) {
            elementButtons[i].setBackground(lanthanideBlue);
            elementButtons[i].setForeground(textWhite);
        } else if (i == 110 || (i > 146 && i < 162)) {
            elementButtons[i].setBackground(actinideBlue);
            elementButtons[i].setForeground(textWhite);
        }
    }
    add(displayPanel, BorderLayout.CENTER);

}

0 个答案:

没有答案