为什么草图像不在 JFrame 的顶部?为什么角色不是从位置 200、200 开始。我点击一个键后他才会移动。谁能向我解释为什么我会遇到这些问题?
框架:
package frame;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.*;
import input.Key;
import character.Character;
public class Frame {
public static JFrame frame = new JFrame();
public static JPanel panel = new JPanel();
public static JPanel minimap = new JPanel();
public static JLabel map = new JLabel();
private static int width = 400;
private static int height = 400;
public static void create() {
Frame f = new Frame();
f.loadResources();
f.init();
}
private void loadResources() {
Image image = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("map.png"));
ImageIcon mapImg = new ImageIcon(image);
map.setIcon(mapImg);
panel.setMaximumSize(new Dimension(width, height));
panel.setMinimumSize(new Dimension(width, height));
panel.setPreferredSize(new Dimension(width, height));
panel.add(map);
Character.createCharacter();
panel.setComponentZOrder(Character.character, 0);
frame.add(panel);
}
private void init() {
Image image = Toolkit.getDefaultToolkit().getImage(
getClass().getResource("icon.png"));
frame.setIconImage(image);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setResizable(false);
frame.setVisible(true);
frame.setTitle("Infested");
frame.addKeyListener(new Key());
}
}
字符加载:
public static void createCharacter() {
speed = 3;
character.setIcon(CharacterSheet.downRightLeg);
Frame.panel.add(character);
px = 200;
py = 200;
character.setLocation(px, py);
characterObj = new Rectangle(charX, charY, 56, 63);
}