Nimbus Look And Feel调整菜单栏的颜色 [英] Nimbus Look And Feel adjust colors of menubar
问题描述
我正在尝试调整Nimbus外观和感觉的颜色,但它只是部分工作。特别是我在调整菜单栏的颜色时遇到了问题。
I am trying to adjust the colors of the Nimbus Look and Feel but it is only working partially. Especially I have problems adjusting the colors of the menubar.
这是一个运行的例子:
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 但是我无法改变菜单栏的颜色。
As you can see I am able to set background of the controls and set the foreground color of the JMenuItem. But I am not able to change the background of a JMenuItem, neither I am able to change the colors of the MenuBar. I tried a lot of keys from http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html but I was not able to change the color of the menubar.
另一个问题是?为什么我必须在设置外观之前调用一次颜色,在设置外观之后调用一次?为什么我必须调用一次'UIManager.put()'和'UIManager.getLookAndFeelDefaults()。put()'?
Another question is? Why do I have to call the adjusting of the colors once before setting the Look and Feel and once after setting the Look and Feel? And why do I have to call once 'UIManager.put()' and once 'UIManager.getLookAndFeelDefaults().put()'?
在我看来,Nimbus真的很麻烦,不适合专业用途。我尝试使用JDK 1.6.35和JDK 1.7.7,但是使用两个JDK我无法按照需要运行系统?
It seems to me that Nimbus is really buggy and not suitable for professional use. I tried to use both JDK 1.6.35 and JDK 1.7.7, but with both JDKs I could not get the system running as desired?
任何建议如何调整Nimbus LookAnd Feel中菜单栏的颜色?
Any suggestions how to adjust the colors of a menubar in Nimbus LookAnd Feel?
提前致谢
推荐答案
for JMenuBar
必须使用画家来检查< a href =http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/_nimbusDefaults.html#menubar =nofollow noreferrer> NimbusDefault#value
for JMenuBar
to have to use Painter, to check NimbusDefault#value
MenuBar[Enabled].backgroundPainter
MenuBar[Enabled].borderPainter
休息时间为 trashgod +1
这篇关于Nimbus Look And Feel调整菜单栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!