This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abhilash Quiz - Question on Layout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Abhilash Quiz - Question on Layout" Watch "Abhilash Quiz - Question on Layout" New topic
Author

Abhilash Quiz - Question on Layout

Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
The following question is from Abhilash's quiz, Question 39.

The above code compiles and runs cleanly. However none of the buttons are displayed on the screen. Why is this happening?

Khalid Bou-Rabee
Ranch Hand

Joined: Jul 12, 2000
Posts: 54
It seems to be a conflict between LayoutManager and LayoutManager2...
It's a little late, need coffee... :|
Khalid


\\ //<BR>~\// irucidal~
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
I think we need help of java experts.
Hellooooooooo??

Any help would be appreciated here.
vivek
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
It must be because BorderLayout uses constraints like north, south and FlowLayout doesn't use any.
What do you think be displayed if you set the layout to GridBagLayout?
Sandra Marti
Ranch Hand

Joined: Jun 08, 2000
Posts: 63
This has been discussed here
sasank manohar
Ranch Hand

Joined: Feb 14, 2008
Posts: 186
Hi,
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());
//setLayout(new GridLayout());
add(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
setLayout(new FlowLayout());
//setLayout(new GridLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

For FlowLayout and GridLayout you can set the layout after adding the components using add() method. I mean ,It doesn't matter , if you set the layout before or after adding components.
But for BorderLayout, the constraint must be one of the following strings: "North", "South", "East", "West", or "Center".
And you must set the layout before adding the components, this is also not necessary if you are using for the container Frame. As u know BorderLayout is the default Layout manager for Frame.

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(bNorth,BorderLayout.NORTH);
add(bSouth,BorderLayout.SOUTH);
add(bWest,BorderLayout.CENTER);
add(bEast,BorderLayout.EAST);
add(bCenter,BorderLayout.WEST);
setLayout(new BorderLayout());

setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}

If you have set the layout as BorderLayout or used the container Frame . And you are setting the layout before adding the components. While adding the components you must mention the constraints of NORTH, SOUTH ....etc., . Or if you donot mention the constraints then you can see only the last component occupying the whole frame. That is because it overlaps all other components , if you don't mention the constraints..



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(bNorth);
add(bSouth);
add(bWest);
add(bEast);
add(bCenter);
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
In this program only the Button "Center" will display occupying the whole frame and overlaping all other buttons.
Thanks
sdev


"SCJP5 | SCWCD5| DEVELOPER"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abhilash Quiz - Question on Layout
 
Similar Threads
Why the button doesn't appears in the Frame?
java.awt BorderLayout and FlowLayout
Why the button doesn't appear in th frame?
AWT Layout
Layout