[英]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.