Java 视图不';当我尝试运行我的程序时,是否无法打开?
我正在跟随这位先生()的教程,他在Java中四处游荡,试图制作一个塔防游戏。我已经完成了上面的视频,到目前为止我的代码如下。我将其分为三类,都在同一个包中: GameView.javaJava 视图不';当我尝试运行我的程序时,是否无法打开?,java,eclipse,Java,Eclipse,我正在跟随这位先生()的教程,他在Java中四处游荡,试图制作一个塔防游戏。我已经完成了上面的视频,到目前为止我的代码如下。我将其分为三类,都在同一个包中: GameView.java package jTowerDefensev1; import javax.swing.JFrame; public class GameView extends JFrame{ public static void main (String [] args) { new GameView(); }
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);
}
}
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;
}
}
}
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) {
}
}
BattleGrid.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);
}
}
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;
}
}
}
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) {
}
}
KeyEventHandler.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);
}
}
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;
}
}
}
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。忽略一些评论定时器的东西-我只是尝试不同的方式,而不是说明
更新:感谢您提供有关设置可见性的提示。然而,现在框架已经弹出,我看不到在我的战网类设置的颜色。有什么想法吗
非常感谢您的帮助 您需要将visible属性设置为true,就像
JFrame
实例上的setVisible(true)
一样,因为默认情况下它设置为false
。D'oh!非常感谢!如果你不介意的话,我现在有另一个问题,我可以看到框架了。我的颜色没有显示出来——它仍然是灰色的。有什么想法吗?没关系,我自己发现的!基本上,对于任何想知道的人来说,我将GameView设置为一个特定的视图-使用this.ANYMETHOD使其在命令时更新!