我遇到了此布局的问题。如何制作第二个按钮2x2,尺寸为网格宽度?GridBagLayout组件放置
我也想知道,如果我想拉伸第二个按钮为两列宽,我该怎么说从哪里开始以及在哪里停止?如何区分覆盖第2列和第3列的第1列和第2列?
所有的public class SwingExample { public static void main(String[] args) { JFrame theFrame = new JFrame("Grid Bag Example"); theFrame.setSize(600,400); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel thePanel = new JPanel(); theFrame.add(thePanel); JButton button1 = new JButton("1"); JButton button2 = new JButton("2"); JButton button3 = new JButton("3"); GridBagLayout gbl = new GridBagLayout(); thePanel.setLayout(gbl); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(10, 10, 10, 10); gbc.gridx = 0; gbc.gridy = 0; thePanel.add(button1,gbc); gbc.gridx = 1; gbc.gridy = 1; thePanel.add(button2,gbc); gbc.gridwidth = 2; gbc.gridheight = 2; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.fill = GridBagConstraints.VERTICAL; gbc.gridx = 3; gbc.gridy = 3; thePanel.add(button3,gbc); theFrame.setVisible(true); } }
回答
首先你运行本教程中的演示代码???
我会尝试解释教程代码,以便更好地理解。它说明如何使用gridwidth
约束,使得:
- 在第二行按钮跨越第三行3列
- 按钮跨越2列开始的第二列。
GridBagLayout首先需要确定每个包含组件的列的宽度。因此找到每个列的最大组件。
现在您已知道每列中最大组件的大小,您可以通过累加列宽来确定第2行和第3行按钮的大小。第2行中的按钮将添加所有3列的宽度。第3列中的按钮将仅添加最后两列的宽度。
而且你到底会在第4栏组件有如果你想有一个组件跨越多个列与部件3
的行为,并跨越做的,然后一些其他行都必须有一个分量该列可以确定列的宽度。仅仅因为你使用了gridwidth = 2
并不意味着这个按钮的大小会自动增加一倍。
gbc.gridx = 3;
在上面,你不能只是有按钮3奇迹般地在第3栏显示有在COLUMN2没有组件那么第2列的宽度将为0
有没有窍门一些虚拟,不可见,零维组件
那么您不能使用零维组件,因为那么宽度/高度将为零,这意味着列/行大小也将为零。所以你可以添加一个大小的虚拟组件。例如:
import java.awt.*;
import javax.swing.*;
public class SwingExample {
public static void main(String[] args) {
JFrame theFrame = new JFrame("Grid Bag Example");
theFrame.setSize(600,400);
theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel thePanel = new JPanel();
theFrame.add(thePanel);
JButton button1 = new JButton("1");
JButton button2 = new JButton("2");
JButton button3 = new JButton("3");
JLabel label1 = new JLabel();
label1.setPreferredSize(button1.getPreferredSize());
JLabel label2 = new JLabel();
label2.setPreferredSize(button1.getPreferredSize());
GridBagLayout gbl = new GridBagLayout();
thePanel.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
thePanel.add(button1,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
thePanel.add(button2,gbc);
gbc.gridx = 2;
gbc.gridy = 2;
thePanel.add(label1,gbc); // dummy component
gbc.gridx = 3;
gbc.gridy = 3;
thePanel.add(label2,gbc); // dummy component
gbc.gridx = 2;
gbc.gridy = 2;
gbc.gridwidth = 2;
gbc.gridheight = 2;
thePanel.add(button3,gbc);
theFrame.setVisible(true);
}
}
在出任何标签添加到面板,你会看到它停止工作,因为没有组件占据单个电池的列/行以上代码注释。这是我在说明网格宽度相对于列中其他组件时要解释的。
那么这里没有实际的网格?我不做任何网格,然后扔在组件?只添加了组件,然后我想它们会形成一个网格,然后尝试通过虚构网格来操纵它们? – user7381956
通过向网格添加组件,可以使网格尽可能大。如果您没有将组件添加到网格中的某个单元格,那么该单元格的大小为(0,0)。我不知道如何更好地解释它。要理解的唯一方法就是在教程链接和上面的代码中使用已给出的工作示例。 – camickr
好的,会试试。谢谢。 – user7381956
- 1. 如何在GridBagLayout中的组件之间放置空格?
- 2. GridBagLayout不在页面开始处放置组件
- 3. 在交替列中设置组件GridBagLayout
- 4. 通过使用GridBagLayout设置组件组件
- 5. GridBagLayout - 放置图表和标签
- 6. Java GridBagLayout - 组件脱落
- 7. GridBagLayout的调整组件
- 8. 替换GridBagLayout中的组件
- 9. GridBagLayout中的组件大小
- 10. GridBagLayout不显示JPanel组件
- 11. Java GridBagLayout - 如何将我的组件无间隙地逐个放置?
- 12. 如何推动GridbagLayout不在JPanel的中心放置组件的问题
- 13. 如何使用GridBagLayout将组件放在右下角?
- 14. 为什么GridBagLayout将我的组件放在中心而不是放在角落?
- 15. 如何为GridBagLayout中的所有组件设置间隔?
- 16. 在java中使用GridBagLayout时设置最大组件大小
- 17. 使用GridBagLayout使组件跨越多行
- 18. GridBagLayout添加后不要让组件“跳”
- 19. GridBagLayout中的右侧1个组件
- 20. Java GridBagLayout组件未全屏显示
- 21. 迭代地用gridbaglayout添加组件?
- 22. GridBagLayout中组件的大小不正确
- 23. GridBagLayout中最轻的间隔器组件
- 24. 从JFrame中获取组件大小GridBagLayout
- 25. GridBagLayout:如何移动指定组件
- 26. 如何使用GridBagLayout定位组件?
- 27. 在Silverlight中放置组件
- 28. Java Swing组件放置
- 29. Swing Draw GridBagLayout组件中组件之间的1px边框线
- 30. Java Swing GridBagLayout在其他组件之间添加组件
网格宽度相对于这些列中的其他组件。你只有3列。您不能只让第三个按钮的宽度为2,因为它是第三列中的唯一按钮。如果将第三个按钮放在第0列中,则可以将其设置为两个宽度,因为现在它可以占用前两列中按钮的宽度。 – camickr
您能否详细说明该句子 - _gridwidth是相对于这些列中的其他组件的__。我不明白这一点。 [Oracle](https://docs.oracle.com/javase/8/docs/api/java/awt/GridBagConstraints.html#gridwidth)明确指出它_指定组件显示区域的行中单元格的数量._看起来很简单明了。 – user7381956
第4列中没有组件,因此您不能说第3列中的组件可以跨越第3列和第4列。我给出了组件跨多个列的示例。阅读Swing教程[如何使用GridBagLayout [(http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)以获取更多信息和工作示例。 – camickr