2d 배열의 단추가 있고 일련의 이미지가 있습니다. 단추에 이미지를 표시하고 싶지만 프로그램을 시작할 때마다 이미지가 임의의 단추에 있도록하고 싶습니다. 이처럼 : What I want it to look like. 지금은 새로운 JButton을 만들 때 아이콘의 값을 변경하여 모든 버튼에 하나의 색상 만 표시 할 수 있습니다. 내가 뭘해야한다고 생각 Math.Random()
변수로 설정하고 이미지의 배열에서 임의의 값을 얻으려면 icons[]
때 내가 새 JButton
을 선언하지만 이것이 옳고 돈이 있는지 모르겠 변수를 넣어 그것을하는 방법을 모른다. 좀 검색을했고, 이것을 사용하여 시도 :Java GUI - 배열에서 무작위 값 얻기
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")};
'randomValue = icons [(int) (Math.random() * icons.length))]' – exception1