I am creating a game at the moment, it consists of a 2D array full of buttons with a gridLayout of (7,7)
ImageIcon myImage = new ImageIcon("C:\\Temp\\Image1.gif"); JButton[][] button = new JButton[7][7]; this.setLayout(new GridLayout(7, 7)); for(int i = 0; i < 7; i++) { for(int j = 0; j < 7; j++) { //button[i][j].addActionListener(this); button[i][j] = new JButton(); add(button[i][j]); } }
At the moment this fills up my array with blank buttons.
I want to add an image to a specific location in my array, example [1][1].
I have tried underneath the for loop:
But it doesn't display the image in the correct button ?button[1][1].setIcon(peg); add(button[1][1]);
Is there an easier way to add these images?
I have also tried :
ImageIcon myImage = new ImageIcon("C:\\Temp\\Image1.gif"); JButton[][] button = new JButton[7][7]; this.setLayout(new GridLayout(7, 7)); for(int i = 0; i < 7; i++) { for(int j = 0; j < 7; j++) { //button[i][j].addActionListener(this); button[i][j] = new JButton(myImage); add(button[i][j]); } }
This will fill up my array of buttons with the images, so how would I go about setting a button say in position [1][1], to be blank?
Thank you