我是图形编程的新手,使用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
添加到所需的组件中,而不是整个JFrame
。并确保他们专注。
另外,您可能会发现How to Use Key Bindings有用,可以代替键侦听器。
关于java - KeyListener出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10387975/