Swing JButton 不改变背景颜色 onclick

发布于 2024-09-08 11:59:28 字数 178 浏览 1 评论 0原文

我希望单击按钮时不会更改背景颜色,默认情况下颜色将更改为浅蓝灰色。

所以这是我的代码,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远山浅 2024-09-15 11:59:28

按钮的绘制取决于 LAF。您可以执行以下操作之一:

  1. 定义您自己的 UI 委托并将其分配给按钮。
    缺点:复杂,破坏 LAF。
  2. 扩展JButton并实现paintComponent()
    缺点:您需要自己绘制按钮的标签。
  3. 通过调用 setContentAreaFilled(false) 完全删除按钮的背景绘制,然后在不透明面板中添加具有所需背景颜色的按钮。
    优点:按钮不变,LAF没有损坏。
    缺点:您需要为每个按钮创建一个面板,并调整它以完全适合按钮,这可能相当困难,具体取决于布局。

The painting of the button depends on LAF. You can do one of the following:

  1. Define your own UI delegate and assign it to the button.
    Disadvantages: complex, breaks LAF.
  2. Extend JButton and implement paintComponent().
    Disadvantages: You will need to paint button's label yourself.
  3. Remove the button's background painting altogether by invoking 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.
鹿! 2024-09-15 11:59:28

您可以修改用于背景和前景的颜色。如何做到这一点取决于您使用的外观和感觉。

如果您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文