Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

pack()

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the result of executing the following Java class:
import java.awt.*;
public class FrameTest extends Frame {
public FrameTest() {
add (new Button("First"));
add (new Button("Second"));
add (new Button("Third"));
pack();
setVisible(true);
}
public static void main(String args []) {
new FrameTest();
}
}
Select from the following options:
A.Nothing happens.
B.Three buttons are displayed across a window.
C.A runtime exception is generated (no layout manager specified).
D.Only the "first" button is displayed.
E.Only the "second" button is displayed.
F.Only the "third" button is displayed.
Select the most appropriate answer. What do the pack() method do. Can someone help me.
[This message has been edited by Deepak Jindal (edited February 01, 2001).]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are not specfiying the compass direction of the buttons, they are added by default to the center of the frame. So, buttons get replaced one upon the other and finally, only the last button added is displayed, occupying the entire frame.
Hope this helps!.
thanx.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,
The correct choice is F,Only the "third" button is displayed.
This is b'cos the default layout for a frame is the BorderLayout and the default region for a BorderLayout is CENTER.Since the buttons don't specify the region,the default is assumed and consequently only the last component added in the Center gets displayed!..
As for the pack() method,it results in the window size being set to match the preferred size of the sub-components.
The BorderLayout honors
1)the preferred height of the components in the North & South regions,
2)the preferred width of the components in the East & West regions,
3)The Center takes whatever space is left-over.
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic