Domanda

I have seriously tried everything I can think of from what I have gathered on how to use a JLayeredPane and nothing I do seems to work. First, I create my ImageIcons, then I add those ImageIcons to JLabels, and Then I add that JLabel to the JLayeredPane, and then add the JLayeredPane to a JPanel. I have tried using the setSize, setBounds methods, as well as any other method I could find to size my JLabels before adding them to the JLayeredPane and I can never get my images to display, let alone be layered on top of each other. How do I accomplish this??

Here is my code:

public Class FaceLayout extends JFrame
{

 public FaceLayout()
     {

           ImageIcon face = new ImageIcon(getClass().getResource("Imported Image");
           ImageIcon eyes = new ImageIcon(getClass().getResource("Imported Image");
           ImageIcon nose = new ImageIcon(getClass().getResource("Imported Image");
           ImageIcon mouth = new ImageIcon(getClass().getResource("Imported Image");

           JLabel fLab = new JLabel(face);
                 fLab1.setBounds(25, 25, 50, 50);
           JLabel eLab = new JLabel(eyes);
                 eLab1.setBounds(25, 25, 50, 50);
           JLabel nLab = new JLabel(nose);
                 nLab1.setBounds(25, 25, 50, 50);
           JLabel mLab = new JLabel(mouth);
                 mLab1.setBounds(25, 25, 50, 50);

           JCheckBox eBox = new JCheckBox("Eyes", false);
           JCheckBox nBox = new JCheckBox("Nose", false);
           JCheckBox mBox = new JCheckBox("Mouth", false);

           JButton submit = new JButton("Submit");

           JPanel leftPanel = new JPanel(new GridLayout(4, 1));
                leftPanel.add(eBox);
                leftPanel.add(nBox);
                leftPanel.add(mBox);
                leftPanel.add(submit);

           JLayeredPane layers = new JLayeredPane();
 // Here I have tried the setLayer method and just adding them to the layeredPane itself. Right now I have it as:

           layers.add(fLab, new Integer(1));
           layers.add(eLab, new Integer(2));
           layers.add(nLab, new Integer(3));
           layers.add(mLab, new Integer(4));

           JPanel rightPanel = new JPanel();
                rightPanel.add(layers);

           JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,    rightPanel);
        pane.setDividerLocation(150);

         Dimension lSize = new Dimension(100, 50);
         Dimension rSize = new Dimension(100, 400);
         leftPanel.setMinimumSize(lSize);
         rightPanel.setMinimumSize(rSize);

         add(pane);
     }
}

The leftPanel displays just fine with all of the checkboxes, but all I ever get on the rightPanel is a blank white background and nothing else. What am I doing wrong here??

È stato utile?

Soluzione

The problem is you need to set the layout to null on the container of the JLayeredPane and also set the bounds on the JLayeredPane

JPanel rightPanel = new JPanel();
rightPanel.setLayout(null);
layers.setBounds(0, 0, 300, 300);
rightPanel.add(layers);

Here's a final code

import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;

public class FaceLayout extends JFrame {

    public FaceLayout() {

        ImageIcon face = new ImageIcon(getClass().getResource("/marioblobs/bowser.png"));
        ImageIcon eyes = new ImageIcon(getClass().getResource("/marioblobs/mario.png"));
        ImageIcon nose = new ImageIcon(getClass().getResource("/marioblobs/luigi.png"));
        ImageIcon mouth = new ImageIcon(getClass().getResource("/marioblobs/koopa.png"));

        JLabel fLab = new JLabel(face);
        fLab.setBounds(25, 25, 100, 100);
        JLabel eLab = new JLabel(eyes);
        eLab.setBounds(100, 100, 100, 100);
        JLabel nLab = new JLabel(nose);
        nLab.setBounds(175, 175, 100, 100);
        JLabel mLab = new JLabel(mouth);
        mLab.setBounds(250, 250, 100, 100);

        JCheckBox eBox = new JCheckBox("Eyes", false);
        JCheckBox nBox = new JCheckBox("Nose", false);
        JCheckBox mBox = new JCheckBox("Mouth", false);

        JButton submit = new JButton("Submit");

        JPanel leftPanel = new JPanel(new GridLayout(4, 1));
        leftPanel.add(eBox);
        leftPanel.add(nBox);
        leftPanel.add(mBox);
        leftPanel.add(submit);

        JLayeredPane layers = new JLayeredPane();

        layers.add(fLab, new Integer(1));
        layers.add(eLab, new Integer(2));
        layers.add(nLab, new Integer(3));
        layers.add(mLab, new Integer(4));

        JPanel rightPanel = new JPanel() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 400);
            }
        };
        rightPanel.setLayout(null);
        layers.setBounds(0, 0, 400, 400);
        rightPanel.add(layers);

        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        pane.setDividerLocation(150);

        Dimension lSize = new Dimension(100, 50);
        Dimension rSize = new Dimension(100, 400);
        leftPanel.setMinimumSize(lSize);
        rightPanel.setMinimumSize(rSize);

        add(pane);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new FaceLayout();
            }
        });
    }
}

enter image description here

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top