♠ Posted by Unknown in Adv Java at 00:13
Layout Managers
The way that you place components on a form in Java
is probably different from any other GUI system you’ve used. The way components
are placed on a form is controlled not by absolute positioning but by a “Layout
Manager” that decides how the components lie based on the order that you add()
them. The size, shape, and placement of components will be remarkably different
from one layout manager to another. In addition, the layout managers adapt to
the dimensions of your applet or application window, so if the window dimension
is changed, the size, shape, and placement of the components can change in
response.
JApplet, JFrame, JWindow, and JDialog can all
produce a Container with
getContentPane() that can contain and display Component. In Container, there’s
a method called setLayout() that allows you to choose a different layout
manager. Other classes, such as JPanel, contain and display components directly
and so you also set the layout manager directly, without using the content
pane.
- BorderLayout
:-
The applet uses a default layout scheme : the
BorderLayout. Without any other instruction, this takes whatever you add() to
it and places it in the center, stretching the object all the way out to the
edges.
This layout manager has the concept of four border
regions and a center area. When you add something to a panel that’s using a
BorderLayout you can use the overloaded add() method that takes a constant
values as its first argument.
BorderLayout.NORTH(top);
BorderLayout.SOUTH(bottom);
BorderLayout.EAST(right);
BorderLayout.WEST(left);
BorderLayout.CENTER(fill
the middle);
If
you don’t specify an area to place the object, it defaults to CENTER.
- FlowLayout
:-
This simply “flows” the components onto the form,
from left to right until the top space is full, then moves down a row and
continues flowing.
All components will be compacted to their smallest
size in a FlowLayout, so you might get a little bit of surprising behavior.
- GridLayout
:-
A GridLayout allows you to build a table of
components, and as you add them they are placed left-to-right and top-to-bottom
in the grid. In the constructor you specify the number of rows and columns that
you need and these are laid out in equal proportions.
- GridBagLayout
:-
The GridBagLayout provides you with tremendous
control in deciding exactly how the regions of your window will lay themselves
out and reformat themselves when the window is resized. it’s also the most
complicated layout manager, and quite difficult to understand.
0 comments:
Post a Comment