Hi everyone! This code runs without throwing exceptions and stuff, but it doesn't exactly run when I click the "Submit" button -- what is supposed to happen is that I click it and it generates a random number. What happens is either nothing or if I get lucky and click random places repeatedly on the screen something shows up Dx

So what do I do to fix this "lag" and make the applet work correctly on the click of a button?

//import necessary packages
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// Inherit the applet class from the class Applet
public class FirstApplet extends JApplet implements ActionListener
      private JLabel Maximum;
      private JLabel Minimum;
      private JButton Submit;
      private JLabel Result;
      private JTextField UserMax;
      private JTextField UserMin;
      private String ultimo="";

      // init - method is called the first time you enter the HTML site with the applet
      public void init()
          this.setLayout(new FlowLayout());
          Maximum = new JLabel("Enter the maximum value: ");
          Minimum = new JLabel("Enter the minimum value: ");
          Submit = new JButton("Roll Die");
          ultimo = "The die landed on a ";
          Result = new JLabel(ultimo);
          UserMin = new JTextField(4);
          UserMax = new JTextField(4);
      public void actionPerformed (ActionEvent e)
          String in1, in2;
          int max, min, range, randomization;
          if (e.getSource() == Submit)
                  in1 = UserMax.getText();
                  in2 = UserMin.getText();
                  if (containsOnlyNumbers(in1) && containsOnlyNumbers(in2))
                      max = Integer.parseInt(in1);
                      min = Integer.parseInt(in2);
                      range = max-min;
                      randomization = (int)(Math.random()*range+min);
              catch (Exception ex)
                  randomization = 0;
              if (Result!=null)
              Result = new JLabel(ultimo+randomization);
      public static boolean containsOnlyNumbers(String str)
        //It can't contain only numbers if it's null or empty...
        if (str == null || str.length() == 0)
            return false;
        for (int i = 0; i < str.length(); i++) {

            //If we find a non-digit character we return false.
            if (!Character.isDigit(str.charAt(i)))
                return false;
        return true;
VernonDozier commented: Code tags on first post! +22

Lines 60 - 64 : Don't add and remove elements from the Applet. You already have a JLabel called Result. Keep it. Just change the text inside of it.

String textNumber = Integer.toString (randomization);
String newLabelContents = ultimo + textNumber;
Result.setText (newLabelContents);

Line 51 - You may want to consider creating your random numbers using a Random object instead of using Math.random () , multiplying, then typecasting.

Random random; // class variable
random = new Random (); // initialize in init ()
randomization = random.nextInt (range + 1) + min; // in actionPerformed


TYVM It works now!!! xD