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]