Advertisement Jump to content


This topic is now archived and is closed to further replies.


[java] Need help with first applet

This topic is 5740 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;

  public void actionPerformed(ActionEvent thisEvent)
    Object source = thisEvent.getSource();
    if(source == makeGuess || source instanceof JTextField)
      int guess = Integer.parseInt(answer.getText());
      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 + ")");
                  hint.setText("You're a moron that can't follow directions.");
    if(source == enterName)
      user = player.getText();
and here is the html code
<TITLE>Guessing Game</TITLE>
<APPLET CODE = "Guess.class" WIDTH = 400 HEIGHT = 140>
Any ideas? [edited by - MetroidHunter on May 1, 2003 4:50:09 PM]

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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...

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!