File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Layout Manager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Layout Manager" Watch "Layout Manager" New topic
Author

Layout Manager

Susie Chow
Ranch Hand

Joined: Jun 12, 2001
Posts: 33
Hi,
code #1 returns no component on the frame. However, code #2 returns a list of button in a row. Can anyone explain to me why? My thought is that code #1 will return one big button C at the center of the frame because when you add an element in borderLayout w/o specifying the location, it would add the button to center.
Thank you very much.
#1
class TestFrame extends Frame
{
Button bN = new Button("N");
Button bS = new Button("S");
Button bE = new Button("E");
Button bW = new Button("W");
Button bC = new Button("C");
public TestFrame()
{
setLayout(new FlowLayout());
add(bN);
add(bS);
add(bW);
add(bE);
add(bC);
setLayout(new BorderLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
TestFrame tf = new TestFrame();
}
}
#2
class TestFrame extends Frame
{
Button bN = new Button("N");
Button bS = new Button("S");
Button bE = new Button("E");
Button bW = new Button("W");
Button bC = new Button("C");
public TestFrame()
{
setLayout(new BorderLayout());
add(bN, BorderLayout.NORTH);
add(bS, BorderLayout.SOUTH);
add(bW, BorderLayout.WEST);
add(bE, BorderLayout.EAST);
add(bC, BorderLayout.CENTER);
setLayout(new FlowLayout());
validate();
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
TestFrame tf = new TestFrame();
}
}
Christophe Lee
Ranch Hand

Joined: Jul 10, 2001
Posts: 142
This topic was discussed in another thread and I'd provide the link, but I'm on a terribly slow connection, so you'll have to do a search.
But I remember the conclusion from that thread was basically:
BorderLayout keeps a list of its own components separate from the container. When BorderLayout goes to format the positions, it checks its own list. In this case, since the components were all added before BorderLayout was set, then its list contains no components and therefore the blank screen.
FlowLayout, on the other hand, does not keep its own list and refers to the container's list of components and therefore, sees the components added to the Frame before it was set.
I guess the key is to know there are two lists of components being kept separately.
Regards,
Chris
 
wood burning stoves
 
subject: Layout Manager