Java GridLayout 将我所有的 JPanel 布局在左上角
已解决:
在尝试制作 SSCCE 后才发现问题所在。
这与我的单元类有关,我没有意识到我正在重写 JComponent 类中的 getX() 和 getY() 。
重命名这些访问器后,一切都按预期工作
============================================
我有一个 JPanel,其 GridLayout 设置为 3 行 x 3 列。
我正在尝试将 JPanel 添加到网格布局中的每个单元格以填充所有 9 个单元格。 这些 JPanel 中的每一个都有一个重写的 PaintChildren 方法,该方法将从 JPanel 的左上角开始绘制某种矩形 - 最终结果将是每个单元格中都有一个从单元格的左上角开始的矩形。
将所有 JPanel 添加到网格布局后,它们都出现在左上角,彼此重叠(我已确认它们是重叠的),而不是放置在 3x3 网格中。
如何将它们排列在 3x3 网格中?
(简化)代码:
public class Panel extends JPanel {
public Panel(int x, int y) {
layout = new GridLayout(x, y, 2, 2);
setLayout(layout);
populateGrid();
}
public void populateGrid() {
removeAll();
for (int i = 0; i < 9; i++)
add(new Cell(50,50));
}
}
public class Cell extends JPanel {
public Cell(int x, int y) {
// x/y values used to define rectangle
setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
setBackground(Color.WHITE);
}
public void paintChildren(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, x, y);
}
}
SOLVED:
Just found out what the problem was, after trying to make an SSCCE.
It had to do with my cell class, I didn't realise I was overriding getX() and getY() from the JComponent class.
After renaming these accessors it all works as expected
========================================
I have a JPanel with a GridLayout set at 3 rows x 3 cols.
I'm trying to add JPanels to each cell in the gridlayout to fill up all 9 cells.
Each one of these JPanels has an overriden paintChildren method which will paint some kind of rectangle starting at the top left of the JPanel - the end result will be each cell has a rectangle in it starting at the top left of the cell.
After adding all the JPanels to the gridlayout, they all appear in the top left corner overlapping each other (I have confirmed they are overlapping), instead of being laid out in a 3x3 grid.
How can I get them arranged in the 3x3 grid?
(Simplified) Code:
public class Panel extends JPanel {
public Panel(int x, int y) {
layout = new GridLayout(x, y, 2, 2);
setLayout(layout);
populateGrid();
}
public void populateGrid() {
removeAll();
for (int i = 0; i < 9; i++)
add(new Cell(50,50));
}
}
public class Cell extends JPanel {
public Cell(int x, int y) {
// x/y values used to define rectangle
setBorder(BorderFactory.createLineBorder(new Color(0,0,0)));
setBackground(Color.WHITE);
}
public void paintChildren(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, x, y);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保导入正确的
面板
。 (而不是java.awt.Panel
类。)或者,更好的方法是将您的类重命名为GridPanel
或类似的名称,以避免混淆/冲突。您可能不想覆盖
paintChildren
。在这种情况下,覆盖paintComponent
听起来是更好的选择。您可能希望为
Cell
类设置最小/首选/最大大小。如果没有实例化和使用
Panel
类的实际代码,很难进行更多观察。Make sure you import the right
Panel
. (And not thejava.awt.Panel
class.) Or, better, rename your class toGridPanel
or something similar to avoid confusion / clashes.You probably don't want to override
paintChildren
. OverridingpaintComponent
sounds like a better option in this case.You may want to set minimum / preferred / maximum size for the
Cell
class.Hard to make more observations without the actual code that instantiates and uses your
Panel
class.