简体   繁体   中英

Some 2D Java Code Trouble

I'm trying to start a bit of 2d code and I have this much so far. It should just make a window appear but when I run it (in debug) I get an error saying source not found? Anyone see what I'm missing?

    package game;

    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Dimension;

    import javax.swing.JFrame;

public class Game extends Canvas implements Runnable{


private static final long serialVersionUID = 1L;

public static final int WIDTH = 160;
public static final int HEIGHT= WIDTH/12*9;
public static final int SCALE=3;
public static final String NAME = "My Game";

private JFrame frame;
public boolean running=false;

public Game()
{
    setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
    setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));

    frame = new JFrame(NAME);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());

    frame.add(this,BorderLayout.CENTER);
    frame.pack();

    frame.setResizable(false);
    frame.setLocation(null);
    frame.setVisible(true);
}

public synchronized void start() 
    {
    running = true;
    new Thread(this).start();
    }

public synchronized void stop() 
    {
    running =false;
    }

public void run() 
    {
    while(running)
    {
        System.out.println("Hello!");
    }

    }

public static void main(String[]args)
{
    new Game().start();

}


 }

I was working from a few tutorials to get this and I went through the code myself a few times but I cant spot whats wrong. :(

The problem is this line:

frame.setLocation(null);

The parameter provided to setLocation must not be null. If you do not need to set the location of the frame, don't call the method. By default, the frame is placed in the upper-left corner of the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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