一旦通过 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
} }
现在..从屏幕之间切换的角度来看,一切正常..但我的播放器精灵在切换后卡在屏幕上,但在初始加载时,我可以用箭头键移动精灵。我不确定是否将 Player 传递给新的 ViewA(Player) 可能是罪魁祸首..但我有一种感觉,它是..无法弄清楚我做错了什么..
这是播放器:
导入 java.awt.Image;导入 java.awt.Rectangle;导入 java.awt.event.KeyEvent;
导入 java.util.ArrayList;
导入 javax.swing.ImageIcon;
公共类播放器{
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; } } }