Re: [SLUG] Java question

From: Russ Wright (rwrigh10@tampabay.rr.com)
Date: Sat Nov 01 2003 - 22:00:22 EST


Okay I created the public void start() function and moved the code that
generates the statusbar message to there. I still don't see the message.

> You're doing everything inside the init() method, which is actually
> supposed to be used for pre-runtime initialization. Evidently updating
> the status bar doesn't actually do anything until the applet begins to
> execute (i.e. the start() method is called). If you change:
>
> public void init()
>
> to:
>
> public void start()
>
> The code seems to work perfectly.

My Modified Code:
/*
 * MathTrainer.java
 *
 * Created on October 31, 2003, 6:56 PM
 */

/**
 *
 * @author rwright
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;

public class MathTrainer extends JApplet implements ActionListener {
    
    //gui label for question
        JLabel questionLabel;
        JTextField answerField;
        JButton evaluateButton,startButton,stopButton;
     
    //my two mupltipliers
    int m_intValue1 = 0,m_intValue2 = 0;
    
    /** Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    
    public void init()
    {
        Container cont = getContentPane();
        cont.setLayout(new FlowLayout());
        
        //set up the gui
        //label for input text
        questionLabel = new JLabel ( "Your Answer:" );
        //add the label
        cont.add(questionLabel);
        
        //textbox for input
        answerField = new JTextField(10);
        cont.add(answerField);
        
        //Evaluate button
        evaluateButton = new JButton( "Check Answer" );
        //add a listener
        evaluateButton.addActionListener(this);
        //add the button to the gui
        cont.add(evaluateButton);
        
        
    } //init
    
    
    public void start(){
         //ask the first question
        //generate a question and display it
        generateQuestion();
       
    }
    
    
    
    public void actionPerformed(ActionEvent e) {
        
            //call event to check the answer
            checkAnswer();
        
        
    }
    
    public void checkAnswer()
    {
       //evaluate the users response
        boolean isCorrect = evaluateAnswer();
        
        
        //if it is correct affirm user and display a new question
        
        if (isCorrect == true)
        {
                //tell the user they got it right
                JOptionPane.showMessageDialog(null,"Correct!");
                
                //ask a new question
                    generateQuestion();
        }
        else
        {
            //if it is incorrect tell user and wait for new response
            //tell the user they got it wrong
                JOptionPane.showMessageDialog(null, "Wrong Answer Try
Again!");
            
        }
        
    }//checkanswer
  
    //function to evaluate user answer
     public boolean evaluateAnswer()
    {
        //get users response
        long userAnswer = Long.parseLong(answerField.getText() );
        
        //compare user's response to answer
        if ((m_intValue1 * m_intValue2) == userAnswer)
        {
            //if the answer is correct return true
            return true;
            
        }
        else
        {
            //if the answer is incorrect return false
            return false;
        }
        
    }//evaluateAnswer
    

    public void generateQuestion()
    {
        //create the 2 random numbers
        //assign their values to the module level variables
        m_intValue1 = 1 + ( int) ( Math.random() * 9);
        m_intValue2 = 1 + ( int) ( Math.random() * 9);
                
        //assemble message
        String msg = "What is " + m_intValue1 + " times " + m_intValue2 + "?";
        
        //display message in statusbar
        showStatus( msg );
       
        
    }//generateQuestion
 
}//mathTrainer

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:36:33 EDT