jPopUpMenu 更改鼠标悬停背景

发布于 2024-12-19 02:48:23 字数 165 浏览 7 评论 0原文

我想在翻转事件时更改 jMenuItems 的背景颜色:

在此处输入图像描述

现在它是蓝色的,我想要白色的,我该怎么办? (我使用netbeans GUI)

I would like to change the background color of my jMenuItems on a rollover event :

enter image description here

Now it's blue, I want it white, how can I do ? (I use netbeans GUI)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

£冰雨忧蓝° 2024-12-26 02:48:23

您可以使用类似以下内容:

UIManager.put("MenuItem.selectionBackground", Color.WHITE);

问题是这将更改所有菜单项的颜色,甚至是添加到 JMenu 的菜单项。

要控制更改哪些菜单项,您需要创建自定义 MenuItem UI 来替换默认值。然后,您需要修改代码以使用自定义选择背景颜色。

You can use something like:

UIManager.put("MenuItem.selectionBackground", Color.WHITE);

Problem is this will change the color for all menu items, even those added to a JMenu.

To control which menu items are changed you will need to create a custom MenuItem UI to replace the default. You will then need to modify the code to use your custom selection background color.

伪装你 2024-12-26 02:48:23

我希望能够在应用程序的不同部分使用不同的颜色。我没有使用 UIManager 来更改 LookAndFeel,而是扩展了 javax.swing.plaf.basic.BasicMenuItemUI:-

public class CustomMenuUI extends BasicMenuItemUI {
    public CustomMenuUI(Color color){
        super.selectionBackground = color;
    }
}

然后您只需为 JMenuItem 设置 UI:-

CustomMenuUI menuUI = new CustomMenuUI(Color.WHITE);
jMenuItem.setUI(menuUI);

I wanted to be able to use different colors in different parts of an app. Rather than use UIManager to change the LookAndFeel, I extended javax.swing.plaf.basic.BasicMenuItemUI:-

public class CustomMenuUI extends BasicMenuItemUI {
    public CustomMenuUI(Color color){
        super.selectionBackground = color;
    }
}

Then you just need to set the UI for your JMenuItem:-

CustomMenuUI menuUI = new CustomMenuUI(Color.WHITE);
jMenuItem.setUI(menuUI);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文