简体   繁体   中英

Unable to add image to specific cell grid for JPanel GridLayout

I have a JPanel which has a GridLayout of (5,7 ) and I am trying to add an image to a specific grid cell eg : (3,1) . On researching , I realised there is a way and followed it.

Compilable source code

package testing;

import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

public class Testing extends JPanel {

    public static class TestPanel extends JPanel {
        TestPanel() {
            int i = 5; // num of rows
            int j = 7; // num of cols

            PanelHolder = new JPanel[i][j];
            setLayout(new GridLayout(i, j));

            for (int m = 0; m < i; m++) {
                for (int n = 0; n < j; n++) {
                    PanelHolder[m][n] = new JPanel();
                    add(PanelHolder[m][n]);
                }
            }

            File file = new File("./src/testing/Ace_Club_1_1.png");
            File file2 = new File("./src/testing/Ace_Diamond_1_1.png");

            try {
                image = ImageIO.read(file);
                image2= ImageIO.read(file2);
            } catch (IOException ie) {

            }

            ImagePanel ip = new ImagePanel(image);
            PanelHolder[3][1].add(ip); // doesnt add according to cell grid coordinates
            ImagePanel ip2 = new ImagePanel(image2);
             PanelHolder[3][3].add(ip2);

        }

        JPanel[][] PanelHolder;

        JLabel DeckLabel;
        JPanel DeckPanel;
        ImageIcon Deckimg;

        private BufferedImage image;
        BufferedImage image2;

    }


    public static class ImagePanel extends JPanel {
        ImagePanel(BufferedImage image) {
            this.i = image;

        }


        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(i, 0, 0, getWidth(), getHeight(), this);

        }

        BufferedImage i;
    }

    public static void main(String[] args) {
        TestPanel tp = new TestPanel();

        JFrame frame = new JFrame();

        frame.add(tp);
        frame.pack();
        frame.setVisible(true);

    }

}

For some unknown reason , I cant add my image to a specific cell grid and it appears in the wrong grid cell , i also cant add more than 1 image.

You can download the images from here

EDIT: edited the source code to include Andrew Thompson answer to add more than 1 image but still doesnt work

This is the output after adding the second image on the advice of Andrew Thompson

在此处输入图片说明

ImagePanel ip = new ImagePanel(image);
PanelHolder[3][1].add(ip); // doesnt add according to cell grid coordinates
PanelHolder[3][3].add(ip); // only 1 image appears 

1 component can only appear in one place in a GUI. If you need to display 2, create 2.

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