我不知道为什么KeyEvent不会更改为gif并移动我的精灵
试图更改我加载图像的方式,但由于某些原因,KeyEvent在JPanel中未显示任何更改。
试图更改我加载JPanel的方式,但由于某些原因,KeyEvent在JPanel中未显示任何更改。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
@SuppressWarnings("serial")
public class Level1 extends JPanel implements ActionListener {
private static final Color COLOR1 = Color.BLACK;
private static final Color COLOR2 = Color.LIGHT_GRAY;
private JPanel gamePanel;
private JButton quit;
private JLabel introLabel;
protected JFrame lvl1;
ImageIcon lvl, sprite;
private int x = 500, y=500;
public Level1() {
lvl1 = new JFrame("ROAMING");
lvl1.setSize(640, 480);
lvl = new ImageIcon("C:\\Users\\srs42\\Sprites\\LEVEL1.gif");
JLabel icon = new JLabel(lvl);
sprite = new ImageIcon("C:\\Users\\srs42\\Sprites\\Sprite.gif");
JLabel spriteLabel = new JLabel(sprite);
gamePanel = new JPanel();
spriteLabel.setLocation(x, y);
icon.setLayout(new BorderLayout());
icon.add(spriteLabel);
introLabel = new JLabel("//: ROAMING");
introLabel.setForeground(COLOR1);
gamePanel.add(introLabel);
quit = new JButton("Quit");
quit.addActionListener(this);
quit.setBackground(COLOR2);
quit.setOpaque(true);
gamePanel.add(quit);
gamePanel.add(icon);
lvl1.add(gamePanel);
lvl1.setVisible(true);
//player.play("\\Users\\srs42\\Songs\\Mixdown\\Song2.wav");
}
public void keyPressed(KeyEvent e)
{
int key = e.getKeyCode();
if (key == KeyEvent.VK_D) {
sprite = new ImageIcon("C:\\Users\\srs42\\Sprites\\walking_down.gif");
}
}
显示级别和精灵,但精灵不会改变