当我通过CardLayout切换到另一个JPanel时,我遇到了卡住精灵的问题。
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame implements Runnable {
private viewA v1;
private viewB v2;
private viewC v3;
private viewD v4;
private Player player;
private CardLayout c1;
private JPanel contPanel;
// ############## VIEW A BUTTONLISTENER
public class ViewAButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "1");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEW B BUTTON LISTENER
public class ViewBButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "2");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEWC BUTTON ACTIONEVENT
public class ViewCButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "3");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
// ############## VIEW D BUTTON ACTION EVENT
public class ViewDButtonList implements ActionListener{
public void actionPerformed(ActionEvent ev){
try{
c1.show(contPanel, "4");
} catch (Exception ex){
ex.printStackTrace();
}
}
}
public Game() {
player = new Player();
// load player settings from server
// ..
// contPanel = new JPanel();
//load views v1 = new ViewA(Player); v2=new ViewB(Player); v3 = new ViewC(Player); v4 =new ViewD(Player);
c1 = new CardLayout();
contPanel.setLayout(c1);
contPanel.add(v1,"1");
contPanel.add(v2,"2");
contPanel.add(v3"3");
contPanel.add(v4,"4");
c1.show(contPanel, "2");
currPos =2;
this.add(contPanel);
setSize(652, 480);
setLocationRelativeTo(null);
setTitle("GAME");
setResizable(false);
setVisible(true);//go to end of view B (x=0), change to View A, close view B
// Create 4 of each button to place in each seperate view (to swtich back and forth)
// ................
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Game();
}
});
}
@Override public void run() { // TODO Auto-generated method stub
} }
现在......从屏幕切换的角度来看,一切都有效..但是我的播放器精灵在切换后卡在屏幕上,但在初始加载时我可以用箭头键移动精灵。我不确定是否在新的ViewA(播放器)上传播播放器可能是罪魁祸首..但我感觉它是......无法弄清楚我做错了什么..
这是播放器:
import java.awt.Image; import java.awt.Rectangle;进口 java.awt.event.KeyEvent中;
import java.util.ArrayList;
导入javax.swing.ImageIcon;
public class Player {
private String playersprite = "playersprite.png"; private int dx; private int dy; private int x; private int y; private int width; private int height; private boolean isFired; private boolean visible; private Image image; private ArrayList missiles; public Player() { ImageIcon ii = new ImageIcon(this.getClass().getResource(playersprite)); image = ii.getImage(); width = image.getWidth(null); height = image.getHeight(null); missiles = new ArrayList(); visible = true; isFired=false; //default spawn location x = 600; y = 400; } public void move() { x += dx; y += dy; if(x<=0){x=0;} if(x>=640-20){x=640-20;} if(y<=0){y=0;} if(y>=400){y=400;} } public int getX() { return x; } public int getY() { return y; } public void setX(int x){ this.x=x; } public void setY(int y){ this.y=y; } public Image getImage() { return image; } public ArrayList getMissiles() { return missiles; } public void setVisible(boolean visible) { this.visible = visible; } public boolean isVisible() { return visible; } public Rectangle getBounds() { return new Rectangle(x, y, 32, 32); } public void keyPressed(KeyEvent e) { System.out.println("X: "+x+",Y: "+y); int key = e.getKeyCode(); if (key == KeyEvent.VK_SPACE) { if(isFired==false){ isFired=true; fire(); } else return; } if (key == KeyEvent.VK_LEFT) { dx = -2; } if (key == KeyEvent.VK_RIGHT) { dx = 2; } if (key == KeyEvent.VK_UP) { dy = -2; } if (key == KeyEvent.VK_DOWN) { dy = 2; } } public void fire() { System.out.println("Player used weapon"); // missiles.add(new Missile(x, y)); } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = 0; } if (key == KeyEvent.VK_RIGHT) { dx = 0; } if (key == KeyEvent.VK_UP) { dy = 0; } if (key == KeyEvent.VK_DOWN) { dy = 0; } if (key == KeyEvent.VK_SPACE) { isFired=false; } } }
答案 0 :(得分:1)
KeyListener
要求他们注册的组件是可聚焦的并且具有焦点,这意味着当您点击像JButton
焦点这样的内容移动到按钮和具有KeyListener
的组件时不再具有键盘焦点,因此将不再接收关键事件。
相反,建议您使用Key Bindings,因为此API有能力克服这些限制