• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Q#16 Marcus Green Layout Managers

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 16)
What most closely matches the appearance when this code runs?
import java.awt.*;
public class CompLay extends Frame{
public static void main(String argv[]){
CompLay cl = new CompLay();
}
CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button("One"));
p.add(new Button("Two"));
p.add(new Button("Three"));
add("South",p);
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
}
1) The buttons will run from left to right along the bottom of the Frame
2) The buttons will run from left to right along the top of the frame
3) The buttons will not be displayed
4) Only button three will show occupying all of the frame
The panel is being added with the add methd version of
add("South",p); this seems ok for borderlayout , for flow layout out what is the meaning of south???
If some one can explain ...Thanks in advance
Wali
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Whenever you set layout manager again , it overrides the previous one. So when we set flowlayout after adding buttons( which were of course added with border layout ) , java reformats those buttons with the latest layout manager set.
Thanks
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
WaliM,
I have not tried out this code but this what I believe will be the output.
For Frame - BorderLayout is default.
For Panel - FlowLayout is default.
So, In the Panel three buttons will be added and the panel is being added to the South of Frame. So, three buttons should be seen at the bottom of the Frame. The setLayout(new FlowLayout()) is done after the panel has been added so it should not affect the panel or the buttons in the panel.
This is what I thought!!! But, actually u see three buttons at the top centered as they would be in a FlowLayout!!. So, moral of the story is that the components(e.g. button) added to another component (e.g. panel) follow the layout of the container(i.e. panel in this case). But, when the final Layout is set, it will play a role for those components which are being explicitly added to the Frame. Nice one!!
Ankur
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here I found something really useful. I think (feel free to correct me) for Frame Layout manager comes in action when u specify setVisible(true).
So If u change something ( like layout manager) before setVisible(true) command it will take into account. But if u change your program something like:
import java.awt.*;
public class CompLay extends Frame{
public static void main(String argv[]){
CompLay cl = new CompLay();
}
CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button("One"));
p.add(new Button("Two"));
p.add(new Button("Three"));
add("South",p);
setSize(300,300);
setVisible(true);
setLayout(new FlowLayout());
}
}

Then it will not take new layout manager into account.
Now add two more lines into code and see new layout manager is in action.
import java.awt.*;
public class CompLay extends Frame{
public static void main(String argv[]){
CompLay cl = new CompLay();
}
CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button("One"));
p.add(new Button("Two"));
p.add(new Button("Three"));
add("South",p);

setSize(300,300);
setVisible(true);

setLayout(new FlowLayout());
add(new Button("vivek"));

setVisible(true);

}
}

Hope this will help u.
please feel free to correct me.
vivek
[This message has been edited by Vivek Shrivastava (edited July 10, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 10, 2000).]
[This message has been edited by Vivek Shrivastava (edited July 10, 2000).]
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic