GridBagLayout 不要让组件“跳转”到其他布局添加后
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用GridLayout的一个小示例程序:
A small example program of using GridLayout:
如果所有
JComponents
可以具有相同的HEIGHT
和WEIGHT
,则查找GridLayout
,以防
JComponent
无法使用相同的WEIGHT
,然后将每个"line"
放入单独的JPanel
(通过使用BorderLayout< /code> 或
BoxLayout
) 并使用GridLayout
用于将这些JPanels
放入Container
if all
JComponents
could have the sameHEIGHT
andWEIGHT
, then look forGridLayout
in case that the
JComponent
couldn't sameWEIGHT
, then put each"line"
to the separateJPanel
(by usingBorderLayout
orBoxLayout
) and useGridLayout
for put theseJPanels
intoContainer