更改其中的JMenuBar和JMenu对象的背景和文本颜色 [英] Change background and text color of JMenuBar and JMenu objects inside it

查看:823
本文介绍了更改其中的JMenuBar和JMenu对象的背景和文本颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 JMenuBar JMenu 内的对象设置自定义背景颜色?我试过 .setBackgroundColor 但它不起作用!

How can I set custom background color for JMenuBar and JMenu objects inside it? I tried .setBackgroundColor and it does not work!

推荐答案

创建一个扩展的新类 JMenuBar

public class BackgroundMenuBar extends JMenuBar {
    Color bgColor=Color.WHITE;

    public void setColor(Color color) {
        bgColor=color;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(bgColor);
        g2d.fillRect(0, 0, getWidth() - 1, getHeight() - 1);

    }
}

现在你使用这个类而不是 JMenuBar 并使用 setColor()设置背景颜色。

Now you use this class instead of JMenuBar and set the background color with setColor().

这篇关于更改其中的JMenuBar和JMenu对象的背景和文本颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆