GridBagLayout 不要让组件“跳转”到其他布局添加后

发布于 2024-12-11 18:30:43 字数 2509 浏览 0 评论 0原文

我想将 GridBagLayout 用于具有 <= 3 列和可变数量行的布局(如果有人知道可以轻松完成此操作的另一种布局,请告诉我),每次我按“添加”按钮时,都会出现一个正方形添加到第一个可用的位置。就像这样:

|x x x|
|x x x|
|x o  |

x 是正方形,当我按添加时,应该在 o 现在所在的位置添加一个新正方形。 我设法“让它工作”,如下所示:

public void addSquare(Square square) {
    c.fill = GridBagConstraints.NONE;
    c.gridx = nrOfSquares % 3;
    c.gridy = (int) (nrOfSquares / 3);
    c.weighty = 1;
    c.weightx = 1;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.insets = new Insets(5, 5, 5, 5);
    this.container.add(square, c);
    this.container.revalidate();
    ++nrOfSquares;
}

问题是我添加的第二个正方形是这样添加的:

|x  x  |

请注意,第一个正方形和第二个正方形之间有一个额外的空间。添加额外行时我遇到同样的问题。

现在,我如何修复我的代码,以便方块不会“跳跃”并像我给出的第一个示例中那样添加?

编辑:根据要求,将其转换为常规 GridLayout 后的一个更好的示例:

public class Square extends JPanel {
   public Square() {
       super();     
       Dimension SIZE = new Dimension(200, 200);
       this.setSize(SIZE);
       this.setPreferredSize(SIZE);
       this.setMinimumSize(SIZE);
       this.setMaximumSize(SIZE);

       this.setBackground(Color.ORANGE);

       this.setVisible(true);
   }
}

public class SquareContainer extends JPanel {
    protected JPanel realContainer;

    public SquareContainer(int width, int height) {
        super();
        this.setLayout(new BorderLayout());
        this.setBackground(Color.WHITE);
        this.setSize(width, height);
        this.realContainer = new JPanel();
        GridLayout layout = new GridLayout(0, 3);
        layout.setHgap(10);
        layout.setVgap(10);
        this.realContainer.setLayout(layout);
        this.realContainer.setBackground(this.getBackground());
        JScrollPane scroller = new JScrollPane(this.realContainer);
        scroller.getVerticalScrollBar().setUnitIncrement(20);
        this.add(scroller, BorderLayout.CENTER);
    }

    public void addSquare(Square square) {
        this.realContainer.add(square);
        this.realContainer.revalidate();
    }
}

我只是将其添加到 JFrame 中:

public class TheGreatFrame extends JFrame {
    public TheGreatFrame() {
        super();
        this.setSize(800, 800);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setResizable(false);

        this.add(new SquareContainer(750, 660), BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
}

I want to use GridBagLayout for a layout that has <= 3 columns, and a variable amount of rows(If anyone knows another layout that can do this easily, please tell me), everytime I press the "add" button a square will be added at the first location that is available. Like so:

|x x x|
|x x x|
|x o  |

The x's are squares, and when I press add a new square should be added where the o is now.
I managed to "kind of make it work" like so:

public void addSquare(Square square) {
    c.fill = GridBagConstraints.NONE;
    c.gridx = nrOfSquares % 3;
    c.gridy = (int) (nrOfSquares / 3);
    c.weighty = 1;
    c.weightx = 1;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.insets = new Insets(5, 5, 5, 5);
    this.container.add(square, c);
    this.container.revalidate();
    ++nrOfSquares;
}

the problem is that the second square I add is added like this:

|x  x  |

please note that there is an extra space between the first square and the second one. I have the same problem when an extra row is added.

Now how do I fix my code so that the squares don't "jump" and are added like in the first example I gave?

EDIT: as requested, a better example after I converted it to a regular GridLayout:

public class Square extends JPanel {
   public Square() {
       super();     
       Dimension SIZE = new Dimension(200, 200);
       this.setSize(SIZE);
       this.setPreferredSize(SIZE);
       this.setMinimumSize(SIZE);
       this.setMaximumSize(SIZE);

       this.setBackground(Color.ORANGE);

       this.setVisible(true);
   }
}

public class SquareContainer extends JPanel {
    protected JPanel realContainer;

    public SquareContainer(int width, int height) {
        super();
        this.setLayout(new BorderLayout());
        this.setBackground(Color.WHITE);
        this.setSize(width, height);
        this.realContainer = new JPanel();
        GridLayout layout = new GridLayout(0, 3);
        layout.setHgap(10);
        layout.setVgap(10);
        this.realContainer.setLayout(layout);
        this.realContainer.setBackground(this.getBackground());
        JScrollPane scroller = new JScrollPane(this.realContainer);
        scroller.getVerticalScrollBar().setUnitIncrement(20);
        this.add(scroller, BorderLayout.CENTER);
    }

    public void addSquare(Square square) {
        this.realContainer.add(square);
        this.realContainer.revalidate();
    }
}

And I just add that to a JFrame:

public class TheGreatFrame extends JFrame {
    public TheGreatFrame() {
        super();
        this.setSize(800, 800);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setResizable(false);

        this.add(new SquareContainer(750, 660), BorderLayout.CENTER);

        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }
}

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

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

发布评论

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

评论(2

苏别ゝ 2024-12-18 18:30:43

使用GridLayout的一个小示例程序:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridLayoutEg {

   private static void createAndShowGui() {
      final JPanel centerPanel = new JPanel(new GridLayout(0, 3)); 

      JButton addBtn = new JButton(new AbstractAction("Add Button") {

         @Override
         public void actionPerformed(ActionEvent e) {
            centerPanel.add(new JButton("X"));
            centerPanel.revalidate();
            centerPanel.repaint();  

            SwingUtilities.getWindowAncestor(centerPanel).pack();
         }
      });
      JPanel btnPanel = new JPanel();
      btnPanel.add(addBtn);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(centerPanel, BorderLayout.CENTER);
      mainPanel.add(btnPanel, BorderLayout.PAGE_END);
      JFrame frame = new JFrame("GridLayoutEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

A small example program of using GridLayout:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GridLayoutEg {

   private static void createAndShowGui() {
      final JPanel centerPanel = new JPanel(new GridLayout(0, 3)); 

      JButton addBtn = new JButton(new AbstractAction("Add Button") {

         @Override
         public void actionPerformed(ActionEvent e) {
            centerPanel.add(new JButton("X"));
            centerPanel.revalidate();
            centerPanel.repaint();  

            SwingUtilities.getWindowAncestor(centerPanel).pack();
         }
      });
      JPanel btnPanel = new JPanel();
      btnPanel.add(addBtn);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(centerPanel, BorderLayout.CENTER);
      mainPanel.add(btnPanel, BorderLayout.PAGE_END);
      JFrame frame = new JFrame("GridLayoutEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
划一舟意中人 2024-12-18 18:30:43

如果所有 JComponents 可以具有相同的 HEIGHTWEIGHT,则查找 GridLayout

以防 JComponent 无法使用相同的 WEIGHT,然后将每个 "line" 放入单独的 JPanel (通过使用 BorderLayout< /code> 或 BoxLayout) 并使用GridLayout 用于将这些 JPanels 放入 Container

if all JComponents could have the same HEIGHT and WEIGHT, then look for GridLayout

in case that the JComponent couldn't same WEIGHT, then put each "line" to the separate JPanel (by using BorderLayout or BoxLayout) and use GridLayout for put these JPanels into Container

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