javaimageswingjlayeredpane

Is there a limit to the amount of layers for a JLayeredPane in java?


I have a java application for an ATC. I just got started with the GUI. First I have a Mainframe, a JLayeredPane on this mainframe, and panels with labels(having ImageIcons) inside them on the JLayeredPane.

I have successfully added about 4 panels(panels have labels,and labels have ImageIcons) to the JLayeredPane. When I go to add the fifth panel, it gives me a wrong GUI display.

This what I get before adding pnlplane(4 layers):

This is what I get(when I try to add a pnlplane - 5 layers[problem]):

This is what I should have got:

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class GUI extends JFrame {

JFrame main = new JFrame();

JLayeredPane jp = new JLayeredPane();

//Add JPanels here
JPanel pnlbackground = new JPanel();
JPanel pnlrunwayone = new JPanel();
JPanel pnlrunwaytwo = new JPanel();
JPanel pnlholding = new JPanel();
JPanel pnlplane = new JPanel();




//Add ImageIcons here
ImageIcon imgbackground = new ImageIcon("background.gif");
ImageIcon imgrunwayone = new ImageIcon("runway01.gif");
ImageIcon imgrunwaytwo = new ImageIcon("runway01.gif");
ImageIcon imgholding = new ImageIcon("holding01.gif");
ImageIcon imgplane = new ImageIcon("plane.gif");




//Add JLabels here
JLabel lblbackground = new JLabel(imgbackground);
JLabel lblrunwayone = new JLabel(imgrunwayone);
JLabel lblrunwaytwo = new JLabel(imgrunwaytwo);
JLabel lblholding = new JLabel(imgholding);
JLabel lblplane = new JLabel(imgplane);




public GUI() {

    //Background
    pnlbackground.setOpaque(false);
    pnlbackground.setBounds(0, -5, 1024, 768);
    pnlbackground.add(lblbackground);

    //Runway one
    pnlrunwayone.setOpaque(false);
    pnlrunwayone.setBounds(170, 404, 685, 39);
    pnlrunwayone.add(lblrunwayone);

    //Runway two
    pnlrunwaytwo.setOpaque(false);
    pnlrunwaytwo.setBounds(170, 443, 685, 39);
    pnlrunwaytwo.add(lblrunwaytwo);

    //        Holding pattern
    pnlholding.setOpaque(false);
    pnlholding.setBounds(0, 00, 330, 143);
    pnlholding.add(lblholding);

    //plane
    pnlholding.setOpaque(false);
    pnlholding.setBounds(0, 0, 48, 60);
    pnlholding.add(lblplane);


    //Adding them to each other
    add(jp);
    jp.add(pnlbackground, new Integer(0));
    jp.add(pnlrunwayone, new Integer(1));
    jp.add(pnlrunwaytwo, new Integer(2));
    jp.add(pnlholding, new Integer(3));
    jp.add(pnlplane, new Integer(4));




    //MainFrame properties
    setSize(1024, 768);
    setBackground(Color.BLACK);
    setTitle("Air Traffic Control");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);


}

public static void main(String[] args) {

    new GUI();

}
}

The easier way out is to go and put all the images together to one big image in photoshop. However, I would like to know if there is any other fix available out there.


Solution

  • Your code seems to work fine for me. That is I see 5 images.

    This looks a little strange:

    pnlholding.setOpaque(false); 
    pnlholding.setBounds(0, 00, 330, 143); 
    pnlholding.add(lblholding); 
    
    //plane 
    pnlholding.setOpaque(false); 
    pnlholding.setBounds(0, 0, 48, 60); 
    pnlholding.add(lblplane); 
    

    I think the lblplane should be added to pnlplane.

    Even easier is to just add the label to the layered pane directly, there is no need to add it to a panel first. This is the way the Swing tutorial on How to Use Layered Panes works.