我是圖形編程新手,在使用KeyListener向左或向右移動圖像時遇到一些困難。目前我的代碼甚至沒有註冊一個鍵被按下。如果有人能夠幫我解決這個問題,那麼我可以自己完成剩下的工作。使用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的類。就像我說的,我想要做的就是讓程序輸出一些東西,當我按下鍵盤上的一個鍵。如果我需要告訴你其他東西,讓我知道,我會發布它。
爲更好的幫助,用[SSCCE](http://sscce.org/)更快地編輯您的問題證明了您的問題與KeyListener – mKorbel
' this.setSize(806,628);'不要這樣做。相反,請爲自定義內容區域設置首選大小,然後使用佈局將其添加到具有所需其他任何組件的面板中,將面板添加/設置爲內容窗格,然後在框架上調用'pack()'。這將是顯示組件和框架裝飾所需的最小尺寸。 –