java - 无法更改标题栏和 JMenuBar 的颜色

标签 java swing look-and-feel

我尝试修改 Windows 标题栏的颜色或 JMenuBar 的颜色。我通过 UIManager.put("XXXXXXX", Color.GREEN) 将每个外观键设置为 Color.GREEN,其中 XXXXX 只是一个占位符。大多数元素都会改变,但有些元素不会。例如,请参见图片。 enter image description here

红色的是我的问题(其他的也很奇怪,但还好)。

使用 netbeans gui 构建器构建 GUI(JFrame ->JMenuBar)

也许我应该提一下:

  1. 使用过 LAF“Windows”
  2. 使用的操作系统: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();
  }
}

您可以看到框架Frame Image

关于java - 无法更改标题栏和 JMenuBar 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200737/

相关文章:

java - 覆盖默认外观 Java

Java Swing JButton

java - 将文本字段添加到 JOptionPane

java - 某种外观和感觉是否保证可用?

java - 序列化对象的大小

java - 将框架设置为不可见时,为什么此代码会产生错误?

java - JTable 行标题外观

java - [学习EJB] : annotation type is not applicable to this kind of declaration?

java - 在java计算器程序中添加日志功能

java - spring - 为 JSESSIONID cookie 配置域