0

I need a layout manager that can arrange JPanels in a JFrame as represented in the picture. The JPanels are neither in a row nor column. I used absolute layout for the demo and colored each JPanel to be visible.enter image description here

camickr
  • 321,443
  • 19
  • 166
  • 288
Isaac
  • 23
  • 5
  • 2
    Assuming 4 visible JPanels, the grey and right tan JPanels are inside an inner JPanel with a BorderLayout. The grey JPanel is in the center ad the right tan panel is east. The inner JPanel is inside an outer JPanel in the north position, and the light purple JPanel is south. The left tab JPanel is inside the outer JPanel in the west. – Gilbert Le Blanc Jun 07 '21 at 23:26
  • [`GridBagLayout`](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) can do most things you can imagine, but is a bit finicky to use. Multiple nested `JPanel` with the way simpler-to-use `BorderLayout` are probably easier to implement. – Joachim Sauer Jun 07 '21 at 23:36
  • 1
    You need additional requirements, like how does each panel resize as the frame is resized horizontally or vertically? That is the most important part of layout management. The other key is you are never restricted to a single layout manager. You can mix and match panels with different layout managers to achieve your layout. Read the Swing tutorial on [Layout Mangers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) and start experimenting. – camickr Jun 08 '21 at 00:05

1 Answers1

3

If(1) the blue panel needs to get the extra width & height as the frame is expanded, the gray panels get extra height, and the light blue area extra width, I'd use one BorderLayout for both blue panels and the gray panel on the RHS, and a BorderLayout for that panel and the gray panel on the left.

  1. Of course, this all depends on how the size should be assigned when the frame is enlarged. In future provide ASCII art or a simple drawing of the intended layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used.

In this image, the red & green boxes (roughly) outline two panels, each with a BorderLayout. The colored text in the different areas indicate the layout constraints used to position the containers within the panels.

enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433