Archived

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

MetroidHunter

[java] Need help with first applet

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;
    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]

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