【问题标题】:GridBagLayout Don't make components "jump" after addGridBagLayout 添加后不要让组件“跳转”
【发布时间】:2011-12-13 17:37:38
【问题描述】:

我想将 GridBagLayout 用于具有

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

x 是正方形,当我按下 add 时,应该在 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);
    }
}

【问题讨论】:

  • 为什么不简单地使用GridLayout(0, 3)(3 列,行数可变)?
  • 好的,这样可以正常工作吗?
  • 它可以,这取决于你想要它做什么。 :)
  • 确实有效,但现在前几个正方形被垂直拉伸以适应。
  • 为了更好地了解它是如何不工作的,请创建一个类似于我所做的小型可编译运行示例 sscce

标签: java swing alignment gridbaglayout


【解决方案1】:

一个使用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();
         }
      });
   }
}

【讨论】:

  • +1 因为这对我帮助很大,我现在唯一的问题是我的正方形被垂直拉伸直到我得到 3 行。编辑:它们也被水平拉伸,没有立即注意到,因为容器的宽度几乎与 3x(square + margin) 相同
  • frame.setLocationRelativeTo(null);Tut-tut。 ;)
  • @AndrewThompson 我现在添加了类似的内容
  • @teuneboon (sigh) IMO,为了获得帮助,有些东西是 SSCCE,有些东西是 SSCCE。您发布的内容属于后一组。请仔细阅读链接。
【解决方案2】:

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

如果JComponent 不能与WEIGHT 相同,则将每个"line" 放入单独的JPanel(使用BorderLayoutBoxLayout)并使用GridLayout 将这些@ 987654332@转Container

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多