从init()调用方法 - Java

时间:2013-09-12 05:25:24

标签: java

过去一周左右,我一直在教自己Java。我是编程的新手。我的代码编译得很好,但是applet没有显示任何文本字段,并且没有调用genRandomNumbers()方法。因此,提示用户乘以两个数字的问题不会显示在小程序上。

在我的代码中,genRandomNumber()正在调用init()。这会导致问题吗?我尝试用paint()来做这件事。这导致了更大的问题 - applet上没有显示任何文本字段。所以我将调用移至genRandomNumber()init()

小程序窗口在状态区域显示"Start: applet window not initialized"

你能指出我正确的方向吗?非常感谢任何帮助!

这是我的代码:

 //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!!");
      }  

    }
}   

1 个答案:

答案 0 :(得分:1)

基本上,你有一系列NullPointerException个。这就是为什么applet不是开始编程的好地方的原因之一,因为除非你启用了Java控制台,否则你不会从程序中获得任何可能有用的输出...

任何方式......

您声明以下实例变量...

JTextField answerTextField, commentTextField, questionTextField;

然后在你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
}

您将questionTextFieldanswerTextFieldcommentTextField声明为本地变量。这通常称为阴影。基本上,当你认为实例变量已被初始化时,他们没有,因为你已经使用了局部变量......

相反,如果你做的更像......

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
}

您现在应该发现您的实例变量已经初始化,并且现在可以按照预期从您班级的其他部分进行访问...