• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

awt

 
Greenhorn
Posts: 19
  • 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 is B, why?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because an instance of a Component can only be added once to a container. Here you are trying to add okButton several times.
Concerning the Cancel buttons, you add a new instance each time you invoke add.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
I was born with webbed fish toes. This tiny ad is my only friend:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic