aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes layout managers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "layout managers" Watch "layout managers" New topic
Author

layout managers

Alokesh Phukan
Greenhorn

Joined: May 24, 2001
Posts: 21
Dear Friends,
>
>
> These questions are from abhilash's mock exam at http://www.angelfire.com/or/abhilash/Main.html
> I'm unable to figure why they give opposite results(sort of!).The answers are
options 5, 4 respectively. I thought that layout like font and color affected
the components added afterwards the change(apparantly that is a very wrong
assumption!!). Anyone who can give me hints on how to approach similar
problems?? .
> Q1
> import java.awt.*;
> public class TestFrame extends Frame
> {
> Button bNorth = new Button("North");
> Button bSouth = new Button("South");
> Button bEast = new Button("East");
> Button bWest = new Button("West");
> Button bCenter = new Button("Center");
>
> public TestFrame()
> {
> setLayout(new FlowLayout());
> add(bNorth);
> add(bSouth);
> add(bWest);
> add(bEast);
> add(bCenter);
>
> setLayout(new BorderLayout());
> validate();
> setSize(300,300);
> setVisible(true);
> }
>
> public static void main(String args[])
> {
> TestFrame tf = new TestFrame();
> }
> }
> Attemping to compile and run the above code
>
> Answers
> 1. Will cause a compilation error - a Layout cannot be set after a component
has been added with a preset Layout Manager.
> 2. Will cause a Runtime Exception - a Layout cannot be set after a component
has been added with a preset Layout Manager.
> 3. Will compile cleanly and throw no runtime Exception. Only the button with
label "Center" is visible and occupies the whole screen.
> 4. Will compile cleanly an throw no runtime Exception. All the buttons are
arranged in a single line. Any other component added in future will follow the
rules of the BorderLayout Manager.
> 5. Will compile and run cleanly, but no component is visible.
> 6. Will compile cleanly and throw no runtime Exception. The buttons are
arranged as listed below
> Button Label Position
> Center Center
> North North
> South South
> East East
> West West
>
> Q2.
>
> import java.awt.*;
> public class TestFrame extends Frame
> {
> Button bNorth = new Button("North");
> Button bSouth = new Button("South");
> Button bEast = new Button("East");
> Button bWest = new Button("West");
> Button bCenter = new Button("Center");
> public TestFrame()
> {
> setLayout(new BorderLayout());
> add(bSouth,BorderLayout.SOUTH);
> add(bWest,BorderLayout.WEST);
> add(bEast,BorderLayout.EAST);
> add(bNorth,BorderLayout.NORTH);
> add(bCenter);
>
> setLayout(new FlowLayout());
>
> validate();
> pack();
> setVisible(true);
> }
>
> public static void main(String args[])
> {
> TestFrame tf = new TestFrame();
> }
>
> }
>
> What will be the effect trying compile and run the above class?
>
> Answers
> 1. Compilation error - a Layout cannot be set twice for a component.
> 2. Compilation error - One button is added without specifing the position in
the borderLayout
> 3. No Compilation Error. The Buttons are arranged in a line in the order
(From left to right) "North","South","West","East" and "Center".
> 4. No Compilation Error. The Buttons are arranged in a line in the order
(From left to right) "South","West","East","North" and "Center".
> 5. No Compilation Error. The Buttons are arranged in the north , south, west,
east and center regions, as in a borderlayout. Any further additions will
follow the rules of FlowLayout manager.
>
> Thanking in advance,
>
> Alokesh
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Alokesh,
In the first example, the buttons are added before BorderLayout is set. BorderLayout implements LayoutManager2. All layouts implementing this interface keep track of their components; they do not query the container. In this example, no components were added while BorderLayout was active so it thinks it has none and therefore displays nothing.
In the second example, the last layout set is FlowLayout, which implements LayoutManager. All layouts implementing LayoutManager query the container for a list of components; therefore, all the components added to the container are visible.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: layout managers