java - 如何在 JButton 上绘制彩色矩形?

标签 java swing jbutton

我将使用 Swing UI 增强应用程序,以允许用户选择颜色,这样他们就不会被默认颜色选择所困。

It is common for other applications to have shaded rectangles drawn on each button that activates a color selector, with the rectangle's color changing accordingly when a new color is selected.我试图通过在按钮上放置一个带有选定颜色的小 JPanel 来实现相同的效果,但这会导致按钮中间出现一个小方 block ,而不是填充按钮的大部分表面。

我认为另一种方法是动态生成带有颜色的矩形图标,然后将适当的图标添加到每个按钮,但肯定有更简单的方法吗?

最佳答案

JButton.setIcon 和预期的 Rectangle 放在那里,example

编辑

I am trying to achieve the same effect by placing a small JPanel with the selected color on the button, but that results in a tiny square in the middle of the button, instead of filling most of the surface of the button.

只有 JFrame (BorderLayout)JPanel (FlowLayout) 已经预先实现了 LayoutManager,其余的 JComponents(将一个 JComponent 添加到另一个 JComponent)你必须定义 LayoutManagerplease read this thread

关于java - 如何在 JButton 上绘制彩色矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333934/

相关文章:

java - 在 java 中绘制图形 - NetBeans IDE

java - JTextArea 在 JFrame 边界截断

java - 按钮尺寸定为迷你

java swing - 如何改变窗口背景

java - JButton 对 actionListener 无响应

java - 具有单独链接的 HashMap

java - 使用 ReSTLet 和 GAE 过滤 ROOT 路径

java - 我可以在程序的不同部分重复使用 JLabel 组件吗?

java - Ambari-metrics-collector 服务未启动

java - 在 Mac Android Studio 上生成 JavaDoc 时出错