Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaGUI-从数组中获取随机值_Java_Arrays_User Interface_Random - Fatal编程技术网

JavaGUI-从数组中获取随机值

JavaGUI-从数组中获取随机值,java,arrays,user-interface,random,Java,Arrays,User Interface,Random,我有一个二维的按钮数组,还有一个图像数组。我想在按钮上获得图像,但我希望每次程序启动时图像都在随机按钮上。这样地:。现在,当我制作新的JButton时,通过改变图标的值,我只能在所有按钮上获得一种颜色。我想我需要做的是将Math.Random()设置为一个变量,并从图像数组中获取一个随机值,然后在我声明新的JButton时将该变量放入图标[],但我不知道这是否正确,也不知道如何操作。我进行了一些搜索,并尝试使用以下方法: var randomValue = icons[Math.floor(Ma

我有一个二维的按钮数组,还有一个图像数组。我想在按钮上获得图像,但我希望每次程序启动时图像都在随机按钮上。这样地:。现在,当我制作新的JButton时,通过改变图标的值,我只能在所有按钮上获得一种颜色。我想我需要做的是将
Math.Random()
设置为一个变量,并从图像数组中获取一个随机值,然后在我声明新的
JButton
时将该变量放入
图标[]
,但我不知道这是否正确,也不知道如何操作。我进行了一些搜索,并尝试使用以下方法:

var randomValue = icons[Math.floor(Math.random() * icons.length)];
但是我有一个错误,说

possible loss of precision, required int, found double.
非常感谢您的帮助。如果你想让我发布整个代码,让我知道

// 2D Array of buttons
buttons = new JButton[8][8];
    for(int row=0; row<8; row++) 
    {
        for (int col=0; col<8; col++) 
        {
            buttons[row][col] = new JButton(icons[0]);
            buttons[row][col].setLocation(6+col*70, 6+row*70);
            buttons[row][col].setSize(69,69);

            getContentPane().add(buttons[row][col]);
        }
    }

// Array of images
public static ImageIcon[] icons = {new ImageIcon("RedButton.png"),
                                   new ImageIcon("OrangeButton.png"),
                                   new ImageIcon("YellowButton.png"),
                                   new ImageIcon("GreenButton.png"),
                                   new ImageIcon("BlueButton.png"),
                                   new ImageIcon("LightGrayButton.png"),
                                   new ImageIcon("DarkGrayButton.png")};
//按钮的二维数组
按钮=新的JButton[8][8];

对于(int row=0;row,我将通过将所有图像图标放在一个ArrayList中,调用
java.util.Collections.shuffle(…)来大大简化这一过程
在ArrayList上,然后按顺序从无序排列的ArrayList中发出ImageIcons。或者,如果您的按钮允许重复图标,则使用java.util.Random变量,称为
Random
,只需调用
Random.nextInt(icons.length)
即可获得数组的随机索引

另一方面,为了您自己的利益,请不要使用空布局和绝对定位。您的JButtons网格正在请求使用JPanel.begging将其保存在网格布局中


另一方面,你为什么在同一个项目上发布问题,但使用不同的名称?你在这里的其他两篇文章中都有类似的帖子,但用户名不同:


在设置JButton上的图标之前,请使用此洗牌功能

public ImageIcon[] shuffle(ImageIcon[] icons)
{
    int index = 0;
    ImageIcon temp = 0;

    for(int i = icons.length -1; i > 0; i--)
    {
        index = r.nextInt(i + 1);
        temp = icons[index];
        icons[index] = icons[i];
        icons[i] = temp;
    }
    return icons;
}

尝试
randomValue=icons[(int)(Math.floor(Math.random()*icons.length))]
我之所以不使用arraylist,是因为这个程序是作业的一部分,它要求我们这样做。另外,我们被告知要使用空布局。顺便说一句,这些实际上不是我的帖子,我想其他人也在作业上遇到了问题。这实际上是我第一次使用这个网站,我今天刚刚创建了我的帐户。谢谢你的提示!