File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Layout Manager

 
Susie Chow
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic