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");
          Submit.addActionListener(this);
          ultimo = "The die landed on a ";
          Result = new JLabel(ultimo);
          UserMin = new JTextField(4);
          UserMax = new JTextField(4);
          add(Maximum);
          add(UserMax);
          add(Minimum);
          add(UserMin);
          add(Submit);
      }
      public void actionPerformed (ActionEvent e)
      {
          String in1, in2;
          int max, min, range, randomization;
          if (e.getSource() == Submit)
          {
              try
              {
                  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);
                  }
                  else
                      randomization=0;
              }
              catch (Exception ex)
              {
                  randomization = 0;
              }
              if (Result!=null)
                remove(Result);
              Result = new JLabel(ultimo+randomization);
          }
          add(Result);
      }
      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

http://java.sun.com/javase/6/docs/api/java/util/Random.html

TYVM It works now!!! xD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.