1

我是图形编程的新手,在使用 KeyListener 向左或向右移动和图像时遇到了一些困难。目前我的代码甚至没有注册一个键被按下。如果有人可以帮我注册它,那么我可以自己做剩下的事情。

这是框架代码:

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FrameMain extends JFrame {

    final JPanel pnlShow;
    PanelHome pnlHome = new PanelHome();
    PanelPlayerInfo pnlPlayerInfo = new PanelPlayerInfo();
    PanelPlay pnlPlay = new PanelPlay(pnlPlayerInfo);
    PanelInstruction pnlInstructions = new PanelInstruction();
    PanelStore pnlStore = new PanelStore();
    PanelHighscores pnlHighscores = new PanelHighscores();
    ControlActionListenter CAL = new ControlActionListenter();

    public FrameMain() {
        pnlShow = new JPanel(new CardLayout());
        pnlShow.add(pnlHome, "Home");
        pnlShow.add(pnlPlay, "Play");
        pnlShow.add(pnlInstructions, "Instructions");
        pnlShow.add(pnlStore, "Store");
        pnlShow.add(pnlHighscores, "Highscores");
        pnlShow.add(pnlPlayerInfo, "PlayerInfo");

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setTitle("TANKS");
        this.setVisible(true);
        this.setSize(806, 628);
        this.setResizable(false);
        this.add(pnlShow);
        this.addKeyListener(new Move());

        pnlHome.btnExit.addActionListener(CAL);
        pnlHome.btnExit.setActionCommand("Exit");
        pnlHome.btnPlay.addActionListener(CAL);
        pnlHome.btnPlay.setActionCommand("PlayerInfo");
        pnlHome.btnInst.addActionListener(CAL);
        pnlHome.btnInst.setActionCommand("Instructions");
        pnlHome.btnHigh.addActionListener(CAL);
        pnlHome.btnHigh.setActionCommand("Highscores");
        pnlInstructions.btnBack.addActionListener(CAL);
        pnlInstructions.btnBack.setActionCommand("Main");
        pnlPlay.pnlToolbar.btnHome.addActionListener(CAL);
        pnlPlay.pnlToolbar.btnHome.setActionCommand("Main");
        pnlHighscores.btnBack.addActionListener(CAL);
        pnlHighscores.btnBack.setActionCommand("Main");
        pnlPlayerInfo.btnPlay.addActionListener(CAL);
        pnlPlayerInfo.btnPlay.setActionCommand("Play");
        pnlPlayerInfo.btnBack.addActionListener(CAL);
        pnlPlayerInfo.btnBack.setActionCommand("Main");

    }

    class ControlActionListenter implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            CardLayout cl = (CardLayout) (pnlShow.getLayout());
            String cmd = e.getActionCommand();

            if (cmd.equals("Main")) {
                cl.show(pnlShow, "Home");
            } else if (cmd.equals("Exit")) {
                System.exit(0);
            } else if (cmd.equals("Play")) {
                pnlPlay.arpPlayer[0].populateName(pnlPlayerInfo.txtPlayer1.getText());
                pnlPlay.arpPlayer[1].populateName(pnlPlayerInfo.txtPlayer2.getText());
                pnlPlay.pnlPlayer.lblPlayer1.setText(pnlPlay.arpPlayer[0].sPlayer);
                pnlPlay.pnlPlayer.lblPlayer2.setText(pnlPlay.arpPlayer[1].sPlayer);
                cl.show(pnlShow, "Play");
            } else if (cmd.equals("PlayerInfo")) {
                cl.show(pnlShow, "PlayerInfo");
            } else if (cmd.equals("Instructions")) {
                cl.show(pnlShow, "Instructions");
            } else if (cmd.equals("Highscores")) {
                cl.show(pnlShow, "Highscores");
            }
        }
    }

    class Move implements KeyListener {

        public void keyPressed(KeyEvent e) {
            System.out.println("rp");
        }

        public void keyTyped(KeyEvent e) {
            System.out.println("rp");
        }

        public void keyReleased(KeyEvent e) {
            System.out.println("rp");
        }
    }
}

我在框架中添加了一个 keylistener 并创建了一个实现这个 keylistener 的类。就像我说的,我想做的就是让程序在我按下键盘上的一个键时输出一些东西。如果我需要向您展示其他任何内容,请告诉我,我会发布它。

4

1 回答 1

3

尝试添加KeyListener到您需要的组件,而不是整个JFrame. 并确保他们专注。

此外,您可能会发现如何使用键绑定很有用,可以替代键侦听器。

于 2012-04-30T17:44:53.033 回答