• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Layout Manager

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic