Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在单元格边界内显示图像_Java_Image_Gridbaglayout - Fatal编程技术网

Java 如何在单元格边界内显示图像

Java 如何在单元格边界内显示图像,java,image,gridbaglayout,Java,Image,Gridbaglayout,如何在单元格边界内放置图像 我是说不占用其他牢房的空间? 在下面的代码中,选择了随机单元来显示图像。一个单元格中有一个图像。 问题是,这张图像似乎也拍摄了其他细胞。 。。。 设置首选尺寸(新尺寸(600600)); 最终整数行=6; 最终整数COLS=6; 最终整型图像=10; setLayout(新的GridBagLayout()); GridBagConstraints gc=新的GridBagConstraints(); gc.weightx=1d; gc.weighty=1d; gc.

如何在单元格边界内放置图像

我是说不占用其他牢房的空间? 在下面的代码中,选择了随机单元来显示图像。一个单元格中有一个图像。 问题是,这张图像似乎也拍摄了其他细胞。

。。。
设置首选尺寸(新尺寸(600600));
最终整数行=6;
最终整数COLS=6;
最终整型图像=10;
setLayout(新的GridBagLayout());
GridBagConstraints gc=新的GridBagConstraints();
gc.weightx=1d;
gc.weighty=1d;
gc.insets=新的insets(0,0,0,0)//顶部、左侧、底部和右侧
gc.fill=GridBagConstraints.NONE;
JLabel[][]标签=新JLabel[行][COLS];
Random rand=新的Random();
//用标签填充面板

对于(int i=0;i这是什么类型的问题?你有大约5个关于同一主题的问题。你发布的随机代码是无用的,除非人们已经阅读了之前的4个问题,并且对你正在做的事情有所了解。我知道我已经阅读了它们,但仍然不理解

单元格没有边界。当您向单元格中添加组件时,单元格的大小将与您添加的组件的大小相同。由于您只有少数占用的单元格,因此大多数单元格没有实际大小

因此,假设您想在位置(3,4)处为未占用的单元添加一个新组件例如,我想您需要查看第3行上的所有单元格以确定该行的最大高度。然后您需要查看第4列中的所有单元格以确定最大宽度。将这两个单元格组合起来,就可以得到单元格的大小

您已经知道如何获取约束,因此应该能够理解其余的约束

再说一次,我可能不知道你在做什么,因此我的答案只是一个大漫谈


如果您需要更多帮助,请发布。

这是什么类型的问题?您有大约5个关于同一主题的问题。除非人们已经阅读了之前的4个问题,并且对您正在做的事情有所了解,否则您发布的随机代码是无用的。我知道我已经阅读了这些问题,但仍然不理解

单元格没有边界。当您向单元格中添加组件时,单元格的大小将与您添加的组件的大小相同。由于您只有少数占用的单元格,因此大多数单元格没有实际大小

因此,假设您想在位置(3,4)处为未占用的单元添加一个新组件例如,我想您需要查看第3行上的所有单元格以确定该行的最大高度。然后您需要查看第4列中的所有单元格以确定最大宽度。将这两个单元格组合起来,就可以得到单元格的大小

您已经知道如何获取约束,因此应该能够理解其余的约束

再说一次,我可能不知道你在做什么,因此我的答案只是一个大漫谈


如果您需要更多帮助发布您的。

回忆您关于基本相同代码的内容,请尝试将您的
ImageIcon
添加到
JLabel
中。

回忆您关于基本相同代码的内容,尝试将您的
ImageIcon
添加到
JLabel
中。

您想拥有相同代码的所有图像吗ize?否,我随机缩放图像并随机选择单元格。是否希望所有图像大小相同?否,我随机缩放图像并随机选择单元格。
...
setPreferredSize(new Dimension(600,600)); 
final int ROWS = 6;
final int COLS = 6;
final int IMAGES = 10;

setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.weightx = 1d; 
gc.weighty = 1d;
gc.insets = new Insets(0, 0, 0, 0);//top, left, bottom, and right 
gc.fill = GridBagConstraints.NONE; 

JLabel[][] label = new JLabel[ROWS][COLS];          
Random rand = new Random();

// fill the panel with labels
for (int i=0;i<IMAGES;i++){
   ImageIcon icon = createImageIcon("myImage.jpg");
   int r, c;
   do{    
      //pick random cell which is empty to avoid overlap image in the same cell
     r = (int)Math.floor(Math.random() * ROWS);
     c = (int)Math.floor(Math.random() * COLS); 
   } while (label[r][c]!=null); 

   //scale the image                            
   int x = rand.nextInt(20)+30;
   int y = rand.nextInt(20)+30;                     
   Image image = icon.getImage().getScaledInstance(x,y, Image.SCALE_SMOOTH);
   icon.setImage(image);

   JLabel lbl = new JLabel(icon); 
   gc.gridx = r;
   gc.gridy = c;            
   add(lbl, gc); //add image to the cell        
   label[r][c] = lbl; 
}