简体   繁体   中英

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 .

for example if i do 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); it is added to right sid ,

please guide me to do the functionality shown in Pic , 在此处输入图片说明

i can handle 1 JPanel and all the components but dont know abut to handle more than 1 ,

I think in your case BorderLayout will be more useful. 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.

You need to specify the position and other settings using 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. fill tells the layout to use all the available space both horizontally and vertically. 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.

Also consider two JSplitPane instances. The outermost pane would have a VERTICAL_SPLIT between top and bottom. The top of the outer pane would contain another JSplitPane having a HORIZONTAL_SPLIT between left and right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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