[java] Need help with first applet

Started by
8 comments, last by MetroidHunter 20 years, 11 months ago
I wrote my first applet today. It compiles with jsdk1.4.0, and I was able to load it with an html file using AppletViewer. However, I can't get it to show up in Internet Explorer. *edit* It says it can not find the class, even when I specify the path, and I have the Java plug-in installed. Can anyone help me out? Here's my code...
      
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Guess extends JApplet implements ActionListener
{
  int number, count;
  String user;
  FlowLayout flow = new FlowLayout();
  JTextField answer    = new JTextField("",5);
  JTextField player    = new JTextField("",20);
  JTextField hint      = new JTextField("Pick a number between 1 and 100",35);
  JLabel     question  = new JLabel("Can you guess my number?");
  JLabel     name      = new JLabel("Enter your name: ");
  JButton    makeGuess = new JButton("Guess!");
  JButton    enterName = new JButton("Let's play!");

  public void init()
  {
    Container con = getContentPane();
    number = (int)(Math.random() * 100) + 1;
    con.add(question);
    con.setLayout(flow);
    con.add(answer);
    con.add(makeGuess);
    con.setLayout(flow);
    con.add(hint);
    con.add(name);
    con.add(player);
    con.add(enterName);
    answer.setEnabled(false);
    makeGuess.setEnabled(false);
    hint.setEnabled(false);
    enterName.addActionListener(this);
    player.addActionListener(this);
  }

  public void actionPerformed(ActionEvent thisEvent)
  {
    Object source = thisEvent.getSource();
    if(source == makeGuess || source instanceof JTextField)
    {
      int guess = Integer.parseInt(answer.getText());
      count++;
      if (guess == number)
      {
        hint.setText("You got the number (" + guess + ") in " + count + " tries.");
        count = 0;
        number = (int)(Math.random() * 100) + 1;
      }
      else if (guess < number && guess > 0)
           {
             hint.setText("You're a little low. (" + guess + ")");
           }
           else if (guess > number && guess < 101)
                {
                  hint.setText("You're a little high. (" + guess + ")");
                }
                else
                {
                  hint.setText("You're a moron that can't follow directions.");
                }
       answer.setText("");
    }
    if(source == enterName)
    {
      user = player.getText();
      remove(name);
      remove(player);
      remove(enterName);
      answer.setEnabled(true);
      makeGuess.setEnabled(true);
      repaint();      
    }
  }
}      
and here is the html code
  
<HTML>
<HEAD>
<TITLE>Guessing Game</TITLE>
</HEAD>
<BODY>
<APPLET CODE = "Guess.class" WIDTH = 400 HEIGHT = 140>
</APPLET>
</BODY>
</HTML>
  
Any ideas? [edited by - MetroidHunter on May 1, 2003 4:50:09 PM]
Advertisement
Is your html and class file in the same directory?


First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
What is the version of te hJava Virtual Machine running in you Internet Explorer?


"And then 2 men appeared... Men in dark suits.. with dark soulless eyes. Men like this could have come from only one place..
The bank."
Yes, the class file and html file are both in the same directory. How do I find out which version of the JVM my browser is running?
It sometimes helps to add a "./" before your class file. e.g. "./Guess.class".
the ./ didn''t work either
view -> java console,
You might need to go to internet options to get that option under the view menu...


"And then 2 men appeared... Men in dark suits.. with dark soulless eyes. Men like this could have come from only one place..
The bank."
Which version of Explorer are you using? Since you''re using a lot of Swing objects, most browsers are not going to be able to run your code. I don''t have IE 6.x, but any 5.5''s and below I''ve seen only support Java 1.1, and Swing wasn''t introduced until 1.2 - does the error specify which class isn''t found? I suspect it''s not finding JApplet rather than your Guess class...
The version of the Java machine is:
Microsoft (R) VM for Java, 5.0 Release 5.0.0.3802

And I''m using IE 6.0
Even though you have Explorer 6.0, I am still guessing that the browser does not have the necessary version of Java to run Swing. You can download the latest version of Java and it should set up IE to use it as its plug-in JVM.

What if you try to write a similar applet that extends java.applet.Applet rather than javax.swing.JApplet? If you can write a simple Applet and have the browser find the class, then I think you''ve found the problem.

This topic is closed to new replies.

Advertisement