简体   繁体   English

我想在我的jpanel单元格中显示图片,但是我的代码无法正常工作

[英]i want to display a picture in my jpanel cells and my code is not working

public static JPanel[][] creationGrille(JPanel content) throws IOException{ 公共静态JPanel [] [] creationGrille(JPanel内容)抛出IOException {

    ImageIcon monImage = new ImageIcon("src\\images\\tree.png");
    JLabel imageLab = new JLabel();
    imageLab.setIcon(monImage);
     //JPanel affichage Grille
    JPanel contentGrille = new JPanel();
    //On définit le layout à utiliser pour la grille
    contentGrille.setLayout(new GridLayout(14, 14));//14 lignes, 14 colonnes
    content.add(contentGrille,BorderLayout.CENTER);
    JPanel cell[][]= new JPanel[14][14];
    for(int i=0; i<cell.length; i++){
        for(int j=0; j<cell.length; j++){
            cell[i][j]= new JPanel();
            cell[i][j].setSize(new Dimension(50, 50));
            if (i == 5 || i ==6 || i==7 || i==8 || j==5 || j==6 || j==7 || j==8) {
                cell[i][j].setBackground(Color.gray);
                 if((i<5 && j==6) || (i>8 && j==6)){
                    cell[i][j].setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.white));  // top, left,bottom,right, color
                 }else if((i==6 && j<5) || (i==6 && j>8)){
                    cell[i][j].setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.white));
                 }
            }else{
                cell[i][j].add(imageLab);
                //cell[i][j].setBackground(Color.white);
            }


            contentGrille.add(cell[i][j]);
        }
    }
    return cell;
} 

i take each cell and i used my imageIcon to dipaly it in the cell ? 我把每个单元格都用了,我用imageIcon将它放在了单元格里? ?

一个组件只能属于一个父/容器,因此您需要为每个要将图像添加到的面板创建一个新的JLabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想使用jbutton显示从mysql到jtextfield的特殊字符。我的代码仅适用于数字 - i want to display special characters from mysql to jtextfield using jbutton.my code is working only numbers 我想在菜单中添加图片 - I want to add a picture to an item in my menu 为什么我的 window 不显示我在 JPanel 中绘制的图像? - Why does my window not display the image I drew in my JPanel? 我想用Jtable Java为Cell着色吗? - i want to color my Cells in Jtable Java? 我想在我的JPanel中通过xuggle实现播放的视频窗口? - i want implement played video window by xuggle inside A my JPanel? 为什么我的代码没有在JPanel中显示图像? - Why doesn't my code display an image in a JPanel? 我的JPanel程序无法正常运行 - My JPanel program not working successfully 为什么我的BorderLayout无法与我的JPanel一起使用? - Why is my BorderLayout not working with my JPanel? 我想用我的 JPanel 在 Java JFrame 中为我的游戏做简单的 animation - I want to do simple animation with my JPanel in Java JFrame for my Game 我想使用 gridbag 布局在我的代码中添加单独的面板(使用 JPanel)。任何人都可以建议我使用正确的代码。提前致谢 - I want to add separate panels (using JPanel) in my code using gridbag layout .Can anyone one suggest me with the right code.Thanks in advance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM