gpt4 book ai didi

java - Nimbus Look And Feel 调整菜单栏的颜色

转载 作者:搜寻专家 更新时间:2023-11-01 01:45:23 24 4
gpt4 key购买 nike

我正在尝试调整 Nimbus 外观的颜色,但它只是部分起作用。特别是我在调整菜单栏的颜色时遇到了问题。

这是一个运行示例:

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.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;

public class JMenuColorTest extends JFrame {

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {
try {
adjustLAF();
} catch (Exception e) {
e.printStackTrace();
}

JMenuColorTest test = new JMenuColorTest();
test.setDefaultCloseOperation(EXIT_ON_CLOSE);
test.setPreferredSize(new Dimension(400, 300));
test.pack();
test.setLocationRelativeTo(null);

JMenuBar menuBar = new JMenuBar();
JMenu menu1 = new JMenu("Menu 1");
menu1.add(new JMenuItem("Item 1.1"));
menu1.add(new JMenuItem("Item 1.2"));
menu1.add(new JMenuItem("Item 1.3"));
menuBar.add(menu1);
JMenu menu2 = new JMenu("Menu 2");
menu2.add(new JMenuItem("Item 2.1"));
menu2.add(new JMenuItem("Item 2.2"));
menu2.add(new JMenuItem("Item 2.3"));
menuBar.add(menu2);
test.setJMenuBar(menuBar);

test.setVisible(true);
}

private void adjustLAF() throws ClassNotFoundException,
InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {

// Working
UIManager.put("control", Color.GREEN);

// Not working
UIManager.getLookAndFeelDefaults().put(
"MenuItem[Enabled].textForeground", Color.RED);

// Set the look and feel
UIManager.setLookAndFeel(info.getClassName());

// Not working
UIManager.put("control", Color.GREEN);

// Working
UIManager.getLookAndFeelDefaults().put(
"MenuItem[Enabled].textForeground", Color.RED);

break;
}
}

}
});
}
}

如您所见,我能够设置控件的背景并设置 JMenuItem 的前景色。但是我无法更改 JMenuItem 的背景,也无法更改 MenuBar 的颜色。我尝试了很多来自 http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html 的键但我无法更改菜单栏的颜色。

另一个问题是?为什么我必须在设置 Look and Feel 之前和设置 Look and Feel 之后调用一次颜色调整?为什么我必须调用一次“UIManager.put()”和一次“UIManager.getLookAndFeelDefaults().put()”?

在我看来,Nimbus 确实有问题,不适合专业使用。我尝试同时使用 JDK 1.6.35 和 JDK 1.7.7,但是对于这两个 JDK,我无法让系统按预期运行?

关于如何在 Nimbus LookAnd Feel 中调整菜单栏的颜色有什么建议吗?

提前致谢

最佳答案

JMenuBar 必须使用 Painter , 检查 NimbusDefault#value

MenuBar[Enabled].backgroundPainter
MenuBar[Enabled].borderPainter

休息由 trashgod +1 回答

关于java - Nimbus Look And Feel 调整菜单栏的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12369783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com