将按钮放置在网格下

发布于 2025-01-08 22:14:47 字数 796 浏览 2 评论 0原文

我试图将 2 个 JButtons 放置在 9x9 的网格下,但是当我添加按钮时,网格会更改以容纳按钮,将所有内容(包括按钮)放置在一个矩形中。

我想要的是一个网格和网格本身下方的两个按钮。

mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH)); mainPanel.setLayout(new GridLayout(3, 3));

    for (int i = 0; i < subPanels.length; i++)
    {

      for (int j = 0; j < subPanels[i].length; j++)
      {
        subPanels[i][j] = new JPanel(new GridLayout(3, 3));
        subPanels[i][j].setBorder(
          BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
        mainPanel.add(subPanels[i][j]);  
          }
    }
    buttonPanel.add(clear);
    buttonPanel.add(solve);
    buttonPanel.setLayout(new GridLayout(1,1));
    mainPanel.add(buttonPanel);

ButtonPanel 应该位于 mainPanel 下。

I am trying to position 2 JButtons under a grid of 9x9, but when I add the buttons, the grid is changed to accomodate the buttons, placing everything, including the buttons in a rectangle.

What i want is to have a grid and two buttons below the grid itself.

mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
mainPanel.setLayout(new GridLayout(3, 3));

    for (int i = 0; i < subPanels.length; i++)
    {

      for (int j = 0; j < subPanels[i].length; j++)
      {
        subPanels[i][j] = new JPanel(new GridLayout(3, 3));
        subPanels[i][j].setBorder(
          BorderFactory.createLineBorder(Color.black, BORDER_WIDTH));
        mainPanel.add(subPanels[i][j]);  
          }
    }
    buttonPanel.add(clear);
    buttonPanel.add(solve);
    buttonPanel.setLayout(new GridLayout(1,1));
    mainPanel.add(buttonPanel);

The buttonPanel is what is supposed to be under the mainPanel..

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

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

发布评论

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

评论(2

二智少女猫性小仙女 2025-01-15 22:14:47

最简单的事情可能是将 mainPanel 放入 BorderLayout 的中心,然后将 ButtonPanel 放在南边。

另外,您当前将 ButtonPanel 的 GridLayout 设置为 1 行 x 1 列。您可能应该在添加按钮之前设置布局,并将其设置为 1 行 x 2 列。

The easiest thing is probably to put your mainPanel into the center of a BorderLayout, and then put the buttonPanel in the south.

Also, you're currently setting the GridLayout for your buttonPanel to 1 row x 1 column. You should probably set the layout before adding the buttons, and set it to 1 row x 2 columns.

回眸一遍 2025-01-15 22:14:47

表单上的每个组件都有一个父组件。如果将button1放在form1上,button1.parent将为“form1”。

将所需按钮的父级更改为适当的父级

Every component on a form has a parent. If you place button1 on form1, button1.parent will be 'form1'.

change the parent of the buttons you want to the appropriate parent

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