Java GridLayout 将我所有的 JPanel 布局在左上角

发布于 2024-12-09 21:46:09 字数 1101 浏览 0 评论 0原文

已解决:

在尝试制作 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 技术交流群。

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

发布评论

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

评论(1

吻安 2024-12-16 21:46:09
  • 确保导入正确的面板。 (而不是 java.awt.Panel 类。)或者,更好的方法是将您的类重命名为 GridPanel 或类似的名称,以避免混淆/冲突。

  • 您可能不想覆盖paintChildren。在这种情况下,覆盖 paintComponent 听起来是更好的选择。

  • 您可能希望为 Cell 类设置最小/首选/最大大小。

如果没有实例化和使用 Panel 类的实际代码,很难进行更多观察。

  • Make sure you import the right Panel. (And not the java.awt.Panel class.) Or, better, rename your class to GridPanel or something similar to avoid confusion / clashes.

  • You probably don't want to override paintChildren. Overriding paintComponent 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.

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