简体   繁体   中英

Adding Images on 2D JButton Array

I have a two dimensional JButton array and I want to add to each button a different image. These images are named 1.jpg, 2.jpg...

There are a total of 27 buttons (3x9 grid), so setting them manually would be tiresome. I tried it with an ImageIcon array and for-loops, but it doesn't work. Does somebody got an idea what's wrong with my code?

void createButtons() {
        JButton[][] buttons = new JButton[3][9];
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].setIcon(addImages());
            }
        }
    }

ImageIcon addImages() {

    ImageIcon[] images = new ImageIcon[27];
    for (int i = 0; i < images.length; i++) {
        images[i] = new ImageIcon(i + ".jpg");
        return images[i];
    }

You are mixing a method that should be used once to make all the images with another method to fetch the one image you need. Try this:

ImageIcon[] addImages() {
    ImageIcon[] images = new ImageIcon[27];
    for (int i = 0; i < images.length; i++) {
        images[i] = new ImageIcon(i + ".jpg");
    }
    return images;
}

Then you can call addImages() and reference the array:

void createButtons() {
        ImageIcon[] images = addImages();  // called once here
        JButton[][] buttons = new JButton[3][9];
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].setIcon(images[i * buttons[i].length + j]);  // then use it here
            }
        }
    }

You can set the image directly in the loop:

void createButtons() {
    JButton[][] buttons = new JButton[3][9];
    for (int i = 0; i < buttons.length; i++) {
        for (int j = 0; j < buttons[i].length; j++) {
            buttons[i][j] = new JButton();
            buttons[i][j].setIcon(new ImageIcon((i * 9 + j) + ".jpg"));
        }
    }
}

There is not need for a whole method for this (what's more, it's written incorrectly).

Edit: the problem with addImages()

Your addImages() method is iterated over, each time it creates a new ImageIcon[] which, if at all, should be created only once (outside of any loop). Inside, you start iterating over the array, but stop at the first iteration, i=0 , because your return images[0] . This method will always return the same value.

Your addImages method will always return a new Image Icon with 0.jpg.

You can do this instead:

void createButtons() {
        JButton[][] buttons = new JButton[3][9];
        int index = 1;  //start from 1.jpg
        for (int i = 0; i < buttons.length; i++) {
            for (int j = 0; j < buttons[i].length; j++) {
                buttons[i][j] = new JButton();
                buttons[i][j].setIcon(new ImageIcon( (index++) + ".jpg"));
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM