Need help with errors in simple code.
Hey guys I am new to Java programming and I making a simple program to test out a java applett. I keep getting errors that say
"C:\...\Test.java:14: <identifier> expected
container.add(outputArea);
^
C:\...\Test.java:17: <identifier> expected
outputArea.setText("dfgdgdfg");"
with my code, but I cannot figure out why. Can someone look at my code and see if they see anything obvious or not so obvious?
Thanks!
PHP Code:
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet {
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add(outputArea);
outputArea.setText("dfgdgdfg");
}
Re: Need help with errors in simple code.
Try putting all that code in a constructor:
Code:
import java.awt.*;
import javax.swing.*;
public class Test extends JApplet {
public Test()
{
JTextArea outputArea = new JTextArea();
Container container = getContentPane();
container.add(outputArea);
outputArea.setText("dfgdgdfg");
}
}
Re: Need help with errors in simple code.
Put the code within init().
Re: Need help with errors in simple code.
It's also still vaild to define a main() method. In your case when it terminates the there will still be one thread executing in the jvm. The awt.
Re: Need help with errors in simple code.
Just to add. About applets & constructors. One reason to use init() instead of a constructor is that some methods fail to work inside of an applets constructor like the image loading methods.
Re: Need help with errors in simple code.
Oops, sorry. Was thinking this was a JFrame instead of applet. :blush: