更改 JRadioButtonMenuItem 的选定点颜色

2022-01-21 00:00:00 radio-button menuitem java swing

我正在开发我的个人 Java 聊天客户端,它的一个功能是设置用户状态(可用、不可见、忙碌).为了使其用户友好,我将这些状态放入带有 JRadioButtonMenuItemJMenu 中.

I'm working on my personal Java chat client whose one feature is setting user's status (Available, Invisible, Busy). To make it user-friendly, I put those statuses into a JMenu with JRadioButtonMenuItem.

问题是我希望每个状态 RadioButton 都有自己的单选点颜色(或点图标).例如:

The problem is I want each status RadioButton to have its own radio-dot color (or dot-icon). For example:

  • [绿点] 可用
  • [红点] 忙碌
  • [灰点] 不可见.

我想用三个不同的自定义 RadioButtonMenuItem 扩展 JRadioButtonMenuItem,但不明白 JRadioButtonMenuItem 是如何绘制的.

I thought of extending the JRadioButtonMenuItem with three different custom RadioButtonMenuItem, but couldn't understand how JRadioButtonMenuItem is painted.

谁能帮我解决这个问题?

Could anyone help me to solve this problem?

编辑 1
感谢您建议将 IconsetIcon()setSelectedIcon() 方法一起使用.
但是,由于我的问题是关于更改单选点,您能否帮我隐藏 RadioButton 中的单选点?

Edit 1
Thanks for your suggestions to use Icon together with setIcon() and setSelectedIcon() methods.
However since my question is about changing the radio-dot, could you also help me to hide the radio-dot from a RadioButton?

编辑 2
这是我的应用程序的当前屏幕截图.

正如您所看到的那样, RadioButtonMenuItem 之前的点在某种程度上是荒谬的废话.这就是为什么我想摆脱点,或者将其更改为我的自定义图标:)

Edit 2
Here's the current screenshot of my app.

As you can see the dot before that RadioButtonMenuItem is somehow ridiculously nonsense. That's why I want to get rid of the dot, or change it to my custom icon :)

推荐答案

单选按钮的圆点由 UI 委托为每个 Look &感觉.您可以提供自己的 BasicRadioButtonUI,但付出的努力并非微不足道.作为替代方案,实现 Icon 接口,如 here in ColorIcon.

The radio button's dot is rendered by the UI delegate for each Look & Feel. You can supply your own BasicRadioButtonUI, but the effort is not trivial. As an alternative, implement the Icon interface, as shown here in ColorIcon.

相关文章