我是图形编程的新手,并且使用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的类。就像我说的,当我按下键盘上的键时,我想要做的就是让程序输出一些东西。如果我需要告诉你任何其他内容,请告诉我,我会发布。
答案 0 :(得分:3)
尝试将KeyListener
添加到您需要的组件,而不是整个JFrame
。并确保他们专注。
另外,您可能会发现How to Use Key Bindings非常有用,可以替代关键听众。