在尝试创建一个可以帮助小学生学习乘法的 Java 小程序时。使用 Math.random 方法或 Random 对象生成两个正的一位整数。运行小程序时遇到问题。我对java编程相当陌生,这可能是一个非常简单的修复。
请参阅下面我当前的代码。
import java.awt.*;
import java.awt.Graphics;
import java.lang.Object;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class NewJApplets extends JApplet implements ActionListener
{
public Graphics brush;
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
JLabel question = new JLabel("What is " + number1 + " times " + number2 + "?");
JLabel Result = new JLabel("");
JTextField answer = new JTextField(3);
JButton checkAnswer = new JButton("Check Answer");
Font font1 = new Font("Teen", Font.BOLD, 30);
Font font2 = new Font("Teen", Font.ITALIC, 36);
String right = "Very good!!!";
String wrong = "No. Please try again.";
Container con = getContentPane();
public void init()
{
setLayout(new FlowLayout());
con.setBackground(Color.YELLOW);
question.setLocation(20, 20);
question.setFont(font1);
Result.setLocation (0,100);
Result.setFont(font1);
con.add(question);
con.add(Result);
answer.setLocation(20, 40);
con.add(answer);
checkAnswer.setLocation(20, 60);
con.add(checkAnswer);
checkAnswer.addActionListener(new ActionListener()
{
public void paint()
{
brush.setFont(font2);
}
public void actionPerformed(ActionEvent e)
{
int ans = Integer.parseInt(answer.getText());
if(ans == number1 * number2)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(9) + 1;
int number2 = rand.nextInt(9) + 1;
brush.drawString(right, 20, 80);
Result.setText(right);
repaint();
validate();
}
else
{
answer.setText("");
Result.setText(wrong);
brush.drawString(wrong, 80, 20);
repaint();
validate();
}
}
}
);
}
@Override
public void actionPerformed(ActionEvent e)
{
answer.setText("");
Random rand = new Random();
int number1 = rand.nextInt(10);
int number2 = rand.nextInt(10);
}
}
小程序编译没有任何问题,但是在尝试运行小程序时,我收到以下错误消息。
“课程似乎不是小程序。 ”带有继续或取消的选项。快速按下继续后,我得到了该应用程序,该应用程序似乎正在运行。然而,我没有给我正确的结果,而是在 jGRASP 中看到如下输出:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at NewJApplets$1.actionPerformed(NewJApplets.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
对这个问题的任何帮助都会很棒。