문제

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