有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

来自init()Java的方法调用

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

在我的代码中,genRandomNumber()正在从init()调用。这会引起问题吗?我试着用paint()来代替。这导致了更大的问题——小程序上没有显示任何文本字段。所以我把对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) 个答案

  1. # 1 楼答案

    基本上,你有一系列的NullPointerException。这就是为什么小程序不是一个开始编程的好地方的原因之一,因为除非你启用了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
    }
    

    您现在应该会发现您的实例变量已经初始化,现在可以从类的其他部分访问了