突围游戏:JButton问题

时间:2014-02-03 17:29:03

标签: java swing button timer jbutton

我是初学者程序员,我正在尝试制作一个简单的2D游戏,Breakout。我有游戏本身的逻辑,但是我无法使按钮与游戏一起工作。我在屏幕顶部添加了一个开始和暂停按钮,但每当我按下其中一个按钮时,屏幕底部的拨片就无法移动。

当我拿出按钮时,游戏运行得很好。

任何帮助将不胜感激!提前谢谢。

以下是我游戏的代码。我只包括Breakout课程,因为我遇到了麻烦。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;

public class Breakout extends JPanel implements KeyListener, ActionListener
{
boolean rightPressed;
boolean leftPressed;
boolean upPressed;
boolean downPressed;

boolean inGame;

int width = 1450;
int height = 900;
Timer tm = new Timer(5, this);
int numRows  =5;
int numCols = 8;
int numBricks = 40;

int mouseX;
int mouseY;

Paddle paddle = new Paddle(width/2, height-40);
Ball ball = new Ball();
//BrickArray brick = new BrickArray(numRows, numCols, width/numCols, (height-100)/numRows);
Brick[][] bricks = new Brick[8][5];

JButton start = new JButton("Start");
JButton pause = new JButton("Pause");
JButton reset = new JButton("Reset");
JButton exit = new JButton("Exit");
JButton help = new JButton("Help");

public Breakout()
{
    setDoubleBuffered(true);
    //tm.start();
    addKeyListener(this);
    setFocusable(true);

    add(start); 
    start.addActionListener(new StartAction());     
    add(pause);
    pause.addActionListener(new PauseAction());
    add(reset);
    add(exit);
    add(help);

    ball.setMaxX(width);
    ball.setMaxY(height);
    ball.setWidth(width);
    ball.setHeight(height);     
    ball.setX(width/2); //width/2
    ball.setY(height/2);    //height

    paddle.setMaxX(width-paddle.getWidth());

    int k=0;
    //row 1
    for(int a=0; a<bricks.length; a++)
    {
        bricks[a][0]=new Brick(120,40);
        bricks[a][0].setX(a*180);
        bricks[a][0].setY(50);
        //System.out.println(k + ": " + a*80);
    }

    //row 2
    for(int b=0; b<bricks.length; b++)
    {
        bricks[b][1]=new Brick(120,40);
        bricks[b][1].setX(b*180);
        bricks[b][1].setY(100);
    }

    //row 3
    for(int c=0; c<bricks.length; c++)
    {
        bricks[c][2]=new Brick(120,40);
        bricks[c][2].setX(c*180);
        bricks[c][2].setY(150);
    }

    //row 4
    for(int d=0; d<bricks.length; d++)
    {
        bricks[d][3]=new Brick(120,40);
        bricks[d][3].setX(d*180);
        bricks[d][3].setY(200);
    }

    for(int e=0; e<bricks.length; e++)
    {
        bricks[e][4]=new Brick(120,40);
        bricks[e][4].setX(e*180);
        bricks[e][4].setY(250);
    }

}

public void actionPerformed(ActionEvent e)
{
    tm.setRepeats(true);

    if(rightPressed)
    {
        paddle.setX(paddle.getX()+5);
        //System.out.println("Pressed");    
    }
    if(leftPressed)
        paddle.setX(paddle.getX()-5);
    /*if(upPressed)
        y--;
    if(downPressed)
        y++;*/  

    ball.checkCollision(paddle);        
    for(int i=0; i<bricks.length; i++)
    {
        for(int j=0; j<bricks[i].length; j++)
        {
            if(ball.checkCollision(bricks[i][j]))
            {
                bricks[i][j].destroy();
                //System.out.println(bricks[i][j].isDestroyed());
                numBricks--;
                ball.move();
                repaint();
            }
        }
    }       


    //ball.checkCollision(brick);
    ball.move();        
    repaint();
}

public void keyPressed(KeyEvent e)
{
    int c = e.getKeyCode();
    if(c == KeyEvent.VK_LEFT)
    {
        leftPressed=true;
    }
    /*if(c == KeyEvent.VK_UP)
    {
        upPressed=true;
    }*/
    if(c == KeyEvent.VK_RIGHT)
    {
        rightPressed=true;
        //System.out.println("Pressed");    
    }
    /*if(c == KeyEvent.VK_DOWN)
    {
        downPressed=true;
    }*/

}

public void keyTyped(KeyEvent e)
{
}

public void keyReleased(KeyEvent e)
{
    int c = e.getKeyCode();
    if(c == KeyEvent.VK_LEFT)
    {
        leftPressed=false;
    }
    if(c == KeyEvent.VK_UP)
    {
        upPressed=false;
    }
    if(c == KeyEvent.VK_RIGHT)
    {
        rightPressed=false;
    }
    if(c == KeyEvent.VK_DOWN)
    {
        downPressed=false;
    }
}   

public void paintComponent(Graphics g)
{
    super.paintComponent(g);


    for(int a=0; a<bricks.length; a++)
    {
        g.setColor(Color.RED);
        bricks[a][0].paintComponent(g);
    }
    for(int b=0; b<bricks.length; b++)
    {
        g.setColor(Color.ORANGE);
        bricks[b][1].paintComponent(g);
    }
    for(int c=0; c<bricks.length; c++)
    {
        g.setColor(Color.YELLOW);
        bricks[c][2].paintComponent(g);
    }
    for(int d=0; d<bricks.length; d++)
    {
        g.setColor(Color.GREEN);
        bricks[d][3].paintComponent(g);
    }   
    for(int e=0; e<bricks.length; e++)
    {
        g.setColor(Color.BLUE);
        bricks[e][4].paintComponent(g);
    }       
    ball.paintComponent(g);
    paddle.paintComponent(g);
    g.setColor(Color.BLACK);
    g.drawString("Bricks left: " + numBricks, 10, 10);

}

public static void main(String[] args)
{
    Breakout obj = new Breakout();
    JFrame jf = new JFrame();
    jf.setTitle("Breakout");
    jf.setSize(1450,900);
    jf.setVisible(true);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.add(obj);
}

class StartAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        tm.start();
        System.out.println("Start");
    }
}

class PauseAction implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        tm.stop();
        System.out.println("Pause");
    }
}
}

1 个答案:

答案 0 :(得分:0)

我没有实施其他类,所以我无法确认这是否有效。

start.setFocusable(false);
pause.setFocusable(false);

可选地

start.addKeyListener(this);
pause.addKeyListener(this);