-1

I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. How can set the location of panels in the frame?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jayashri
  • 366
  • 4
  • 13
  • 25
  • 4
    Read the swing tutorial about layout managers: http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html – JB Nizet Jun 12 '12 at 07:38
  • 2
    You really should read documentation or make some research efforts. I think you could find the answer by your own. – Jean-Rémy Revy Jun 12 '12 at 07:45

5 Answers5

3

Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:

  • Include default spacing in the constructor (often)
  • Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).

Extra spacing can be organized by adding an EmtpyBorder to child components.

  1. See the nested layout example

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.

Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers

However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.

rolve
  • 10,083
  • 4
  • 55
  • 75
0

If its 4 locations, you can use BorderLayout,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used.

Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • +1 for `GroupLayout` -1 for mentioning `setBounds` - net result 0. – Andrew Thompson Jun 12 '12 at 07:45
  • That answer has become better with the edit, but now you removed `setBounds` I notice you were referring to a `BorderLayout` (as clearly stated in the 1st sentence), as opposed to a `null` layout, where `setBounds` might not be ignored. So.. on the matter of the `BorderLayout`, it is the constraints used when adding components (e.g. `CENTER`, `NORTH`/`PAGE_START`) that does the trick, rather than `setLocation`. If you were to make that change I could up-vote the answer.. – Andrew Thompson Jun 12 '12 at 13:44
0

For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks

SAP
  • 468
  • 2
  • 14
-2
  1. set the layout of the Frame to be null via setLayout(null)
  2. create 4 JPanel and set their location using setLocation method
  3. add these panels using JFrame's add method
weidi
  • 852
  • 7
  • 20