• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Jq + Question ID :970967626570

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?


 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!