我尝试修改 Windows 标题栏的颜色或 JMenuBar 的颜色。我通过 UIManager.put("XXXXXXX", Color.GREEN) 将每个外观键设置为 Color.GREEN,其中 XXXXX 只是一个占位符。大多数元素都会改变,但有些元素不会。例如,请参见图片。
红色的是我的问题(其他的也很奇怪,但还好)。
使用 netbeans gui 构建器构建 GUI(JFrame ->JMenuBar)
也许我应该提一下:
- 使用过 LAF“Windows”
- 使用的操作系统:Windows 10
解决方案更新 - 临时 好吧,我现在有一些东西可以工作(不能测试太多,稍后会做)。 我必须在更改为 Windows LAF 之前创建 JFrame,这会产生像 @bhavna garg 和 @Ganesh Patel 这样的框架,然后我将 LAF 更改为 Windows,所有其他元素看起来就像我想要的那样。颜色不正确,我认为这不是一个可行的解决方案,但我稍后会检查
最佳答案
我更喜欢你可以使用金属外观和感觉,我可以更改标题栏的颜色以及更改菜单栏和菜单的颜色。
这是代码:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRootPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.DefaultMetalTheme;
import javax.swing.plaf.metal.MetalLookAndFeel;
public class MyLookAndFeel {
JFrame frame;
JMenuBar menubar;
MetalLookAndFeel metal;
JMenu menu;
public MyLookAndFeel() {
metal = new MetalLookAndFeel();
metal.setCurrentTheme(new MetalTheme());
try {
UIManager.setLookAndFeel(metal);
}
catch(UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
frame = new JFrame("Hello");
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
menubar = new JMenuBar();
menubar.setOpaque(true);
menubar.setBackground(Color.green);
menu = new JMenu("File");
menubar.add(menu);
frame.setJMenuBar(menubar);
frame.setVisible(true);
frame.setSize(100,100);
}
public class MetalTheme extends DefaultMetalTheme {
@Override
public ColorUIResource getMenuBackground() {
return new ColorUIResource(Color.GREEN);
}
public ColorUIResource getWindowTitleBackground() {
return new ColorUIResource(java.awt.Color.green);
}
}
public static void main(String args[]) {
new MyLookAndFeel();
}
}
关于java - 无法更改标题栏和 JMenuBar 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200737/