Beginner's question

J

Jacky Luk

import java.awt.Graphics;

public class printtest extends java.applet.Applet
{
public void init()
{
}

public void paint (Graphics g)
{
g.drawString ("Hello from java!", 60, 30);
}

public static void main (String args)
{

}
}
===============================
java.lang.NoSuchMethodError: main
Exception in thread "main"

What happened?
Thanks
Jack
 
T

Thomas Hawtin

Jacky said:
public class printtest extends java.applet.Applet

public static void main (String args)
> {

That should be:
public static void main(String[] args) {

But it still wont do anything.

Applets run in web pages. You'll need something like this in an HTML
file (in the same directory as your class file):

<applet class="printtest.class" width="100" height="100"></applet>

Then either view the web page in your browser (if it has a sufficiently
recent version of Java enabled) or use appletviewer.

Tom Hawtin
 
J

Jacky Luk

How do you make the paint method work?
Thanks
Jack

Thomas Hawtin said:
Jacky said:
public class printtest extends java.applet.Applet

public static void main (String args)
{

That should be:
public static void main(String[] args) {

But it still wont do anything.

Applets run in web pages. You'll need something like this in an HTML file
(in the same directory as your class file):

<applet class="printtest.class" width="100" height="100"></applet>

Then either view the web page in your browser (if it has a sufficiently
recent version of Java enabled) or use appletviewer.

Tom Hawtin
 
T

Thomas Hawtin

Jacky said:
How do you make the paint method work?

Works for me... My entire (Java 1.5) code:

import java.awt.Graphics;

public class PrintTest extends java.applet.Applet {
@Override
public void paint(Graphics g) {
g.drawString("Hello from java!", 60, 30);
}
}

<applet code="PrintTest.class" width="100" height="100"></applet>

(Note that's code= not class= as I said earlier.)

Are you sure it's running? No exception messages in the terminal/console
for appletviewer or Java console of your web browser?

Tom Hawtin
 
J

Jacky Luk

Severity Description Resource In Folder Location Creation Time Id
2 The public type PrintTest must be defined in its own file printtest.java
test1 line 3...
1 The serializable class PrintTest does not declare a static final
serialVersionUID field of type long printtest.java test1 line 3...
 
A

Andrew Thompson

Thomas said:
Jacky said:
public class printtest extends java.applet.Applet
.....
public static void main(String[] args) { ....
But it still wont do anything.

Sure it will*. Adding a main allows you to run your applet as
either an applet or an application. Most of my applets have mains
(even if I never intend to use them as applications) simply
because it can be so much quicker developing something
you can simply launch from the command line.

* So long as the main creates a frame, instantiates the
applet class, calls init() and adds it to the frame.

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top