java如何使用GridBagLayout创建3个高度可变的JPanel,一个接一个
我想用java编写一个简单的文本编辑器
我组织了我的布局,并得出结论,我基本上需要3个JPanel,一个在另一个之上。第一个和第二个将是非常短的高度,因为它们将是menubar和一个JPanel,分别包含2个JLabel。 中间的一个必须是高度最大的一个,因为所有文本都将包含在其中
我想我需要使用GridBagLayout,但这不起作用,我需要他们占据大的10倍于小的。所有这些都将利用JFrame提供的宽度
到目前为止,代码片段是-
GridBagConstraints gbc = new GridBagConstraints
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
mainFrame.add(upperGrid, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridheight = 10;
mainFrame.add(upperGrid, gbc);
gbc.gridx = 0;
gbc.gridy = 11;
mainFrame.add(upperGrid, GBC);
我得到的结果是-
# 1 楼答案
我建议你放弃网格布局的想法。我会做以下几点:
使用
JMenuBar
作为菜单栏(https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html)使用边框布局:
例如,当需要安排包含大量文本字段的对话框时,可以使用GridLayout。但对于这种“粗糙”的东西,边界布局更好,也因为它可能更快。(可能,我不确定)
编辑:如果必须使用GridBagLayout,则可以执行以下操作: