英文:
Java Positioning Problem of JLabel in JFrame
问题
// 创建框架
this.setSize((size+2)*imageSize+16, (size+2)*imageSize+39);
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.black);
this.setVisible(true);
// 创建地图JLabels
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
mapJLabels[x][y] = new JLabel();
this.add(mapJLabels[x][y]);
mapJLabels[x][y].setSize(imageSize, imageSize);
mapJLabels[x][y].setLocation((x+1)*imageSize, (y+1)*imageSize);
mapJLabels[x][y].setVisible(true);
}
}
英文:
I´m creating a TileMap out of JLabels and my Problem is that the last Tile is not placed right.
For some reason the last JLablel is on a wrong position. I think this Image:
and the Code explains what my problem is.
Btw. I am using BlueJ (maybe there is a bug in it, that I dont know)
Thanks for help!!!
//create Frame
this.setSize((size+2)*imageSize+16,(size+2)*imageSize+39);
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.black);
this.setVisible(true);
//create MapJLabels
for(int x = 0;x<size;x++){
for(int y = 0;y<size;y++){
mapJLabels[x][y] = new JLabel();
this.add(mapJLabels[x][y]);
mapJLabels[x][y].setSize(imageSize,imageSize);
mapJLabels[x][y].setLocation((x+1)*imageSize,(y+1)*imageSize);
mapJLabels[x][y].setVisible(true);
}
}
答案1
得分: 3
-
代码正在尝试设置组件的大小/位置。这意味着您正在使用空布局。不要这样做。Swing 设计时考虑了布局管理器。您应该使用
GridLayout
。阅读 Swing 教程中关于布局管理器的部分获取更多信息。 -
您创建了一个空的 JLabel。默认情况下,JLabel 是透明的。我现在不确定您发布的图片是否是从您发布的代码创建的。我看不出网格是如何用图像填充的等等。
-
Swing 组件默认是可见的,因此不需要调用 setVisible(true)。
-
您的代码尝试设置框架的大小。同样,不要这样做。使用布局管理器时,只需对框架调用
pack()
,大小将自动计算。 -
您的代码尝试在框架和标签之间留出空白空间。不要这样做。如果您想要额外的空间,可以在面板上添加
EmptyBorder
。阅读 Swing 教程中关于如何使用边框的部分。 -
您的代码正在使用 KeyListener。不要这样做。Swing 设计时考虑了键绑定。
-
组件应该在框架可见之前添加到框架中。
发布的代码看起来还可以,但由于我们看不到您如何更改标签的背景或向标签添加图标,我们不知道您的其余逻辑在做什么。问题可能出在那里。
如果在解决了上述问题后仍需要更多帮助,请发布一个适当的最小可复现示例,演示您的新问题。
英文:
-
The code is attempting to set the size/location of the component. This implies you are using a null layout. Don't. Swing was designed to be used with layout managers. You should use the
GridLayout
. Read the section from the Swing tutorial on Layout Managers for more information. -
You create an empty JLabel. By default a JLabel is transparent. I'm not sure now the picture you posted is created from the code you posted. I don't see how the grid is populated with images etc.
-
Swing components are visible by default so there is no need for the setVisible(true)
-
Your code attempts to set the size of the frame. Again, don't. When using layout managers you just
pack()
the frame and the size will be calculated automatically. -
Your code attempts to leave space between the frame and labels. Don't. If you want extra space you can add an
EmptyBorder
to the panel. Read the section from the Swing tutorial on How to Use Borders -
You code is using a KeyListener. Don't. Swing was designed to be used with Key Bindings
-
Components should be added to the frame BEFORE the frame is made visible.
The posted code looks reasonable, but since we can't see where you change the background of the labels or where you add icons to the labels, we don't know what the rest of your logic is doing. The problem could be there.
If you need more help after fixing the above problems then post a proper minimal, reproducible example that demonstrates your new problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论