如何在Java GUI中设置按钮的背景颜色?

发布于 2024-10-02 10:40:08 字数 171 浏览 0 评论 0原文

下面的代码在特定的 pannel3 上以网格布局形式创建 9 个按钮。我想要的是将每个按钮的背景设为黑色,并在其上显示灰色文本。 有人可以帮忙吗?

 for(int i=1;i<=9;i++)
 {
     p3.add(new JButton(""+i));
 }

Below is the code which creates 9 buttons in gridlayout form on a specific pannel3. What i want is to make the background of each button black with grey text over it.
Can anyone help please?

 for(int i=1;i<=9;i++)
 {
     p3.add(new JButton(""+i));
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

双马尾 2024-10-09 10:40:08

查看 JButton 文档。请特别注意从 JComponent 继承的 setBackgroundsetForeground 方法。

像这样的东西:

for(int i=1;i<=9;i++)
{
    JButton btn = new JButton(String.valueOf(i));
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.GRAY);
    p3.add(btn);
}

Check out JButton documentation. Take special attention to setBackground and setForeground methods inherited from JComponent.

Something like:

for(int i=1;i<=9;i++)
{
    JButton btn = new JButton(String.valueOf(i));
    btn.setBackground(Color.BLACK);
    btn.setForeground(Color.GRAY);
    p3.add(btn);
}
生生漫 2024-10-09 10:40:08

简单:

btn.setBackground(Color.red);

要使用 RGB 值:

btn[i].setBackground(Color.RGBtoHSB(int, int, int, float[]));< /代码>

Simple:

btn.setBackground(Color.red);

To use RGB values:

btn[i].setBackground(Color.RGBtoHSB(int, int, int, float[]));

二智少女猫性小仙女 2024-10-09 10:40:08

更改背景属性可能还不够,因为组件看起来不再像按钮。您可能需要按照此处重新实现paint方法以获得更好的结果:

在此处输入图像描述

Changing the background property might not be enough as the component won't look like a button anymore. You might need to re-implement the paint method as in here to get a better result:

enter image description here

暮光沉寂 2024-10-09 10:40:08
for(int i=1;i<=9;i++) {
    p3.add(new JButton(""+i) {{
        // initialize the JButton directly
        setBackground(Color.BLACK);
        setForeground(Color.GRAY);
    }});
}
for(int i=1;i<=9;i++) {
    p3.add(new JButton(""+i) {{
        // initialize the JButton directly
        setBackground(Color.BLACK);
        setForeground(Color.GRAY);
    }});
}
满天都是小星星 2024-10-09 10:40:08

您可能需要也可能不需要使用 setOpaque 方法来通过将 true 传递给该方法来确保颜色显示。

You may or may not have to use setOpaque method to ensure that the colors show up by passing true to the method.

ぃ弥猫深巷。 2024-10-09 10:40:08

看来 setBackground() 方法在某些平台上效果不佳(我使用的是 Windows 7)。我找到了这个答案这个问题有帮助。但是,我并没有完全用它来解决我的问题。相反,我认为给按钮旁边的面板上色会更容易,而且几乎同样美观。

It seems that the setBackground() method doesn't work well on some platforms (I'm using Windows 7). I found this answer to this question helpful. However, I didn't entirely use it to solve my problem. Instead, I decided it'd be much easier and almost as aesthetic to color a panel next to the button.

很酷不放纵 2024-10-09 10:40:08

使用 setBackground 方法设置背景和 setForeground 更改文本的颜色。但请注意,将灰色文本放在黑色背景上可能会使文本有点难以阅读。

Use the setBackground method to set the background and setForeground to change the colour of your text. Note however, that putting grey text over a black background might make your text a bit tough to read.

日裸衫吸 2024-10-09 10:40:08

我尝试了以前的解决方案,但仍然无法改变颜色。看到另一篇文章并解决了我的问题。该按钮由不同的层组成。删除所有这些有助于:

    btn.setOpaque(true);
    btn.setContentAreaFilled(true);
    btn.setBorderPainted(false);
    btn.setFocusPainted(false);
    btn.setBackground(Color.GRAY); // for the background
    btn.setForeground(Color.white); // for the text

I tried the previous solutions but still couldn't change the color. Came across another article and solved my problem. The button is made of different layers. Removing all of them helps:

    btn.setOpaque(true);
    btn.setContentAreaFilled(true);
    btn.setBorderPainted(false);
    btn.setFocusPainted(false);
    btn.setBackground(Color.GRAY); // for the background
    btn.setForeground(Color.white); // for the text
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文