tesfa koli
tesfa koli

Reputation: 755

Relative positioning and absolute coordinates?

I am in the process of completing a course work but I need some help understanding this:

"Note: Your application should keep the relative positioning of buttons and checkboxes when the frame of the application is resized based on the layout managers and the tech- niques covered in the module. I.e. you should not choose absolute coordinates when placing JComponents on the screen but relative positioning, i.e. component X is to the right of component Y and to the left of component Z and these relative locations should be maintained if the user resizes the frame of the application."

Does this mean im not to use BoxLayout.X/Y_AXIS? Absolute coordinates?

To my understanding I have to use several panels? If so, my code contains a login and when I login how will I change the colour of the frame?

Upvotes: 2

Views: 4205

Answers (2)

G. Bach
G. Bach

Reputation: 3909

There's basically two ways to layout the components in a JFC/Swing application:

  • assign each container a layout manager that will take care of the layout; see the link provided by @Shakedown
  • assign each JComponent an absolute position for it's top left corner, counting pixels from the top left starting at (0,0) of the top-level-container (see here for what that is) the components are in; see this tutorial on absolute positioning.
As you can imagine, the second way has some problems: it's not at all dynamic and is easy to get wrong, to name the most prominent ones. Using a layout manager in most cases is not only the easier, but also the smarter way to arrange your GUI.

What the layout manager in effect does is assign each JComponent an absolute position derived from the layout the component is layed out by at runtime, dynamically - usually using the PreferredSize of the JComponents; for example, a FlowLayout will assign each JComponent an absolute position that will put it exactly fitting to the right of the JComponent before it, wrapping lines when needed. When a container is resized, all components within it will have their positions recomputed. See here for details.

Concerning your question about BoxLayout, that is a layout manager; BoxLayout.X_AXIS and BoxLayout.Y_AXIS are used to determine in which direction the JComponents which the BoxLayout manages are to be arranged; they do not refer to absolute positioning. See the Javadocs on BoxLayout and the Java tutorial on BoxLayout for details.

All in all, your task is to use a layout manager; the different layout managers that are available in the standard java libraries are described in sufficient detail in the tutorials (see links, browse the sites). Have fun!

Upvotes: 5

Nate W.
Nate W.

Reputation: 9259

This sounds like it's saying that you need to use layout managers instead of absolute positioning. Yes, you will end up using several components (likely JComponent or JPanel), each with their own layout manager. You can build up your UI by starting with standalone components and combining them together in another component with a layout manager.

Start here: Laying Out Components

If you've got specific questions such as changing the color of your frame, it'd be best to ask that as a separate question (assuming a similar question doesn't already exist).

Upvotes: 4

Related Questions