透明 JButton 仍在绘制其背景
我有一个半透明 JPanel。我通过扩展 JButton 创建了一个自定义 JButton,因为我需要一个带有圆角的按钮并希望为其添加一些效果。我已将按钮设置为非透明。当我将此按钮添加到半透明 JPanel 时,它看起来很好。但在翻转时,按钮后面会涂上一个黑色补丁,看起来真的很糟糕。我在网上搜索了解决方案,但找不到有用的解决方案。 http://www.java.net/node/661798 也描述了此问题,但我无法真正使 kirillcool 的建议得以实现......任何帮助将不胜感激
I have a translucent JPanel. I have created a custom JButton by extending JButton as I required a button with rounded corners and wanted to add some effects to it. I have made the button non-opaque. When I add this button to my translucent JPanel it apears fine. But on rollover a black patch is painted behind the button which looks really crappy. I searched the net for a solution but could'nt find a useful one . This problem is also described at http://www.java.net/node/661798 but i was not able really make kirillcool's suggestion work out.....Any help will be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要添加:
I believe you need to add:
不确定是否有人仍然感兴趣...
您可以通过重写
paintComponent()
方法来解决该问题,让 Java 以您喜欢的任何形状绘制JButton
。您只需使用 setBackground() 方法将 Graphics 对象的背景设置为透明即可。您还需要在使用clearRect()
方法绘制之前清除 Graphics 对象,然后使用JButton
背景的 alpha 级别再次填充它。这是我的一段代码..它显示了重写的paintComponent()
。通过将其粘贴到您的JButton
中,您应该得到一个带有圆角边缘的JButton
,即使它位于半透明背景上not sure if someone is still interested...
you can fix the problem by overriding the
paintComponent()
method to let Java draw theJButton
in any Shape you like. you just need to set the background of the Graphics object to transparent withsetBackground()
method. also you need to clear the Graphics object BEFORE drawing on it withclearRect()
method and then fill it again with the alpha level of the background of yourJButton
. here is my piece of code.. it shows the overridenpaintComponent()
. by pasting it into yourJButton
you should get aJButton
with rounded edges even if its on semi-transparant background