gpt4 book ai didi

java - 当我尝试运行我的程序时 View 打不开?

转载 作者:行者123 更新时间:2023-12-02 06:07:51 24 4
gpt4 key购买 nike

我正在遵循这位先生 ( 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 View 出现。这可能是我的代码的一个怪癖,还是我的 PC/IDE 的错误?我正在使用 Eclipse 和 JavaSE 1.7。忽略一些评论的计时器内容 - 我只是尝试与所示不同的方法。

更新:感谢您对设置可见性的指导。然而,现在框架已经弹出,我看不到 BattleGrid 类中设置的颜色。有什么想法吗?

非常感谢您的帮助!

最佳答案

您需要将visible属性设置为true,如JFrame实例上的setVisible(true),因为它默认设置为false

关于java - 当我尝试运行我的程序时 View 打不开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com