سؤال

Currently this is my code

package com.raggaer.frame;

import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;

public class Frame {

    private JFrame frame;

    public Frame() {

        this.frame = new JFrame("Java Snake");
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        this.frame.add(new Panel());

        Paint game = new Paint();

        this.frame.setResizable(false);
        this.frame.pack();
        this.frame.setVisible(true);

    }
}

And this is my paint class

package com.raggaer.frame;

public class Paint implements Runnable {

    private Thread thread;

    public Paint() {

        thread = new Thread(this);
        thread.start();
    }

    public void run() {

        System.out.println("aaa");

    }

}

But the System.out.println("aaa"); is just executing one time instead of forever.. what Im doing wrong?

هل كانت مفيدة؟

المحلول

You'll have to loop in the run() method if you want it to run forever. Otherwise the thread finishes execution and exits.

نصائح أخرى

Try This :

           public void run() 
           {
              while(true)
              System.out.println("aaa");
            }

When the run() method returns/ends the thread will be terminated. If you want the thread to continue 'forever' you can use while(true) or recall the run() method inside the run() method.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top