wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt related to applet and frames Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt related to applet and frames" Watch "doubt related to applet and frames" New topic
Author

doubt related to applet and frames

Maya Nair
Greenhorn

Joined: Jan 12, 2001
Posts: 3
I tried the following code taken from some mock exam:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class hello extends Applet
{
public void init()
{
add(new myButton("BBB"));
}
public void paint(Graphics g){}
class myButton extends Button
{
myButton(String label)
{
super(label);
}
public String paramString()
{
return super.paramString();
}
}
public static void main(String a[])
{
Frame myFrame=new Frame("Frame1");
myFrame.setSize(300,100);
Applet myapplet=new hello();
Button b=new Button("My Button");
myapplet.add(b);
b.setLabel(b.getLabel()+"New");
myButton b1=(new hello()).new myButton("ParamButton");
System.out.println(b1.paramString());
myFrame.add(myapplet);
myFrame.setVisible(true);
myFrame.addWindowListener(new WindowAdapter (){
public void windowClosing(WindowEvent e){
System.exit(0);} });
}
}// class hello ends
The above code when run as a standalone Application returns only one button ("MyButtonNew")
Why doesnt the other button("BBB") initialised in the applet appear?
One more thing, when u compile the class hello.java the following files are generated.
1)hello.class,
2)hello$myButton.class,
3)hello$1.class
The first two are understood but why the third one???
Could anyone please explain me as soon as possible??I am planning to give JCP by end of this Jan.
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
appletviewer does not run main(). hello$1.class file was created because of new WindowAdapter (). Let me know if I am wrong.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Maya,
When you run the code as an application vs an Applet you must explicitly call the Applet <code>init()</code> method.

Re-compile and run. You will see both buttons.
When you load an Applet in a browser, it handles the call to <code>init()</code>. When you load it from an application, your code must replicate the functionality provided by the browser.
Hope that helps.
Jane


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Maya Nair
Greenhorn

Joined: Jan 12, 2001
Posts: 3
Hi Jane,
That means if I run the code as an application then applets
init() wont get called,the class hello's constructor will be called which in this case will be default no-arg constr.
Actually I was under the impression that when u execute the line
Applet myapplet= new hello();it will first execute init() method of the applet and then continue further...but since init() is called by the browser,things are simpler.
now I got it!!
Thanks for the explaination.
Maya
 
 
subject: doubt related to applet and frames
 
Similar Threads
inner class
Anonymous Class
Frame and Applet together
Why 3 clss Files
why doesn't this method of "button" work?