• 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

Jq + Question ID :970967626570

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the following program...
import java.awt.*;
public class MyFrame
{
public static void main(String[] args)
{
Frame fr = new Frame();
Panel p = new Panel();
Button b1 = new Button("North");
p.add(b1);
Button b2 = new Button("South");
fr.add("North", p);
fr.add("South", b2);
fr.setSize(300,400);
fr.setVisible(true);
}
}
Which of the following statements are correct?
1) On resizing the frame, both "North"button and "South"button will resize
2)On resizing, the height of "North" will change but "South" will not
3) On resizing, the width of "North" will change but "South" will not
4) On resizing, the width of "South" will change but width of "North" will not change
5)Nothing can be said about the behaviour.
Ans : 4)
Can anyone explain me the above question and how is the answer 4) right?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
The point here is the default layout for each AWT container used. For a Frame it is BorderLayout and for a Panel it is FlowLayout.
Since the Panel p is added as the North component into the Frame (BorderLayout) it will be resized when the Frame is resized. But, since p uses a FlowLayout manager its contents will remain at their preferred size. Therefore, button b1 will remain the same size even though the Frame gets resized.
On the South we have just placed a button b2 inside. Therefore, it will be the width of the Frame because the South section of a BorderLayout will retain its preferred hieght but will be resized to fit to the Frame width.
Regards,
Manfred.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sonir,
[p]
The reason the answer is #4 is that panel and frame have two different default layout managers. Panels are FlowLayout by default. This means that the components added will be arranged in a row (or rows if there are too many), centered, and evenly spaced. Each component will take up only it's preferred size, which for a button is the size of its label text and a small border around the text. So the button which has the text "North" is placed in a row of a panel.[/p] [p]The default layout for a frame is a borderlayout. So when you add the button to the south, it will stretch the width of the Frame. The panel is added to the north of the Frame, but the panel's layout has not been changed. So the panel itself is placed in the north & stretches the width of the frame, but the components in the panel will still be flow layout & therefore retain their preferred size. [/p]
Hope this helped!
--kkoszegi

Originally posted by sonir shah:
Consider the following program...
import java.awt.*;
public class MyFrame
{
public static void main(String[] args)
{
Frame fr = new Frame();
Panel p = new Panel();
Button b1 = new Button("North");
p.add(b1);
Button b2 = new Button("South");
fr.add("North", p);
fr.add("South", b2);
fr.setSize(300,400);
fr.setVisible(true);
}
}
Which of the following statements are correct?
1) On resizing the frame, both "North"button and "South"button will resize
2)On resizing, the height of "North" will change but "South" will not
3) On resizing, the width of "North" will change but "South" will not
4) On resizing, the width of "South" will change but width of "North" will not change
5)Nothing can be said about the behaviour.
Ans : 4)
Can anyone explain me the above question and how is the answer 4) right?


 
You had your fun. Now it's time to go to jail. Thanks for your help tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic