简体   繁体   English

来自init()的方法调用-Java

[英]Method call from init() - Java

I have been teaching myself Java for the past week or so. 在过去的一周左右的时间里,我一直在自学Java。 I'm brand new to programming. 我是编程的新手。 My code compiles just fine, but the applet does not display any text fields and the genRandomNumbers() method is not being invoked. 我的代码可以很好地编译,但是applet不会显示任何文本字段,并且不会调用genRandomNumbers()方法。 So the question prompting the user to multiply two numbers is not displayed on the applet. 因此,提示用户将两个数字相乘的问题不会显示在小程序上。

In my code, genRandomNumber() is being invoked from init() . 在我的代码中, genRandomNumber()init()调用。 Could that cause an issue? 会引起问题吗? I tried to do this from paint() instead. 我试图从paint()代替。 That caused bigger issues - none of the text fields were displaying on the applet. 这导致了更大的问题-小程序上没有显示任何文本字段。 So I moved the call to genRandomNumber() back to init() . 因此,我genRandomNumber()的调用移回了init()

The applet window shows "Start: applet window not initialized" in the Status area. "Start: applet window not initialized"在状态区域中显示"Start: applet window not initialized"

Could you point me in the right direction? 你能指出我正确的方向吗? Any help is greatly appreciated! 任何帮助是极大的赞赏!

Here is my code: 这是我的代码:

 //Generate 2 random numbers
 //Post a question to multiply the two numbers
 //Verify the answer entered
 //Post a new question if the solution is correct

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class LearnMultiplication extends JApplet implements ActionListener
{

   JLabel answerLabel;
   JTextField answerTextField, commentTextField, questionTextField;
   int random1, random2;

   public void init()
   {
   Container c = getContentPane();
   c.setLayout(new FlowLayout() );

   JTextField questionTextField = new JTextField(30);
   c.add(questionTextField);

   JLabel answerLabel = new JLabel("Enter you answer here");
   c.add(answerLabel);

   JTextField answerTextField = new JTextField(5);
   answerTextField.addActionListener(this);
   c.add(answerTextField);   

   JTextField commentTextField = new JTextField(30);
   c.add(commentTextField);

   genRandomNumbers(); // invoke method to generate 2 random numbers
   }



   public void actionPerformed (ActionEvent e)
   {
      int a = Integer.parseInt(e.getActionCommand() ); 
      verifyAnswer(a); // invoke method to verify the product
   }

   //method to generate 2 random numbers
   public void genRandomNumbers()
   {
        random1= 1 + (int)(Math.random() * 9    );    
        random2 = 1 + (int)(Math.random() * 9   );
        questionTextField.setText("Multiply " + Integer.toString(random1) + "and " + Integer.toString(random2) + ".");

  }  

   // method to verify the product of the 2 random numbers
   public void verifyAnswer(int answer)
   {
      int correctAnswer = random1 * random2;

      if ( correctAnswer == answer)
      {
         commentTextField.setText("Very Good!");
         genRandomNumbers(); //call the method again to generate 2 new random numbers
      }  
      else
      {
         commentTextField.setText("No, try again!!");
      }  

    }
}   

Basically, you have a series of NullPointerException s. 基本上,您有一系列的NullPointerException This is one of the reasons why applets aren't a great place to get started with programming, because unless you have the Java console enabled, you won't get any output from your program which might be useful... 这就是为什么小程序不是开始编程的好地方的原因之一,因为除非启用了Java控制台,否则程序将不会获得任何有用的输出...

Any way... 随便...

You declare the following instance variables... 您声明以下实例变量...

JTextField answerTextField, commentTextField, questionTextField;

Then in you init method you do this... 然后在您的init方法中执行此操作...

public void init() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    // Re-decleared/shadowed variable...
    JTextField questionTextField = new JTextField(30);
    c.add(questionTextField);

    JLabel answerLabel = new JLabel("Enter you answer here");
    c.add(answerLabel);

    // Re-decleared/shadowed variable...
    JTextField answerTextField = new JTextField(5);
    answerTextField.addActionListener(this);
    c.add(answerTextField);

    // Re-decleared/shadowed variable...
    JTextField commentTextField = new JTextField(30);
    c.add(commentTextField);

    genRandomNumbers(); // invoke method to generate 2 random numbers
}

You declare the questionTextField , answerTextField and commentTextField as local variables. 您将questionTextFieldanswerTextFieldcommentTextField为局部变量。 This is commonly known as shadowing. 这通常称为阴影。 Basically, when you think the instance variables have being initialised, they haven't, because you've used local variables instead... 基本上,当您认为实例变量已初始化时,它们还没有初始化,因为您改用了局部变量...

If, instead, you do something more like... 相反,如果您执行的操作更像是...

public void init() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    questionTextField = new JTextField(30);
    c.add(questionTextField);

    JLabel answerLabel = new JLabel("Enter you answer here");
    c.add(answerLabel);

    answerTextField = new JTextField(5);
    answerTextField.addActionListener(this);
    c.add(answerTextField);

    commentTextField = new JTextField(30);
    c.add(commentTextField);

    genRandomNumbers(); // invoke method to generate 2 random numbers
}

You should now find your instance variables have being intialised and are now accessiable as expected from the other parts of your class... 现在,您应该发现实例变量已经初始化,并且可以按预期从类的其他部分进行访问...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM