简体   繁体   English

GridBagLayout中有1个以上的JPanel

[英]More than 1 JPanel in GridBagLayout

Hello please see the attached image ,i am coding interface that has three JPanels and i am using GridBagLayout , i have read some good tutorials and got some understanding as well , but i need your guidance regarding placing 2 JPanels side by side as in given pic . 您好,请参见所附的图片,我是具有三个JPanels的编码接口,并且我正在使用GridBagLayout ,我已经阅读了一些不错的教程并获得了一些理解,但是我需要您的指导,关于并排放置2个JPanels,如给定的图片。

for example if i do frame.add(leftpanel); 例如如果我做frame.add(leftpanel); and then there is only one panel on the JFrame .. how to align it left on half of the JFrame so that when i do frame.add(panelright); 然后在JFrame上只有一个面板..如何将其对齐到JFrame的一半,以便当我执行frame.add(panelright); it is added to right sid , 它被添加到正确的sid中,

please guide me to do the functionality shown in Pic , 请指导我完成Pic所示的功能, 在此处输入图片说明

i can handle 1 JPanel and all the components but dont know abut to handle more than 1 , 我可以处理1个JPanel和所有组件,但不知道要处理1个以上,

I think in your case BorderLayout will be more useful. 我认为在您的情况下BorderLayout会更有用。 Try reading about it here . 尝试在这里阅读有关内容。

You can add your panels north south east west or at the center it will be much better than GridBag in this case. 您可以在东南西北或在中心添加面板,在这种情况下,它比GridBag更好。

You need to specify the position and other settings using GridBagConstraints . 您需要使用GridBagConstraints指定位置和其他设置。 For example, you could do something like this: 例如,您可以执行以下操作:

GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.BOTH;

frame.add(leftPanel, constraints);
constraints.gridx = 1;
frame.add(panelright, constraints);

constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 2;
frame.add(bottomPanel, constraints);

GridBagConstraints.gridx and GridBagConstraints.gridy determine the row and column for this element. GridBagConstraints.gridx和GridBagConstraints.gridy确定此元素的行和列。 fill tells the layout to use all the available space both horizontally and vertically. fill告诉布局在水平和垂直方向上使用所有可用空间。 If you want to set that one cell will use a space with certain proportion relative to other cells, you can use the weightx and weighty fields. 如果要设置一个单元格将相对于其他单元格使用一定比例的空间,则可以使用weightx和weighty字段。

Also consider two JSplitPane instances. 还考虑两个JSplitPane实例。 The outermost pane would have a VERTICAL_SPLIT between top and bottom. 最外面的窗格在顶部和底部之间将具有VERTICAL_SPLIT The top of the outer pane would contain another JSplitPane having a HORIZONTAL_SPLIT between left and right. 外窗格的顶部将包含另一个JSplitPane ,该JSplitPane在左右之间具有HORIZONTAL_SPLIT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM