JApplet is not working properly...

Hi there...

I am having a problem with my applet. Here is the code:

package click;

import javax.swing.*;

import java.awt.*;

import java.awt.event.MouseEvent;

import java.awt.event.MouseListener;

public class click1 extends JApplet implements MouseListener{

JPanel jp;

JButton b;

StringBuffer buffer;

String message;

public void init(){

jp = new JPanel();

jp.setBackground(Color.white);

b = new JButton("click");

b.addMouseListener(this);

jp.add(b);

getContentPane().add(jp, BorderLayout.WEST);

buffer = new StringBuffer();

}

public void paint(Graphics g){

g.drawString(buffer.toString(), 80, 30);

}

public void mouseClicked(MouseEvent arg0) {

buffer.append("Click");

repaint();

}

the problem is, when I start the applet, it appears as a blank white applet and the button does not appear untill I move my mouse over it. Also when i add this applet to a html page it appears as a box with red cross in the upper left corner. Any Ideas?

regards

[1122 byte] By [Raieda] at [2007-11-27 9:32:45]
# 1

In your system tray find the java icon, right click and select "Open Console". Then you should see any error messages.

Your basic problem is probably that you both add children and override the paint method. The default paint method is responsible for drawing children. You could call super.paint() in your paint method, but better to override paintComponent().

malcolmmca at 2007-7-12 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 2

This is what the consol shows:

java.lang.NoClassDefFoundError: click1 (wrong name: click/click1)

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at sun.applet.AppletClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.applet.AppletClassLoader.loadCode(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

but I still didn't get it :S

Raieda at 2007-7-12 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 3

The "wrong name" suggests you are supplying the .class files wrong on whatever you're using as a web server. Class files need to be either in a jar, or in a heirarchy of folders with the path to a class file mapping the package structure. Your class file click.class should be in a folder called "click" relative to where your applet tag says to look for the code.

The java compiler will create class files that way.

malcolmmca at 2007-7-12 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 4

How is this code even compiling? The MouseListener interface declares five methods (mouseClicked, mouseEntered, mouseExited, mousePressed, mouseReleased) and you've only provided an implementation for one of the methods. I suspect that's part of your problem.

paternostroa at 2007-7-12 22:51:48 > top of Java-index,Java Essentials,Java Programming...
# 5
By the way - get into the habbit of always starting class names with a capital letter. That's a universal convention.
malcolmmca at 2007-7-12 22:51:48 > top of Java-index,Java Essentials,Java Programming...