简体   繁体   中英

Why won't Applets run on NetBeans? And how do I get them to run?

I use NetBeans for Java programming, and whenever I try to make an Applet, it doesn't work. The code is completely correct and I did not make any mistakes on it. When I hit "Run" a message appears in the output box that says, "BUILD SUCCESSFUL (total time: 0 seconds)" There are no error messages and no errors in the code, the Applet doesn't appear. The code is:

import java.applet.*;
import java.awt.*;
public class myProject extends Applet
{
public void init()
{

}
public void paint(Graphics g)
{
setSize(500,500);
}
public static void main(String[] args) 
{

} 
}
  1. Get rid of the main method. If you're running the code as an applet, main will just confuse you and the IDE.
  2. Make sure that you're telling NetBeans to run it as an applet, not as an application. Again, removing main should help you with this, since without main it can't run it as an app.
  3. Consider not creating applets since they're considered somewhat dead technology.
  4. Don't have setSize(...) within a paint method. Ever. GUI painting methods, such as paint(...) for AWT components and paintComponent(...) for Swing components derived from JComponent, should just do painting and nothing else.
  5. Check out the Java Swing tutorials which can be found here: Swing Info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM