当我尝试运行我的程序时,视图无法打开?

时间:2014-03-02 15:30:52

标签: java eclipse

我正在关注这位绅士(https://www.youtube.com/watch?v=_NCw5S7PUXw)在Java中试图制作塔防游戏的教程。我已经完成了上面的视频,到目前为止我的代码如下。我把它分成了三个类,都在同一个包中:

GameView.java

package jTowerDefensev1;

import javax.swing.JFrame;

public class GameView extends JFrame{

public static void main (String [] args) {
    new GameView();
}

public GameView() {
    JFrame view = new JFrame();

    view.setSize(800, 600);
    view.setTitle("jTowerDefense");
    view.setDefaultCloseOperation(EXIT_ON_CLOSE);
    view.setResizable(false);
    view.setLocationRelativeTo(null);

    BattleGrid grid = new BattleGrid(this);
    this.add(grid);

}

}

BattleGrid.java

package jTowerDefensev1;

import java.awt.Color;
import java.awt.Graphics;
//import java.awt.event.ActionEvent;
//import java.awt.event.ActionListener;


import javax.swing.JPanel;
//import javax.swing.Timer;

public class BattleGrid extends JPanel implements Runnable {

 GameView view;

 Thread thread = new Thread(this);

 public boolean running = false; //Is the game running?

 private int fps = 0; //The Frames Per Second (FPS) of the view

 public int scene = 0;

//ActionListener update = new ActionListener() { 
    //@Override public void actionPerformed(ActionEvent ae) { repaint(); }
//};

//Timer timer = new Timer(1, update); 

public BattleGrid (GameView view) {

    this.view = view;

    this.view.addKeyListener(new KeyEventHandler(this));

    //timer.start();
    thread.start();

}

public void paintComponent (Graphics g) {
    g.clearRect(0, 0, this.view.getWidth() ,this.view.getHeight()); //Make sure painting can proceed as prev. layer is done painting
    g.drawString(fps + "", 10, 10);

    if (scene == 0) {
        g.setColor(Color.BLUE); //Pre-game
    }

    else if (scene ==1){
        g.setColor(Color.GREEN); //During game
    }

    else{
        g.setColor(Color.WHITE); //Anywhere else
    }

    g.fillRect(0, 0, this.view.getWidth(), this.view.getHeight());

}

public void run() {

    //System.out.println("Frame Made!");

    long lastView = System.currentTimeMillis();
    int frames = 0;

    running = true;
    scene = 0;

    while (running) {

        repaint();

        frames++;

        if(System.currentTimeMillis() - 1000 >= lastView) { //At 1000 milliseconds (1 second), set the fps
            fps = frames;
            frames = 0;
            lastView = System.currentTimeMillis();
        }

        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
          }

    }

    System.exit(0);

}


public class KeyPressed {

    public void KeyESC(){ //Close game on ESC key press
        running = false;

    }

    public void KeySPACE() {
        scene = 1;

    }
}


}

KeyEventHandler.java

package jTowerDefensev1;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyEventHandler implements KeyListener {

private BattleGrid grid;
private BattleGrid.KeyPressed keyPressed;

public KeyEventHandler(BattleGrid grid) {
    this.grid = grid;
    this.keyPressed = this.grid.new KeyPressed();
}

public void keyTyped(KeyEvent e) {


}

public void keyPressed(KeyEvent e) {
    int keyCode = e.getKeyCode();

    if(keyCode == 27){ //ESC key to close game
        this.keyPressed.KeyESC();
    }

    if(keyCode == 32){ //Space to begin main game
        this.keyPressed.KeySPACE();
    }

}

public void keyReleased(KeyEvent e) {


}

}

现在我的问题:我一直在尝试将GameView作为Java应用程序运行,以查看我的工作结果。但它似乎挂在那里直到我必须杀死进程 - 没有GUI视图出现。它可能是我的代码的怪癖,还是我的PC / IDE的错误?我正在使用Eclipse和JavaSE 1.7。忽略一些评论计时器的东西 - 我只是尝试了不同于图示的方式。

更新:感谢设置可见性的指针。但是,现在框架已弹出,我无法看到我的BattleGrid类中设置的颜色。那里有什么想法吗?

非常感谢所有人的帮助!

1 个答案:

答案 0 :(得分:2)

您需要将{property}的setVisible(true)上的visible属性设置为true,因为默认设置为JFrame