【发布时间】:2010-03-03 19:27:21
【问题描述】:
如何在网格框上随机填充颜色?
这里不是如图所示有序:
Grid http://www.freeimagehosting.net/uploads/4ed76557de.jpg
public class grid extends JPanel{
Label one = new Label();
Label two = new Label();
Label three = new Label();
Label four = new Label();
public static void main(String[] args){
JFrame jf=new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(new YAnswers());
jf.pack();
jf.setVisible(true);
}
grid (){
int rows=10; int cols=10;
setLayout(new GridLayout(rows,cols));
add(one); one.setBackground(Color.red);
add(two); two.setBackground(Color.orange);
add(three); three.setBackground(Color.green);
add(four); four.setBackground(Color.black);
boxes[] bx=new boxes[rows*cols];
for(int i=0;i<rows*cols;i++){
System.out.println("i"+i);
bx[i]=new boxes();
if(i%2<1)
bx[i].setColor(1);
add(bx[i]);
}
} //end grid()
}
【问题讨论】:
-
@Jessy:我已经编辑了您的问题以直接显示图像。您想要随机颜色,还是想要随机字段中的预定义颜色?
-
其实我的意思是随机的,就是填满颜色的盒子的位置,很抱歉造成混乱。
-
我之前编辑了您的问题以在问题中显示图像。为什么您再次删除了图片并提供了链接?
-
非常抱歉,我只是在图片上添加了标签,我的意思是有序和随机。
-
请检查我的更新答案是否能解决您的问题。