我将使用 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
)你必须定义 LayoutManager
,please read this thread
关于java - 如何在 JButton 上绘制彩色矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10333934/