如何获取Gridlayout的坐标

发布于 2024-08-25 07:32:18 字数 1476 浏览 2 评论 0原文

我将 JPanel 设置为 GridLayout (6,6),尺寸为 (600,600) 网格的每个单元格将显示一张具有不同宽度和高度的图片。 图片首先添加到JLabel,然后将JLabel添加到单元格。

如何检索单元格中图片的坐标而不是单元格的坐标?到目前为止,即使在屏幕上显示不同尺寸的图片,也给出了这些高度和宽度相等的坐标。

例如,

java.awt.Rectangle[x=100,y=100,width=100,height=100]
java.awt.Rectangle[x=200,y=100,width=100,height=100]
java.awt.Rectangle[x=300,y=100,width=100,height=100]

我使用GridLayout而不是gridBagLayout的原因是,我希望每个图片都有边界。如果我使用GridBagLayout,网格将根据图片大小扩展。 我希望网格大小为固定大小。

JPanel pDraw = new JPanel(new GridLayout(6,6));
pDraw.setPreferredSize(new Dimension(600,600));

for (int i =0; i<(6*6); i++)
{           
   //get random number for height and width of the image
   int x = rand.nextInt(40)+(50);
   int y = rand.nextInt(40)+(50);   

   ImageIcon icon = createImageIcon("bird.jpg");    

   //rescale the image according to the size selected
   Image img = icon.getImage().getScaledInstance(x,y,img.SCALE_SMOOTH);         
   icon.setImage(img ); 
   JLabel label = new JLabel(icon);
   pDraw.add(label);    
}

for(Component component:components)
{
   //retrieve the coordinate
   System.out.println(component.getBounds());
}

编辑:我已经尝试过这个但不起作用:-(

for(Component component: pDraw.getComponents()){
  System.out.println(((JLabel)component).getIcon());
}

我怎样才能得到这样的输出?

java.awt.Rectangle[x=300,y=100,width=50,height=40]
java.awt.Rectangle[x=400,y=400,width=60,height=50]

I set my JPanel to GridLayout (6,6), with dimension (600,600)
Each cell of the grid will display one pictures with different widths and heights.
The picture first add to a JLabel, and the JLabel then added to the cells.

How can retrieved the coordinate of the pictures in the cells and not the coordinate of cells? So far the out give these coordinate which equal height and width even on screen the pictures showed in different sizes.

e.g.

java.awt.Rectangle[x=100,y=100,width=100,height=100]
java.awt.Rectangle[x=200,y=100,width=100,height=100]
java.awt.Rectangle[x=300,y=100,width=100,height=100]

The reason why I used GridLayout instead of gridBagLayout is that, I want each pictures to have boundary. If I use GridBagLayout, the grid will expand according to the picture size.
I want grid size to be in fix size.

JPanel pDraw = new JPanel(new GridLayout(6,6));
pDraw.setPreferredSize(new Dimension(600,600));

for (int i =0; i<(6*6); i++)
{           
   //get random number for height and width of the image
   int x = rand.nextInt(40)+(50);
   int y = rand.nextInt(40)+(50);   

   ImageIcon icon = createImageIcon("bird.jpg");    

   //rescale the image according to the size selected
   Image img = icon.getImage().getScaledInstance(x,y,img.SCALE_SMOOTH);         
   icon.setImage(img ); 
   JLabel label = new JLabel(icon);
   pDraw.add(label);    
}

for(Component component:components)
{
   //retrieve the coordinate
   System.out.println(component.getBounds());
}

EDITED: I have tried this but not working :-(

for(Component component: pDraw.getComponents()){
  System.out.println(((JLabel)component).getIcon());
}

How can I get output like these?

java.awt.Rectangle[x=300,y=100,width=50,height=40]
java.awt.Rectangle[x=400,y=400,width=60,height=50]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆萌少年 2024-09-01 07:32:18

您的图像是否以所需的尺寸显示?

我想是的。

不管怎样,从你的代码似乎做的事情来看,我猜它得到的是标签大小,而不是图标大小。 JLabel 与任何 JComponent 一样,实际上是 Container 实例。因此,它们的大小取决于约束。因此,在 GridLayout 中,JLabel 将具有单元格的大小,而包含的 Icon 将具有图像的大小。

因此,要获取图像尺寸,您必须调用 ((JLabel) component).getIcon() 才能检索有效图像尺寸。

Do your images appear at the desired size ?

i think so.

Anyway, from what your code seems to do, I guess it gets the labels size, and not the icons size. JLabel, like any JComponent, are in fact Container instance. As such, their size depends upon constraints. As a consequence, in a GridLayout, a JLabel will have the size of a cell, whereas the contained Icon will have the size of the image.

As a consquence, to get image size, you have to call ((JLabel) component).getIcon() to be able to retrieve effective image dimension.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文