Sal*_*lar 17 java swing jbutton
下面是在特定pannel3上以gridlayout形式创建9个按钮的代码.我想要的是使每个按钮的背景为黑色,上面有灰色文字.有人可以帮忙吗?
for(int i=1;i<=9;i++)
{
p3.add(new JButton(""+i));
}
Run Code Online (Sandbox Code Playgroud)
Pab*_*ruz 22
查看JButton文档.特别注意setBackground
和setForeground
继承的方法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);
}
Run Code Online (Sandbox Code Playgroud)
Ali*_*adi 12
简单:
btn.setBackground(Color.red);
要使用RGB值:
btn[i].setBackground(Color.RGBtoHSB(int, int, int, float[]));