Java组件放置

时间:2013-10-21 14:16:01

标签: java components jlabel

为什么草图像不在JFrame的顶部?为什么角色不是从位置200,200开始。他只有在我点击一个键时才会移动。任何人都可以向我解释为什么我会遇到这些问题吗?

enter image description here

框:

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);
}

0 个答案:

没有答案