简体   繁体   English

在小程序中显示随机否

[英]displaying random no in the applet

i want to display random no in the applet as long as the applet is running but its not displaying.只要小程序正在运行但它不显示,我想在小程序中显示随机数。 kindly guide whats wring with the code请指导使用代码的问题

package javaapplication1;
 import java.util.*;
import java.applet.Applet;
import java.awt.Graphics;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class set extends Applet implements Runnable {
    private DateFormat formatter = null;
    private Thread t = null;

    public void init() {

        int n = (int)(10.0 * Math.random()) + 1;
       System.out.print(n);
        t = new Thread(this);
    }

    public void start() {
        t.start();
    }

    public void stop() {
        t = null;
    }

    public void paint(Graphics g) {
        Date now = Calendar.getInstance().getTime();
        //
        // Show the current time on the browser status bar
        //
        this.showStatus(formatter.format(now));
    }

    public void run() {
        int delay = 1000;
        try {
            while (t == Thread.currentThread()) {
                //
                // Repaint the applet every on second
                //
                repaint();
                Thread.sleep(delay);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

and its floating with the following exception及其浮动,但有以下例外

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:248)
        at sun.awt.RepaintArea.paint(RepaintArea.java:224)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
4Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at java.awt.Container.update(Container.java:1801)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at java.awt.Container.update(Container.java:1801)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at java.awt.Container.update(Container.java:1801)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at java.awt.Container.update(Container.java:1801)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
        at javaapplication1.set.paint(set.java:39)
        at java.awt.Container.update(Container.java:1801)
        at sun.awt.RepaintArea.updateComponent(RepaintArea.java:239)
        at sun.awt.RepaintArea.paint(RepaintArea.java:216)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:310)
        at java.awt.Component.dispatchEventImpl(Component.java:4706)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4460)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException

This should not have been difficult.这应该不难。 A NullPointerException occurs when you try to do an operation on a null .当您尝试对null执行操作时,会发生NullPointerException

You have a NullPointerException in your own code (as opposed to code in a library).自己的代码中有NullPointerException (与库中的代码相反)。 All you have to do is to go to that line and see what all possibly can be null .您所要做的就是将 go 转到该行,然后查看所有可能是null

In the line在行

this.showStatus(formatter.format(now));

formatter is (most likely) null . formatter是(很可能) null This is the reason for the NullPointerException .这就是NullPointerException的原因。

I think stackoverflow is making people infinitely lazy.我认为 stackoverflow 让人们变得无限懒惰。

How do you want to display it, and which bit of your code do you think will be achieving this?你想如何显示它,你认为你的哪一点代码会实现这一点?

At the moment I can see you create a random number and then call System.out.println with that number.目前我可以看到您创建了一个随机数,然后使用该数字调用System.out.println This will print the number out to the console , but it will not display it in your applet.这会将数字打印到控制台,但不会在您的小程序中显示。

You'll need to put some logic in the paint(Graphics g) method to display this number - for example, calling g.drawString() .您需要在paint(Graphics g)方法中添加一些逻辑来显示此数字 - 例如,调用g.drawString() However there are a multitude of ways that you could possibly output text, depending on how you want it to loko at what you have already, such that it's not plausible to give a single correct, or more specific, answer to this question.但是,您可以通过多种方式发送 output 文本,具体取决于您希望它如何看待您已经拥有的内容,因此对这个问题给出一个正确或更具体的答案是不合理的。

You should take another look at an applet tutorial that goes over how you control the output, and perhaps post back with more specific questions based on the outcome of that.您应该再看一个小程序教程,该教程介绍了您如何控制 output,并且可能会根据结果回复更具体的问题。

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

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