File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes applet&buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "applet&buttons" Watch "applet&buttons" New topic
Author

applet&buttons

g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
hi this prints 1 button with label "Ok" and 4 buttons with label "cancel", pls explain, thanks.

import java.applet.*;
import java.awt.*;
public class Q16 extends Applet {
Button okButton = new Button("Ok");
public void init()
{
add(okButton);
add(okButton);
add(okButton);
add(okButton);
add(new Button("Cancel"));
add(new Button("Cancel"));
add(new Button("Cancel"));
add(new Button("Cancel"));
setSize(300,300); }
}
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Because okButton was only created once while you are creating 4 new cancel buttons (they all are added with the keyword new.)
Bill
g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
hi bill,
can't we add a same button again, for any no.of times we want.
thanks
Winfried Gottschalk
Greenhorn

Joined: Mar 31, 2001
Posts: 3
Think about that:
if java would allow that you add one button object more than once to a panel what do you think would happen if you put an event lister to the botton object.
wingo
g krishnan
Greenhorn

Joined: Mar 30, 2001
Posts: 23
wingo thanks for that, this clarifies.
Parimala Somasundaram
Ranch Hand

Joined: Mar 31, 2001
Posts: 41
I think one way of explaining this is that every instance that you create creates an object. Since there is only one instance of okButton, it appears only once, whereas 4 instances of 'cancel' are created everytime you call the constructor using 'new'; hence the four cancel buttons.
Originally posted by g krishnan:
hi this prints 1 button with label "Ok" and 4 buttons with label "cancel", pls explain, thanks.

import java.applet.*;
import java.awt.*;
public class Q16 extends Applet {
Button okButton = new Button("Ok");
public void init()
{
add(okButton);
add(okButton);
add(okButton);
add(okButton);
add(new Button("Cancel"));
add(new Button("Cancel"));
add(new Button("Cancel"));
add(new Button("Cancel"));
setSize(300,300); }
}

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applet&buttons
 
Similar Threads
AWT
awt
Buttons
majii q#16
component on applet : from majji exam