如何将 JPanel 绘制为 Nimbus JButton?
在 Nimbus 中,JButton 的外观和感觉非常整洁和准确,具有圆形边框和漂亮的背景。
我想渲染一个具有完全相同外观的 JPanel(显然它不会有按下状态等)。
我有什么选择?
In Nimbus look and feel JButtons have a very tidy and accurate look, with rounded border and nice background.
I'd like to render a JPanel with the very same look (obviously it won't have pressed state etc).
What are my options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 JPanel 上获得“按钮外观”的最简单方法可能是扩展
JPanel
并覆盖paintComponent
。这是 Nimbus
JButton
的外观:这是我的类似实现查看
JPanel
(我在周围添加了一个空边框来显示此示例,并且角不是半透明的):这是我的代码(使用 渐变 ):
更新
这是 AgostinoX 改进的
paintComponent
方法,它解决了我代码中的角落问题。另请参阅我对 如何隐藏箭头的回答JScrollBar 中的按钮,了解如何自定义 Nimbus 的外观。并查看 Nimbus 默认值了解颜色和画家。
The easiest way to get "Button look" on a JPanel is probably by extending the
JPanel
and overridepaintComponent
.Here is the Nimbus
JButton
look:And here is my implementation of a similar look on a
JPanel
(I added an empty border around for showing this example, and the corners are not translucent):Here is my code (using gradients):
UPDATE
Here is an improved
paintComponent
method by AgostinoX that solved the corner issue in my code.See also my answer to How to hide the arrow buttons in a JScrollBar on how you can customize the look and feel for Nimbus. And see the Nimbus defaults for colors and painters.