I am having trouble getting this program to work. This is my first applet and I can’t seem to get my mind around how to get the commands in correct format. I have completed this program in application and gui with no problems, but applet format is not working in my favor. Any help would be much appreciated. Thanks in advance.
Erich

[TEX] 
/* Java applet that helps students with their multiplication.  This program will
   ask the student a multiplication problem and then display their resluts(correct/wrong).
   The program also needs the have the math.ramdom option to keep different question.
  */ 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 

public class JMath extends JApplet implements ActionListener
{
  
  int Number1 = 0;
  int Number2 = 0;
  int ActualAnswer = 0;

  Number1 = (int)( Math.random () * 9 ); 
  Number2 = (int)( Math.random () * 9 );
  ActualAnswer = Number1 * Number2;
 
  JLabel Mathout = new JLabel (" MATH FUN ");
  Font headlineFont = new Font ("Helvetica", Font.BOLD, 38);
  JLabel MathAnswer = new JTextField (2);
  JButton CheckAnswer = new JButton (" Check Answer ");
  JLabel MathQuestion = new JLabel ("What is " + Number1 + " TIMES " + Number2 + " ? ");
  ActualAnswer = Number1 * Number2;
  JLabel Correct = new JLabel ("Correct");
  JLabel Wrong = new JLabel ("Try again ");
  container con = getContentPane();
   
  public void init ()
{ // publicopen
   Mathout.setFont (headlineFont);
   MathQuestion.setFont (headlineFont);
   con.add (Mathout);
   con.add (MathQuestion);
   con.add (MathAnswer);
   con.add (CheckAnswer);
 
   
} // public end

   public void actionPerformed (ActionEvent e)
{ // openaction

   ActualAnswer = Number1 * Number2;
   int UserAnswer = MathAnswer.getText();
   
  
    if (UserAnswer = ActualAnswer)
 con.add (Correct);
    
    else
        con.add (Wrong);
        
      

} // end
}

[/TEX]

sorry, i put my reply in the wrong place...(never too old to learn)

I am having trouble getting this program to work. This is my first applet and I can’t seem to get my mind around how to get the commands in correct format. I have completed this program in application and gui with no problems, but applet format is not working in my favor. Any help would be much appreciated. Thanks in advance.
Erich

hi erich,

i played a little with your program and think it's ok now
i made several changes as you can see and i hope it is not too difficult to understand.
when you have anymore problems : let me know

wiederschauen

peter ;)

[tex] 
/* Java applet that helps students with their multiplication.  This program will
   ask the student a multiplication problem and then display their resluts(correct/wrong).
   The program also needs the have the math.ramdom option to keep different question.
  */ 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 

public class JMath extends JApplet implements ActionListener
{
  
  int Number1 = 0;
  int Number2 = 0;
  int ActualAnswer = 0;

  Number1 = (int)( Math.random () * 9 ); 
  Number2 = (int)( Math.random () * 9 );
  ActualAnswer = Number1 * Number2;
 
  JLabel Mathout = new JLabel (" MATH FUN ");
  Font headlineFont = new Font ("Helvetica", Font.BOLD, 38);
  JLabel MathAnswer = new JTextField (2);
  JButton CheckAnswer = new JButton (" Check Answer ");
  JLabel MathQuestion = new JLabel ("What is " + Number1 + " TIMES " + Number2 + " ? ");
  ActualAnswer = Number1 * Number2;
  JLabel Correct = new JLabel ("Correct");
  JLabel Wrong = new JLabel ("Try again ");
  container con = getContentPane();
   
  public void init ()
{ // publicopen
   Mathout.setFont (headlineFont);
   MathQuestion.setFont (headlineFont);
   con.add (Mathout);
   con.add (MathQuestion);
   con.add (MathAnswer);
   con.add (CheckAnswer);
 
   
} // public end

   public void actionPerformed (ActionEvent e)
{ // openaction

   ActualAnswer = Number1 * Number2;
   int UserAnswer = MathAnswer.getText();
   
  
    if (UserAnswer = ActualAnswer)
 con.add (Correct);
    
    else
        con.add (Wrong);
        
      

} // end
} 
 
[/tex]


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.