Question:
Netbeans java applet problem?
2010-08-21 18:27:00 UTC
I use netbeans, and I am trying to make the simplest applet in the world. Basically a hello world. The problem is that it displays this error every time I try to run my applet:

Java Plug-in 1.6.0_20
Using JRE version 1.6.0_20-b02-279-10M3065 Java HotSpot(TM) Client VM
User home directory = /Users/BradenSteffaniak

this is my code to the applet:


import java.applet.Applet;
import java.awt.Graphics;

public class maincode extends Applet {
@Override
public void paint( Graphics g ) {
g.drawString("test", 5, 5);
}
}

here is the code to the html page:








They are in the same directory. I just cant seem to find the problem with this. Please help.
Three answers:
modulo_function
2010-08-21 21:20:31 UTC
I did some fooling around with it...while looking here:

http://download.oracle.com/javase/tutorial/deployment/applet/lifeCycle.html



You can't run it with the tool bar arrow. You can run it by pulling down the run tab and choosing 'run file'. You will then see that an html file is created. The applet viewer is called and this program works.



However, when I navigate over to the html file and try to open it, I get just a black box in Firefox. I don't see any error messages, however! But, take a look at the page source and you'll see how to designate locations. Here it is on mine:











package BradenJApplet;



import java.awt.Graphics;



import java.applet.Applet;



public class maincode extends Applet {



StringBuffer buffer;



public void init() {

buffer = new StringBuffer();

addItem("initializing... ");

}



public void start() {

addItem("starting... ");

}



public void stop() {

addItem("stopping... ");

}



public void destroy() {

addItem("preparing for unloading...");

}



private void addItem(String newWord) {

System.out.println(newWord);

buffer.append(newWord);

repaint();

}



public void paint(Graphics g) {

g.drawString("test using Applet & init(), etc", 150, 100);

//Draw a Rectangle around the applet's display area.

g.drawRect(0, 0,

getWidth() - 1,

getHeight() - 1);



//Draw the current string inside the rectangle.

g.drawString(buffer.toString(), 25, 15);

}

}



+add

Unca Alby: I'm surprised that you don't know about override. It does exist in Java. A search for "java override" gets a link to the tutorial site:

http://download.oracle.com/javase/tutorial/java/IandI/override.html



When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error. For more information on @Override, see Annotations.
2016-03-15 11:18:33 UTC
It sounds like you do not have a main method in your application. Every Java application compiles from a main first. Check to ensure you have the following code: public static void main(String[] args) { } This code should be inside your class to compile your application properly. That's my best guess to solve your problem based on the information you provided.
Unca Alby
2010-08-21 19:27:07 UTC
What is that "@Override" ??



I have never seen it before, and a quick search of the web doesn't produce any other examples that use it.



Maybe you're confused with C++.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...