WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
frame.addWindowListener(l);
inAnApplet = false;
drawing applet = new drawing();
applet.init();
frame.add(applet);
frame.pack();
frame.show();
}
}
but I am not successful because without the main() function, it works ok,
but with main() function, the mouse listener action reports error like:
Exception occurred during event dispatching:
java.lang.NullPointerException:
at java.applet.Applet.getAppletContext(Applet.java:146)
at java.applet.Applet.showStatus(Applet.java:186)
at drawing.mouseEntered(drawing.java:196)
....
so there must be something wrong with my main() function, can someone
tell me what I need to modify in order to make the applet work as appliation
too?
Thanks.
--
One way to make a quick fix is to override the showStatus method in your
applet:
public void showStatus(String msg) {
if (getAppletContext() != null) super.showStatus(msg);
}
If there is an applet context, the showStatus method will be executed,
otherwise, the super-classes' method will not be called.
A better way to do it would be to set a boolean variable of your applet to
indicate whether it is an application or not, and checking this boolean in
the overridden showStatus method:
public void showStatus(String msg) {
if (!runningAsApplication) super.showStatus(msg);
}
public static void main(String[] args) {
runningAsApplication = true;
//.....
//.....
}
It may also be necessary to call some of the Applet methods in the main
function so that the applet is initialized properly. That is, if your
applet overrides the init, start, or stop method, then it may be necessary
for the main function to call these methods...or the frame can do it
(Perhaps: init in constructor, start when visible, stop when not visible).
Yang Su wrote in message <6r2quh$c0g$1...@news.iastate.edu>...
In article <6r2quh$c0g$1...@news.iastate.edu>,
yan...@cs.iastate.edu wrote:
> For example, I have an applet :
> class drawing extends Applet implement MouseListener {
> ....
> // try to make this applet serve as application too
> public static void main(String[] args) {
> Frame frame = new Frame("Drawings");
etc etc
You need to include more of your code. I guess that the problem is in your
code for MouseListener and you haven't included that. Marcus
>
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum