为什么我在Java中使用JLayeredpane时,为什么我的对象不会全部显示在框架中?

时间:2019-04-04 09:31:03

标签: java jframe jpanel jlayeredpane

我目前正在制作迷宫游戏。播放器完成,地图也正常运行。我现在遇到的问题是,我可以使面板完全将地图包含到我的框架中,但我无法使播放器位于地图上方。当我使用JLayeredPane时,播放器不会显示,而当我不使用它时,播放器将位于地图下方。我有以下课程:

我在哪里制作地图和框架的班级:

 import javax.swing.*;
    import java.awt.*;

   public class Speelveld extends JFrame {
        private int x = 0;
        private int y = 0;
        private Speler speler;
        private JPanel panel;
        private JLayeredPane layeredPane;

        private int[][] map = {
                {5, 5, 3, 3, 3, 5, 0, 5, 0, 5},
                {5, 5, 3, 0, 0, 5, 4, 5, 0, 5},
                {5, 1, 3, 4, 0, 5, 5, 0, 0, 5},
                {0, 3, 3, 5, 6, 5, 0, 5, 0, 0},
                {5, 3, 3, 0, 0, 6, 0, 0, 5, 0},
                {5, 5, 5, 0, 0, 5, 0, 0, 0, 0},
                {5, 4, 5, 5, 4, 5, 0, 5, 0, 0},
                {0, 5, 0, 0, 5, 0, 5, 0, 0, 0},
                {0, 4, 6, 0, 5, 0, 0, 0, 0, 6},
                {0, 0, 6, 0, 0, 0, 0, 0, 5, 2}
        };

    private Vakje[][] vakjes = new Vakje[10][10];


        public Speelveld() {
            createComponents();
            setTitle("Sleutel Barricade");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        }

        public void createComponents() {
            panel = new JPanel();
            speler = new Speler();
            layeredPane = getLayeredPane();
            this.setSize(400,400);
            vakjesAanmaken();
            setLocationRelativeTo(null);
            panel.setLayout(new GridLayout(10,10,1,1));
            panel.setSize(300,300);
            layeredPane.add(panel, new Integer(1));
            layeredPane.add(speler, new Integer(5));

            this.setVisible(true);

        }


        public void vakjesAanmaken() {
            int X = 0;
            int Y = 0;

            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[0].length; j++) {
                    Vakje vakje;
                    switch (map[i][j]) {
                        case 0 :
                            vakje = new Barricade(20,X ,Y);
                            vakjes[i][j] = vakje;
                            break;
                        case 1:
                            vakje = new Sleutel(20, X,Y);
                            vakjes[i][j] = vakje;
                            break;
                        case 2:
                            vakje = new Eindveld(X,Y);
                            vakjes[i][j] = vakje;
                            break;
                        case 3:
                            vakje = new DichtVakje(X,Y);
                            vakjes[i][j] = vakje;
                            break;
                        default:
                            vakje = new LeegVakje(X,Y);
                            vakjes[i][j] = vakje;

                    }
                }
            }
        }

        public void paint(Graphics g){
            super.paint(g);
            g.translate(50,50);
            for (int i = 0; i < vakjes.length; i++) {
                for (int j = 0; j <vakjes[0].length ; j++) {
                    panel.add(vakjes[i][j]);
                }
            }
        }

        public boolean mogelijkeStapRecht(int X,int Y){
            if(X != 10){
                if (map[Y][X + 1] != 3){
                    return true;
                }
                else{
                    return false;
                }
            }
            else{
                return false;
            }
        }
    public boolean mogelijkeStapLinks(int X,int Y){
        if(X != 0){
            if (map[Y][X - 1] != 3){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    public boolean mogelijkeStapOmhoog(int X,int Y){
        if(y != 0){
            if (map[Y-1][X] != 3){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }
    public boolean mogelijkeStapOmlaag(int X,int Y){
        if(X != 10){
            if (map[Y + 1][X] != 3){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

}

我在其中创建播放器的班级:

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class Speler extends JComponent {
    Speelveld speelveld;
    private int zak;
    private int XCoordinaat;
    private  int YCoordinaat;
    private int dx;
    private int dy;
    private int x = 0;
    private int y = 0;
    Image square = new BufferedImage(30, 30, BufferedImage.TYPE_INT_RGB);



    public Speler(){
        addKeyListener (new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                int key = e.getKeyCode();
                if (key == KeyEvent.VK_LEFT) dx -= 30;
                if (key == KeyEvent.VK_RIGHT) dx += 30;
                if (key == KeyEvent.VK_UP) dy -= 30;
                if (key == KeyEvent.VK_DOWN) dy += 30;
                repaint();
            }
        });
        initVeld();
    }
    void initVeld()
    {
        setFocusable(true);
    }



    public void oppakkenControleren(Sleutel sleutel) {
        if (YCoordinaat == sleutel.getYCoordinaat() && XCoordinaat == sleutel.getXCoordinaat()){
            zak = sleutel.getPincode();
        }
    }

    public int getXCoordinaat(){
        return XCoordinaat;
    }

    public int getYCoordinaat(){
        return YCoordinaat;
    }

    public void setXCoordinaat(int XCoordinaat) {
        this.XCoordinaat = XCoordinaat;
    }

    public void setYCoordinaat(int YCoordinaat) {
        this.YCoordinaat = YCoordinaat;
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        drawSquare(g);
    }
    public void drawSquare(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(square, dx + getXCoordinaat(), dy + getYCoordinaat(), this);
    }
}

1 个答案:

答案 0 :(得分:0)

在确定布局管理器之前设置所有大小。

最后一次调用您的方法(将面板设置为可见后)。