Swing JButton 不改变背景颜色 onclick
我希望单击按钮时不会更改背景颜色,默认情况下颜色将更改为浅蓝灰色。
所以这是我的代码,
JButton b = new JButton();
b.setBackground(SystemColor.control);
我希望按钮在单击时永远不会改变背景颜色。
I want the button on click it never change the background color, by default the color will change to light blue grey color.
So this is my code
JButton b = new JButton();
b.setBackground(SystemColor.control);
I want the button when on click it will never change the background color.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按钮的绘制取决于 LAF。您可以执行以下操作之一:
缺点:复杂,破坏 LAF。
JButton
并实现paintComponent()
。缺点:您需要自己绘制按钮的标签。
setContentAreaFilled(false)
完全删除按钮的背景绘制,然后在不透明面板中添加具有所需背景颜色的按钮。优点:按钮不变,LAF没有损坏。
缺点:您需要为每个按钮创建一个面板,并调整它以完全适合按钮,这可能相当困难,具体取决于布局。
The painting of the button depends on LAF. You can do one of the following:
Disadvantages: complex, breaks LAF.
JButton
and implementpaintComponent()
.Disadvantages: You will need to paint button's label yourself.
setContentAreaFilled(false)
and then add the button in the opaque panel with the needed background color.Advantages: button is unchanged, LAF isn't broken.
Disadvantages: you need to create a panel for each button, and adjust it to fit the button exactly, which can be quite difficult, depending on the layout.
您可以修改用于背景和前景的颜色。如何做到这一点取决于您使用的外观和感觉。
如果您使用 Nimbus,此处有一个颜色列表,以及这里是一篇关于如何更改颜色的文章。 这里是另一篇可能有帮助的文章。
You could modify the colors that is used for the background and foreground. How you do it depends on what Look And Feel you are using.
If you are using Nimbus, there is a list of colors here, and here is an article about how you change the colors. And here is another article that can be helpful.