• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removed or Hidden

 
yi miao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Code:
import java.awt.*;
public class TestFrame extends Frame
{
Button firstOne = new Button("One");
Button secondOne = new Button("Two");
public TestFrame()
{
add(firstOne,BorderLayout.NORTH);
add(secondOne,BorderLayout.NORTH);
setSize(400,400);
setVisible(true);
}
public static void main(String args[])
{
TestFrame tf = new TestFrame();
}
}
Question:
An Attempt to compile and run the above piece of code

A. Causes compilation error - a component cannot be added to region which is already occupied by another component.
B. Causes Runtime Exception - a component cannot be added to region which is already occupied by another component.
C. Neither A or B. The Frame comes up and only the button with label "Two" occupies the entire North region of the Frame.
D. Addition of secondOne causes firstOne to be removed from the container.
E. Addition of the secondOne causes the firstOne to be hidden in the container.
Answer: C&D. I know that C is correct. But I don't understand why D is correct, not E. I thought method only remove() removes conponents from a container.
 
Anoop Krishnan
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai Yi,
I think that the button 2 replaces the button 1 because you are adding both of them to the same location.That might be what the choice D signifies
 
yi miao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Anoop,
Yes, both of button 2 & button 1 were added to the same location. But my understand is that button 2 is right on the top of button 1, so it hides button 1. If button 1 is removed,
remove() method should be invoked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic