Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about awt

 
kashif sohail
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.awt.*;
import java.applet.Applet;
public class tcenter extends Applet
{
public void init(){
setLayout(new BorderLayout());
Panel p2=new Panel();
p2.add(new Button("n"),BorderLayout.NORTH);
p2.add(new Button("s"),BorderLayout.SOUTH);
Panel p =new Panel();
p.setBackground(Color.white);
this.add(p,BorderLayout.CENTER);// 1
p.add(p2,BorderLayout.CENTER);
}
}
what is this refer to in line 1 as if i replace it with tcenter it gives me error
2: why the result is like flow layout i.e. two buttons appearing in center not on north and south
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kashif,
First you should be aware that both Applet and Panel have FlowLayout as the default. Your code has only changed the Applet (this) to have a BorderLayout. Therefore, your panel p2 of which you are placing your buttons onto has a FlowLayout.
Also your panel P has a FlowLayout also.
To get the correct result you need to create your Panel with the following command:
Panel newPanel = new Panel( new BorderLayout() );
Then your example should give you what you want ...
Enjoy,
Manfred.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic