5

Java 似乎找不到我的构造函数,我不知道出了什么问题。抛出 InterruptedException 有问题吗?任何帮助将不胜感激,谢谢!

    package gameloop;

    import javax.swing.*;

    public class GameLoop extends JFrame {
        private boolean isRunning;
        public int drawx = 0;
        public int drawy = 0;

        public void GameLoop() throws InterruptedException{   
            setSize(700, 700);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);

            while(isRunning){
                doGameUpdate();
                render();
                Thread.sleep(1);
                if (isRunning){
                    GameLoop();
                }
            } 
        }

        private void doGameUpdate() {
            GameUpdate GU = new GameUpdate();
        }

        private void render() {
            Draw dr = new Draw();
        }

       public static void main(String[] args) {
            GameLoop GL = new GameLoop();
        }
    }
4

4 回答 4

6

A constructor is named exactly like its class, and has no return type. If you provide a return type, even void, you create a method called GameLoop. What you are looking for is

public GameLoop()

rather than

public void GameLoop()
于 2013-09-11T00:31:13.957 回答
4

You need public GameLoop() constructors don't have return types

于 2013-09-11T00:31:25.707 回答
4

That's not a constructor - this is:

public GameLoop() throws InterruptedException

A constructor can not have a return type (void in your code), if you add one, Java will interpret it it as a normal method - even if it's called exactly like the class!

于 2013-09-11T00:31:54.363 回答
3

You constructor has a return type so it is treated as any other method

于 2013-09-11T00:32:22.743 回答