Question

so I am trying to separate my panels in different layers but it does not work, what I am doing wrong? How to manage the layers? I want the table to be all the way back , and the panel to be in the middle and then the wkp to be on top of all. Thanks in advance.

public class Main  {
    private static JFrame frame = new MyFrame();
    private static WhiteKingPanel wkp = new WhiteKingPanel();
    private static MyPanel panel = new MyPanel();
    private static TablePanel table = new TablePanel();
    private static JLayeredPane lpane = new JLayeredPane();

public static void main(String[] args) throws InterruptedException{

    EventQueue.invokeLater(new Runnable(){
        public void run(){
             frame.setLayout(new BorderLayout());
                 frame.add(lpane, BorderLayout.CENTER); 
                 wkp.setOpaque(true);
                 wkp.setBounds(0, 0, 50, 50);
                 lpane.add(wkp, new Integer(3), 0);
                 lpane.add(panel, new Integer(2), 0);
                 lpane.add(table, new Integer(1), 0);
                 table.setOpaque(true);
             frame.setTitle("ImageTest");
             frame.setBackground(Color.BLACK);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setBounds(150, 100, 512, 512);
             frame.add(wkp);
             frame.add(table);
                         frame.pack();
             frame.setVisible(true);
             WindowListener listener = new Terminator();
             frame.addWindowListener(listener);

        }

    });

    }


}
Was it helpful?

Solution

The thing about JLayerPane is that you need to set the bounds for all the components you add.

            wkp.setBounds(0, 0, 100, 100);
            panel.setBounds(50, 50, 100, 100);
            table.setBounds(100, 100, 100, 100);

Also set the JLayerPane to be the content pane

            frame.setContentPane(lpane);
            //frame.add(wkp);             No need to add these to the frame
            //frame.add(table);
            //frame.add(lpane);

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {

    private static JFrame frame = new MyFrame();
    private static JPanel wkp = new JPanel();
    private static JPanel panel = new JPanel();
    private static JPanel table = new JPanel();
    private static JLayeredPane lpane = new JLayeredPane();

    public static void main(String[] args) throws InterruptedException {

        EventQueue.invokeLater(new Runnable() {
            public void run() {

                wkp.setBackground(Color.YELLOW);
                panel.setBackground(Color.GREEN);
                table.setBackground(Color.BLUE);

                frame.setLayout(new BorderLayout());
                frame.add(lpane, BorderLayout.CENTER);
                wkp.setOpaque(true);
                table.setOpaque(true);
                panel.setOpaque(true);
                lpane.add(wkp, new Integer(3), 0);
                lpane.add(panel, new Integer(2), 0);
                lpane.add(table, new Integer(1), 0);
                wkp.setBounds(0, 0, 100, 100);
                panel.setBounds(50, 50, 100, 100);
                table.setBounds(100, 100, 100, 100);
                frame.setTitle("ImageTest");
                frame.setBackground(Color.BLACK);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setContentPane(lpane);
                //frame.add(wkp);
                //frame.add(table);
                //frame.add(lpane);
                frame.pack();
                frame.setVisible(true);

            }
        });
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top