来自init()的方法调用-Java
过去一周左右,我一直在自学Java。我对编程一无所知。我的代码编译得很好,但小程序不显示任何文本字段,并且没有调用来自init()的方法调用-Java,java,Java,过去一周左右,我一直在自学Java。我对编程一无所知。我的代码编译得很好,但小程序不显示任何文本字段,并且没有调用genRandomNumbers()方法。因此,提示用户将两个数字相乘的问题不会显示在小程序上 在我的代码中,正在从init()调用genRandomNumber()。这会引起问题吗?我试着用paint()来代替。这导致了更大的问题——小程序上没有显示任何文本字段。因此,我将调用移动到genRandomNumber()返回到init() 小程序窗口在状态区域显示“启动:小程序窗口未初
genRandomNumbers()
方法。因此,提示用户将两个数字相乘的问题不会显示在小程序上
在我的代码中,正在从init()
调用genRandomNumber()。这会引起问题吗?我试着用paint()来代替。这导致了更大的问题——小程序上没有显示任何文本字段。因此,我将调用移动到genRandomNumber()
返回到init()
小程序窗口在状态区域显示“启动:小程序窗口未初始化”
你能给我指一下正确的方向吗?非常感谢您的帮助
这是我的密码:
//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!!");
}
}
}
基本上,您有一系列的
NullPointerException
s。这就是为什么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
}
将questionTextField
、answerTextField
和commentTextField
声明为局部变量。这通常被称为阴影。基本上,当您认为实例变量已经初始化时,它们还没有初始化,因为您使用了局部变量
相反,如果你做一些更像
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
}
您现在应该会发现您的实例变量已经初始化,并且可以从类的其他部分访问…非常感谢!这很有魅力。现在我已经进入学习编程的第二周了,这开始变得更有意义了:)