Help coderanch get a
new server
by contributing to the fundraiser
  • 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 question Tricky..

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the problem...
public class xyz extends Frame{
Button bNorth=new Button();
Button bSouth= new Button();
Button bEast=new Button();
Button bWest= new Button();
public xyz(){
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bEast);
add(bWest);

setSize(300,300);
SetVisible(true);
setLayout(new BorderLayout());
validate();
}
}

This is not displaying any of the buttons when we run, if we change the layout...ie., borderlayout first and flowlayoutnext we will be able to see them in a line.. why is so? pl. clarify my doubt..
Thanks in advance
Kiran
[This message has been edited by satya kiran (edited November 21, 2000).]
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
satya
I am able to run the program.This is the program which i compiled & ran .
import java.awt.*;
import java.awt.event.*;
public class xyz extends Frame{
Button bNorth=new Button("bn");
Button bSouth= new Button("bs");
Button bEast=new Button("be");
Button bWest= new Button("w");
public xyz(){
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bEast);
add(bWest);
setSize(300,300);
setVisible(true);
setLayout(new BorderLayout());

validate();
}
public static void main(String[] args){
xyz x=new xyz();

}
}
And i got the correct output.
I found out one more intersting point Assume if you run the following code
import java.awt.*;
public class xyz extends Frame{
Button bNorth=new Button("b");
Button bSouth= new Button("bs");
Button bEast=new Button("be");
Button bWest= new Button("w");
public xyz(){
setLayout(new FlowLayout());
add(bNorth);
add(bSouth);
add(bEast);

setSize(300,300);
setVisible(true);
setLayout(new BorderLayout());
add(bWest);
validate();
}
public static void main(String[] args){
xyz x=new xyz();

}
}
What would you expect?.I thought, as bWest is added to the borderlayout default position(center), that should occupy whole frame by hiding previously added buttons.But surprisingly it shows the previously added buttons on its surface.How is it possible.
Pls can anybody explain me this concept.
Regards
Prasad

------------------
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not getting the same output as you, but with that said this is what I think is happening. With the BorderLayout, if you don't specify the location (BorderLayout.SOUTH, BorderLayout.EAST, etc..) then BorderLayout.CENTER is being used.
Two things you have to remember is that with border layout, you can only have one component in a particular region and the center region will take the entire space after NORTH, SOUTH, EAST, and WEST are applied. So in this case, that would be the entire frame. Since you are placing buttons with no text on the button, then you are getting a frame filled with Button, which on first glance looks like a blank screen.
With the FlowLayout, it will place them all in a line with default 5 pixel spacing around the components, and that is what happens when you use the FlowLayout.
To make it easier for you to understand, put text on your buttons to make it more clear like Button bNorth = new Button ("bNorth");, etc.. Then you can see what is being added.
When I ran this, I got the FlowLayout when I had it first, and when I changed it so BorderLayout was first and FlowLayout was second. I only got BorderLayout (Frame's default) when I took FlowLayout out or both of them out altogehter.
Hope that helps,
Bill
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic