• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Question about Layout Managers

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following applet, how many Buttons will be displayed?

1: import java.applet.*;
2: import java.awt.*;
3:
4: public class Q16 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: add(okButton);
11: add(okButton);
12: add(okButton);
13: add(okButton);
14:
15: add(new Button("Cancel"));
16: add(new Button("Cancel"));
17: add(new Button("Cancel"));
18: add(new Button("Cancel"));
19:
20: setSize(300,300);
21: }
22: }
A) 1 Button with label "Ok" and 1 Button with label "Cancel" .
B) 1 Button with label "Ok" and 4 Buttons with label "Cancel" .
C) 4 Buttons with label "Ok" and 1 Button with label "Cancel" .
D) 4 Buttons with label "Ok" and 4 Buttons with label "Cancel" .

The answer was given to be B but I thought it would be A.
Can someone please explain ?
Thanks,
Srini
 
srini bhav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I sent the wrong question. Here is the right question.
import java.applet.*;
2: import java.awt.*;
3:
4: public class Q20 extends Applet
5: {
6: Button okButton = new Button("Ok");
7:
8: public void init()
9: {
10: setLayout(new BorderLayout());
11:
12: add("South", okButton);
13: add("North", okButton);
14: add("East", okButton);
15: add("West", okButton);
16: add("Center", okButon);
17:
18: setSize(300,300);
19: }
20: }
The above Applet will display
A) Five Buttons with label "Ok" at Top, Bottom, Right, Left and Center of the Applet.
B) Only one Button with label "Ok" at the Top of the Applet.
C) Only one Button with label "Ok" at the Bottom of the applet.
D) Only one Button with label "Ok" at the Center of the Applet.


The answer for this is given to be B.
Can someone explain ?
Thanks,
Srini
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The layout manager is smart enough to see the same object added to the container more than once and ignore multiple adds. So you end up with only one OK button. The Cancel button, on the other hand, is instantiated each time it is added. So while they all look the same, they're difference instances, and the layout manager sees them as such.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled that code, the result is:
one OK button at the center of the applet.
Your add() method is of old version. If you want to
use the ability of BorderLayout, you must use
the form add(okButton, BorderLayout.SOUTH); and etc.
Otherwise the button will be added by default to the center.
Even if you use this form, okButton will be added
only once. Because the last sentence is center, so the
button will be at center.
 
srini bhav
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks but does that mean that you cannot add the same button to all the 5 positions (North, south, east, west and center) ?
 
ego hu
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YES.
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic