在Java中更改颜色选择器按钮的背景颜色

在Java中更改颜色选择器按钮的背景颜色

本文介绍了在Java中更改颜色选择器按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java按钮,可以打开JColorChooser对话框。我希望按钮的颜色根据对话框中选择的颜色进行更改。我试过调用按钮的setBackgroundColor()方法,但在我的情况下它没有效果(按钮在Winsows 7中的JToolBar上)。另外,问题建议更改按钮的背景不是平台独立的。

I have a Java button that opens a JColorChooser dialog. I would like for the color of the button to change according to the color selected in the dialog. I have tried calling the setBackgroundColor() method of the button, but in my case it has no effect (the button is on a JToolBar in Winsows 7). Also, this question suggests that changing a button's background is not platform independent.

这个问题的答案可能不是首先使用按钮。所以,我的问题是:是否有一种方法可以在Java中显示一个反映拾取颜色的颜色选择器控件?有点像MS Paint将在填充颜色按钮上显示拾取的颜色。

Maybe the answer to this problem is not to use a button in the first place. So, my question is: is there a method of showing a "color picker" control in Java that reflects the picked color? Kind of like how MS Paint would show the picked color on the "fill with color" button.

推荐答案

您有以下几种选择:


  • 更改相关组件的背景,如@ mKorbel的。

按下,如@Robin所建议。

Make the button opaque, as suggested by @Robin here.

修改包含按钮的面板的背景,如图所示。

Modify the background of a panel containing the button, as shown here.

实施图标界面并将实例应用于您的按钮(或附近),如所示。

Implement the Icon interface and apply an instance to (or near) your button, as shown here.

这篇关于在Java中更改颜色选择器按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:48