• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why the button doesn't appear in th frame?

 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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?
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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 ?
 
Dave Turner
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the buttons will not be positioned according to their names (eg bNorth), to position a component in the north of its container, you must first set the layout:
setLayout( new BorderLayout() );
then add the component to your desired location :
add( bNorth, BorderLayout.NORTH );
the Constant BorderLayout.NORTH indicates to the BorderLayout Manager that you wish bNorth to be located at the top of the container.
Hope this helps
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!