你的后两个参数超出范围了,RGB三原色的范围均是0到255三个参数都用同样的方法赋值就可以啦: button.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));当然,用楼上所说的java.util.Random类的nextInt( 256 )可以达到同样效果。 Random rnd = new Random(); Color c = new Color( rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256) ); button.setBackground( c );
nextInt(int n)
返回一个伪随机数,它是从此随机数生成器的序列中取出的、在 0(包括)和指定值(不包括)之间均匀分布的 int值。很好使..
button.setBackground(new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)));当然,用楼上所说的java.util.Random类的nextInt( 256 )可以达到同样效果。
Random rnd = new Random();
Color c = new Color( rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256) );
button.setBackground( c );
也多谢1楼的朋友,还是网友多好啊,有问题发个贴子就能给解决了,
我的QQ是295727286有空一块讨论JAVA啊,