• 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

Why the button doesn't appears in the Frame?

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
When I compile and run the code above , but no component is visible,Why?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gong James:
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();
}
}
When I compile and run the code above , but no component is visible,Why?


Can u please delete or comment the following line
setLayout(new BorderLayout());
and then try.

 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course you are right,but you mistaked my meaning.
you know java allow you to change the layoutmanager,when the layoutmanager is changed ,the container will layout the component with it's rule.

Concern this case ,i changed the layoutmanager to flowlayout manager,but the button doesn't appear as my plan.
Why?or What's the rule when layout manager is changed from border layout to flow layout ?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
The problem is after line
setLayout(new BorderLayout());
Pay attention to the following rules:
1. Construct the frame.
2. Give the frame a layout manager.
3. Populate the frame (in you code above you omit this step)
...
So now simply add your button after line (means frame is populated with the components)
setLayout(new BorderLayout());
and it'll work.
------------------
Alex J.Grig
[This message has been edited by Alex Grig (edited October 31, 2001).]
 
Alex Grig
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the "Complete Java 2 Certification Study Guide" we can summarise "After each container is constructed and assigned an appropriate layout manager, the conteiner is poppulated with the components it is to contain"
------------------
Alex J.Grig
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alex is right, but I'd put this in a different way. BorderLayout implements LayoutManager2, which means that it keeps it's own list of components.
To be more specific, if you added some components to a container and then apply a layout that implements LayoutManager2, this one attempts to redraw the components from own list. But his list is empty because all the components have been added before the layout beeing set.
This is why you don't see anything.
Adrian
 
Ranch Hand
Posts: 1072
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
raj_sb
Would you kindly read our naming policy and re-register with a proper name?
www.javaranch.com/name.jsp
thanks
 
Not so fast naughty spawn! I want you to know about
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic