如何在Java GUI中设置按钮的背景颜色? [英] How to set background color of a button in Java GUI?

查看:108
本文介绍了如何在Java GUI中设置按钮的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是在特定pannel3上以gridlayout形式创建9个按钮的代码。我想要的是使每个按钮的背景为黑色,上面带有灰色文本。
有人可以帮忙吗?

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));
 }


推荐答案

退房 JButton 文档。请特别注意 setBackground setForeground JComponent继承的方法

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

类似于:

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);
}

这篇关于如何在Java GUI中设置按钮的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆