Java 如何添加显示“升级”的按钮?
我尝试了很多方法在下面的代码中添加一个按钮,该代码运行一个太空入侵者游戏,但没有一个成功。谁能看看有没有解决办法 先谢谢你Java 如何添加显示“升级”的按钮?,java,button,jframe,jbutton,containers,Java,Button,Jframe,Jbutton,Containers,我尝试了很多方法在下面的代码中添加一个按钮,该代码运行一个太空入侵者游戏,但没有一个成功。谁能看看有没有解决办法 先谢谢你 import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Image; import java.awt.Toolkit; import j
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends Canvas {
/** Image variables to be drawn */
Image imgLevel;
Image imgAliens;
Image img;
JFrame container;
private BufferStrategy strategy;
private boolean gameRunning = true;
private ArrayList entities = new ArrayList();
private ArrayList removeList = new ArrayList();
private Entity ship;
private double moveSpeed = 500;
private long lastFire = 0;
private long firingInterval;
private long ExtraFiringInterval = 100;
private long bombFiringInterval = 500;
private int alienCount;
private String message = "";
private boolean waitingForKeyPress = true;
private boolean leftPressed = false;
private boolean rightPressed = false;
private boolean upPressed = false;
private boolean downPressed = false;
private boolean firePressed = false;
private boolean fireExtraPressed = false;
private boolean bombFired = false;
private boolean logicRequiredThisLoop = false;
static int level = 8;
private int Armour;
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
/**
* Construct our game and set it running.
*/
public Game() {
img = Toolkit.getDefaultToolkit().createImage("Bg.gif");
container = new JFrame("Space Invaders Mod");
container.setSize(xSize,ySize);
container.show();
JPanel panel = (JPanel) container.getContentPane();
panel.setSize(xSize, ySize);
panel.setLayout(null);
container.setSize(xSize, ySize);
setBounds(0,0,xSize,ySize);
panel.add(this);
setIgnoreRepaint(true);
container.pack();
container.setResizable(true);
container.setVisible(true);
container.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
addKeyListener(new KeyInputHandler());
requestFocus();
createBufferStrategy(2);
strategy = getBufferStrategy();
initEntities();
}
/**
* Start a fresh game, this should clear out any old data and
* create a new set.
*/
private void startGame() {
entities.clear();
initEntities();
leftPressed = false;
rightPressed = false;
upPressed = false;
downPressed = false;
firePressed = false;
fireExtraPressed = false;
bombFired = false;
firingInterval = 100;
}
/**
* Initialise the starting state of the entities (ship and aliens). Each
* entitiy will be added to the overall list of entities in the game.
*/
private void initEntities() {
ship = new ShipEntity(this,"sprites/ship.png",370,ySize - 130);
entities.add(ship);
alienCount = 0;
if (level == 1) {
Armour = 10;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
entities.add(alien3);
alienCount++;
}
}
}
if (level == 2) {
Armour = 20;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
}
}
}
if (level == 3) {
Armour = 30;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
}
}
}
if (level == 4) {
Armour = 40;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
}
}
}
if (level == 5) {
Armour = 50;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
}
}
}
if (level == 6) {
Armour = 60;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
}
}
}
if (level == 7) {
Armour = 70;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
entities.add(alien9);
alienCount++;
}
}
}
if (level == 8) {
Armour = 80;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
entities.add(alien9);
alienCount++;
entities.add(alien10);
alienCount++;
}
}
}
if (level == 9) {
Armour = 90;
ExtraFiringInterval --;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
entities.add(alien9);
alienCount++;
entities.add(alien10);
alienCount++;
entities.add(alien11);
alienCount++;
}
}
}
if (level == 10){
Armour = 100;
ExtraFiringInterval --;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
Entity alien12 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(90)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
entities.add(alien9);
alienCount++;
entities.add(alien10);
alienCount++;
entities.add(alien11);
alienCount++;
entities.add(alien12);
alienCount++;
}
}
}
if (level == 11){
Armour = 110;
for (int row=0;row<1;row++) {
for (int x=0;x<25;x++) {
Entity alien3 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(00)+row*30);
Entity alien4 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(10)+row*30);
Entity alien5 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(20)+row*30);
Entity alien6 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(30)+row*30);
Entity alien7 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(40)+row*30);
Entity alien8 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(50)+row*30);
Entity alien9 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(60)+row*30);
Entity alien10 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(70)+row*30);
Entity alien11 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(80)+row*30);
Entity alien12 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(90)+row*30);
Entity alien13 = new AlienEntity(this,"sprites/alien.png",100+(x*50),(100)+row*30);
entities.add(alien3);
alienCount++;
entities.add(alien4);
alienCount++;
entities.add(alien5);
alienCount++;
entities.add(alien6);
alienCount++;
entities.add(alien7);
alienCount++;
entities.add(alien8);
alienCount++;
entities.add(alien9);
alienCount++;
entities.add(alien10);
alienCount++;
entities.add(alien11);
alienCount++;
entities.add(alien12);
alienCount++;
entities.add(alien13);
alienCount++;
}
}
}
}
public void updateLogic() {
logicRequiredThisLoop = true;
}
public void removeEntity(Entity entity) {
removeList.add(entity);
}
public void notifyDeath() {
message = "Oh no! They got you, try again?";
waitingForKeyPress = true;
level = 0;
}
public void notifyWin() {
message = "Well done! You beat level "+level;
waitingForKeyPress = true;
if (level == 10){
message = "Well done! You actually beat level 10. Good luck!!";
}
}
public void notifyAlienKilled() {
// reduce the alient count, if there are none left, the player has won!
alienCount = alienCount-1;
ShotEntity.death = false;
ShotEntityExtra.death = false;
if (alienCount == 0) {
notifyWin();
}
// if there are still some aliens left then they all need to get faster, so
// speed up all the existing aliens
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
if (entity instanceof AlienEntity) {
// speed up by 2%
entity.setHorizontalMovement(entity.getHorizontalMovement() * 1.02);
}
}
}
public void tryToFire() {
if (System.currentTimeMillis() - lastFire < firingInterval) {
return;
}
if (level > 6) {
lastFire = System.currentTimeMillis();
ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+70,ship.getY()-30);
entities.add(shot2);
lastFire = System.currentTimeMillis();
ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-30,ship.getY()-30);
entities.add(shot3);
}
if (level > 7){
lastFire = System.currentTimeMillis();
ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+90,ship.getY()-30);
entities.add(shot2);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-50,ship.getY()-30);
entities.add(shot3);
}
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntity shot = new ShotEntity(this,"sprites/shot.png",ship.getX()+20,ship.getY()-30);
entities.add(shot);
if (level > 3){
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntity shot2 = new ShotEntity(this,"sprites/shot.png",ship.getX()+50,ship.getY()-30);
entities.add(shot2);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntity shot3 = new ShotEntity(this,"sprites/shot.png",ship.getX()-10,ship.getY()-30);
entities.add(shot3);
}
}
public void tryToFireExtra() {
// check that we have waiting long enough to fire
if (System.currentTimeMillis() - lastFire < ExtraFiringInterval) {
return;
}
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX(),ship.getY()-30);
entities.add(shotExtra);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra2 = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX()+50,ship.getY()-30);
entities.add(shotExtra2);
if (level == 9){
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra3 = new ShotEntityExtra(this,"sprites/shotExtra.png",ship.getX()+25,ship.getY()-30);
entities.add(shotExtra3);
}
if (level == 10){
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra3 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+12.5),ship.getY()-30);
entities.add(shotExtra3);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra4 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+37.5),ship.getY()-30);
entities.add(shotExtra4);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
ShotEntityExtra shotExtra5 = new ShotEntityExtra(this,"sprites/shotExtra.png", (int) (ship.getX()+25),ship.getY()-30);
entities.add(shotExtra5);
}
}
public void tryToFireBomb() {
// check that we have waiting long enough to fire
if (System.currentTimeMillis() - lastFire < bombFiringInterval) {
return;
}
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
Bomb bomb = new Bomb(this,"sprites/bomb.png",ship.getX(),ship.getY()-30);
entities.add(bomb);
// if we waited long enough, create the shot entity, and record the time.
lastFire = System.currentTimeMillis();
Bomb bomb2 = new Bomb(this,"sprites/bomb.png",ship.getX()+10,ship.getY()-30);
entities.add(bomb2);
}
public void gameLoop() {
long lastLoopTime = System.currentTimeMillis();
// keep looping round til the game ends
while (gameRunning) {
// work out how long its been since the last update, this
// will be used to calculate how far the entities should
// move this loop
long delta = System.currentTimeMillis() - lastLoopTime;
lastLoopTime = System.currentTimeMillis();
// Get hold of a graphics context for the accelerated
// surface and blank it out
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
//g.drawImage(img, 0, 0,xSize, ySize, null);
g.drawRect(0, 0, xSize, ySize);
g.fillRect(0, 0, xSize, ySize);
g.setColor(Color.BLACK);
// cycle round asking each entity to move itself
if (!waitingForKeyPress) {
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.move(delta);
}
}
// cycle round drawing all the entities we have in the game
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.draw(g);
}
// brute force collisions, compare every entity against
// every other entity. If any of them collide notify
// both entities that the collision has occured
for (int p=0;p<entities.size();p++) {
for (int s=p+1;s<entities.size();s++) {
Entity me = (Entity) entities.get(p);
Entity him = (Entity) entities.get(s);
if (me.collidesWith(him)) {
me.collidedWith(him);
him.collidedWith(me);
}
}
}
// remove any entity that has been marked for clear up
entities.removeAll(removeList);
removeList.clear();
// if a game event has indicated that game logic should
// be resolved, cycle round every entity requesting that
// their personal logic should be considered.
if (logicRequiredThisLoop) {
for (int i=0;i<entities.size();i++) {
Entity entity = (Entity) entities.get(i);
entity.doLogic();
}
logicRequiredThisLoop = false;
}
// if we're waiting for an "any key" press then draw the
// current message
if (waitingForKeyPress) {
g.setColor(Color.white);
g.drawString(message,(800-g.getFontMetrics().stringWidth(message))/2,250);
g.drawString("Press any key to start game",(800-g.getFontMetrics().stringWidth("Press any key to start game"))/2,300);
}
//score
g.setColor(Color.WHITE);
g.drawString("" + alienCount,10,10);
g.drawString(" Aliens left", 25, 10);
//level
g.setColor(Color.WHITE);
g.drawString("" + level,50,20);
g.drawString(" Level:", 10, 20);
// finally, we've completed drawing so clear up the graphics
// and flip the buffer over
g.dispose();
导入java.awt.BorderLayout;
导入java.awt.Canvas;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics2D;
导入java.awt.Image;
导入java.awt.Toolkit;
导入java.awt.event.KeyAdapter;
导入java.awt.event.KeyEvent;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入java.awt.image.BufferStrategy;
导入java.util.ArrayList;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
公共类游戏扩展画布{
/**要绘制的图像变量*/
图像imgLevel;
图像增强;
图像img;
JFrame容器;
私人缓冲战略;
私有布尔gameRunning=true;
私有ArrayList实体=新ArrayList();
private ArrayList removeList=new ArrayList();
私人实体船舶;
私人双移动速度=500;
私人长lastFire=0;
私人长飞行间隔;
专用长引燃间隔=100;
私人长轰炸间隔=500;
私人帐户;
私有字符串消息=”;
私有布尔waitingForKeyPress=true;
私有布尔leftPressed=false;
private boolean rightPressed=false;
private boolean upPressed=false;
private boolean downPressed=false;
private boolean firpressed=false;
私有布尔值fireExtraPressed=false;
私有布尔值=false;
私有布尔逻辑RequiredThisloop=false;
静态整数级=8;
私人装甲;
Toolkit tk=Toolkit.getDefaultToolkit();
intxsize=((int)tk.getScreenSize().getWidth());
int-ySize=((int)tk.getScreenSize().getHeight());
/**
*构建我们的游戏并使其运行。
*/
公共游戏(){
img=Toolkit.getDefaultToolkit().createImage(“Bg.gif”);
容器=新JFrame(“太空入侵者模型”);
container.setSize(xSize,ySize);
container.show();
JPanel面板=(JPanel)container.getContentPane();
面板设置尺寸(xSize、ySize);
panel.setLayout(空);
container.setSize(xSize,ySize);
立根(0,0,xSize,ySize);
小组。添加(本);
setIgnoreRepaint(真);
container.pack();
容器。可设置大小(true);
container.setVisible(true);
container.addWindowListener(新的WindowAdapter(){
@凌驾
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
addKeyListener(新的KeyInputHandler());
requestFocus();
创新战略(2);
strategy=getBufferStrategy();
初始化实体();
}
/**
*开始一个新的游戏,这应该清除所有旧数据和
*创建一个新集合。
*/
私有void startGame(){
实体。清除();
初始化实体();
leftPressed=false;
右压=假;
upPressed=false;
按下=假;
壁炉=假;
fireExtraPressed=false;
bombFired=false;
飞行间隔=100;
}
/**
*初始化实体(船舶和外国人)的起始状态。每个
*实体将被添加到游戏中实体的总列表中。
*/
私有实体(){
ship=新的ShipEntity(这个“sprites/ship.png”,370,ySize-130);
实体。添加(船舶);
alienCount=0;
如果(级别==1){
装甲=10;
对于(int row=0;row我建议您在继续之前阅读一些
下面是一个简单的通用示例,介绍如何将JButton
添加到JFrame
:
JFrame frame = new JFrame("FrameDemo");
JButton button = new JButton("upgrade"); //The JButton name.
frame.add(button); //Add the button to the JFrame.
frame.setSize(200, 200);
frame.setVisible(true);
如果你了解这5行代码是如何工作的,你可以将它应用到你正在做的任何事情上。plsI知道如何使用它们,但它仍然不适用于此。还有其他建议吗?@user2149407 no1将阅读500行代码来回答你的问题,你需要更具体地说明它是如何“不工作”的请切换最后两个代码lines@mKorbel对不起,修好了