Java GridBagLayout不使组件在添加后“跳转”

Java GridBagLayout不使组件在添加后“跳转”,java,swing,alignment,gridbaglayout,Java,Swing,Alignment,Gridbaglayout,我想将GridBagLayout用于具有使用GridBagLayout的小示例程序的布局: public class Square extends JPanel { public Square() { super(); Dimension SIZE = new Dimension(200, 200); this.setSize(SIZE); this.setPreferredSize(SIZE); this.s

我想将GridBagLayout用于具有使用GridBagLayout的小示例程序的布局:

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();
    }
}
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();
         }
      });
   }
}

使用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();
    }
}
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();
         }
      });
   }
}
如果所有JComponents都可以具有相同的高度和重量,那么请查找GridLayout

如果JComponent不能具有相同的重量,则使用BorderLayout或BoxLayout将每行放入单独的JPanel,并使用GridLayout将这些JPanel放入容器中

如果所有JComponent都具有相同的高度和重量,则查找GridLayout


如果JComponent不能获得相同的权重,那么使用BorderLayout或BoxLayout将每一行放到单独的JPanel中,并使用GridLayout将这些JPanel放入容器中

为什么不简单地使用GridLayout 0、3、3列、可变行数?好的,这样做没有问题?它可以,取决于您希望它做什么:确实有效,但现在前两个正方形被垂直拉伸以适应。为了更好地了解它的工作原理,创建一个类似于我所做的小型可编译可运行示例,为什么不简单地创建一个GridLayout0,3列,可变行数?好的,这样做没有问题吗?它可以,取决于您希望它做什么:确实有效,但现在前两个正方形被垂直拉伸以适应。为了更好地了解它是如何工作的,创建一个类似于我所做的小型可编译可运行示例,an+1,因为这对我有很大帮助,我现在唯一的问题是,我的正方形被垂直拉伸,直到我得到3行。编辑:它们也是水平拉伸的,没有立即注意到,因为容器的宽度几乎与3xsquare+marginframe.setLocationRelativeTonull;相同@AndrewThompson我添加了类似的内容now@teuneboon唉,我想,为了得到帮助,有些东西是SSCCE,有些东西不是SSCCE。您发布的内容属于后一组。请仔细阅读链接。+1因为这对我帮助很大,我现在唯一的问题是我的方块垂直拉伸,直到我得到3行。编辑:它们也是水平拉伸的,没有立即注意到,因为容器的宽度几乎与3xsquare+marginframe.setLocationRelativeTonull;相同@AndrewThompson我添加了类似的内容now@teuneboon唉,我想,为了得到帮助,有些东西是SSCCE,有些东西不是SSCCE。您发布的内容属于后一组。请仔细阅读链接。
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);
    }
}
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();
         }
      });
   }
}