通过ActionListener使用getContentPane()后,KeyListener不执行任何操作

时间:2015-11-26 18:18:17

标签: java swing actionlistener keylistener

大家好我是新手,这是我在编码论坛的第一篇文章(对不起我的英文不好XD)我决定制作一个菜单来开始游戏,但当我通过使用JFrame setContentPane()我的菜单添加它class,keylistener无法做任何事情,虽然当我通过直接添加我的Tank类进行测试时,它运行正常,我该如何解决这个问题?谢谢:D

我的菜单类:

public class Menu extends JPanel {
private JButton jb1=new JButton("Start game"),
        jb2=new JButton("Exit");
private JFrame frame;
public Menu(JFrame x){
    this.frame=x;
    setLayout(new FlowLayout());
    add(jb1);
    add(jb2);
    jb1.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
           frame.setContentPane(new Tank(frame));
           frame.pack();
       }
    });

    jb2.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
           frame.dispose();
           System.exit(0);
       }
    });    
}
public Dimension getPreferredSize(){
    return new Dimension(1200,600);
}}

我的坦克课程:

public class Tank extends JPanel implements ActionListener,KeyListener{
private int xTank=0,   
            yTank=0,
            xTankMotion=0,
            yTankMotion=0;
private int xAlien=800,
            yAlien=0,
            xAlienMotion=0,
            yAlienMotion=0;
private Image tank=new ImageIcon("Tank.jpg").getImage(),
              alien= new ImageIcon("Alien.jpg").getImage();
private int tankWidth=tank.getWidth(this), 
            tankHeight=tank.getHeight(this);
private int fire=0,
            xRocket=0,
            yRocket=0,
            xRocketMotion=0,
            yRocketMotion=0;
private int alienWidth=alien.getWidth(this), 
            alienHeight=alien.getHeight(this);
private int key=1;
private Timer time=new Timer(1,this);
private int angle;
private int truot=0,trung=0;
private boolean banduoc=true;
private JFrame frame;
private void gameOver(JFrame jf){
    jf.setContentPane(new Victory());
}

public Tank(JFrame frame){ 
    this.frame=frame;
    time.start();
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    requestFocus();
    addKeyListener(this);
}
@Override
public void actionPerformed(ActionEvent e){

    if(xTank<=0) xTank=0;
    if(xTank>=400) xTank=400;
    if(yTank<=0) yTank=0;
    if(yTank>=600-tankHeight) yTank=600-tankHeight;

    if(truot==0){
    if(xAlien<=1200-alienWidth) {
        xAlienMotion=1; 
        yAlienMotion=0;}
    if(xAlien>=1200-alienWidth){ 
        xAlienMotion=0; 
        yAlienMotion=1;}
    if(yAlien>=600-alienHeight){ 
        xAlienMotion=-1; 
        yAlienMotion=0;}
    if(xAlien<=800&&yAlien>=0){ 
        xAlienMotion=0; 
        yAlienMotion=-1;}}
    else {
        if(trung==0&&((xTank<=xAlien+alienWidth&&xTank+tankWidth>=xAlien+alienWidth&&((yTank>=yAlien&&yTank<=yAlien+alienHeight)||(yTank+tankHeight>=yAlien&&yTank+tankHeight<=yAlien+alienHeight)))||
            (yTank<=yAlien+alienHeight&&yAlien+alienHeight<=yTank+tankHeight&&((xTank>=xAlien&&xTank<=xAlien+alienWidth)||(xTank+tankWidth>=xAlien&&xTank+tankWidth<=xAlien+alienWidth)))||
            (xTank+tankWidth>=xAlien&&xAlien>=xTank&&((yTank>=yAlien&&yTank<=yAlien+alienHeight)||(yTank+tankHeight>=yAlien&&yTank+tankHeight<=yAlien+alienHeight)))||
            (yTank+tankHeight<=yAlien&&yAlien>=yTank&&((xTank>=xAlien&&xTank<=xAlien+alienWidth)||(xTank+tankWidth>=xAlien&&xTank+tankWidth<=xAlien+alienWidth)))))
        {time.stop();  frame.setContentPane(new Defeated(frame)); frame.pack(); return;}
        else{
            if(yAlien>yTank){
                yAlienMotion=-1;
                if(xAlien>xTank) xAlienMotion=-1;
                else if(xAlien<xTank) xAlienMotion=1;
                else xAlienMotion=0;
            }
            else if(yAlien<yTank){
                yAlienMotion=1;
                if(xAlien>xTank) xAlienMotion=-1;
                else if(xAlien<xTank) xAlienMotion=1;
                else xAlienMotion=0;
            }
            else{
                yAlienMotion=0;
                if(xAlien>xTank) xAlienMotion=-1;
                else if(xAlien<xTank) xAlienMotion=1;
            }
        }
    }
    xTank+=xTankMotion;
    yTank+=yTankMotion;

    xRocket+=xRocketMotion;
    yRocket+=yRocketMotion;

    xAlien+=xAlienMotion;
    yAlien+=yAlienMotion;

    if((xRocket>=1200||xRocket<0||yRocket<0||yRocket>600)&&truot==0)  {truot=1; banduoc=true;}
    if(xRocket>=xAlien&&xRocket<=xAlien+alienWidth&&yRocket>=yAlien&&yRocket<=yAlien+alienHeight) {trung=1; frame.setContentPane(new Victory());  frame.pack();}
    repaint();
}
@Override
public Dimension getPreferredSize(){
    return new Dimension(1200,600);
}
@Override
protected void paintComponent(Graphics g){
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, this.getWidth(), this.getHeight());
    AffineTransform at= AffineTransform.getTranslateInstance(xTank,yTank);
    at.rotate(Math.toRadians(angle),tankWidth/2,tankHeight/2);
    Graphics2D g2d=(Graphics2D) g;
    g2d.drawImage(tank,at,null);
    g.drawImage(alien, xAlien, yAlien, this);
    if(fire==1) {
        g.setFont(new Font("Monospaced",Font.BOLD,20));
        g.setColor(Color.yellow);
        g.fillOval(xRocket,yRocket, 5, 5);
    }
    if(truot==1) {
        g.setColor(Color.red);
        g.drawString("Noooooo! :(", xTank, yTank-50);
    }
}
public static void main(String[] args) {
    JFrame frame=new JFrame("Tank");
    frame.setContentPane(new Menu(frame));
    frame.setContentPane(new Tank(frame));
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setResizable(false);
}

@Override
public void keyTyped(KeyEvent e) {

}

@Override
public void keyPressed(KeyEvent e) {
    int i=e.getKeyCode();
    if(i==KeyEvent.VK_UP) {
        key=2;
        xTankMotion=0;
        yTankMotion=-1; 
        angle=90;}
    else if(i==KeyEvent.VK_DOWN) {
        key= 4;  
        xTankMotion=0;
        yTankMotion=1; 
        angle=-90;}
    else if(i==KeyEvent.VK_RIGHT) {
        key=3;  
        xTankMotion=1; 
        yTankMotion=0;
        angle=180;}
    else if(i==KeyEvent.VK_LEFT) {
        key=1;   
        xTankMotion=-1;
        yTankMotion=0;
        angle=0;}
    if(i==KeyEvent.VK_SPACE&&banduoc) {
        fire=1; 
        if(key==1) {
            xRocket=xTank;
            yRocket=yTank+tankHeight/2;
            xRocketMotion=-2;
            yRocketMotion=0;
        }
        if(key==2) {
            xRocket=xTank+tankWidth/2;
            yRocket=yTank;
            yRocketMotion=-2;
            xRocketMotion=0;
        }
        if(key==3) {
            xRocket=xTank+tankWidth;
            yRocket=yTank+tankHeight/2;
            xRocketMotion=2;
            yRocketMotion=0;
        }
        if(key==4) {
            xRocket=xTank+tankWidth/2;
            yRocket=yTank+tankHeight;
            yRocketMotion=2;
            xRocketMotion=0;
        }
        banduoc=false;
    }
}

@Override
public void keyReleased(KeyEvent e) {
    xTankMotion=0;
    yTankMotion=0;
}}

1 个答案:

答案 0 :(得分:0)

这是我的答案,我使用KeyBindings而不是KeyListener(感谢MadProgrammer:D),我在Tank类的构造函数中编写了它

TreeMap