Java Swing JLayeredPane not showing up

旧街凉风 提交于 2019-12-24 05:06:28

问题


I seem to be having some major issues with JLayeredPane. I have a BorderLayout() pane, and I'd like for the West-side element to contain a few JLayeredPane's on top of each other, so I can switch between them to show the right information.

The west pane should be 200 pixels wide and should be as long as the total window is. In my sample code I have added two layers to the JLayeredPanel, but they don't show up. They should be in the west pane.

Here is my code:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static JFrame mainFrame = new JFrame();
    private static JPanel mainPane = new JPanel();
    public Main(){}

    public static void initGui(){
        JLayeredPane westPanel = new JLayeredPane();
        westPanel.setPreferredSize(new Dimension(200,0));
        westPanel.setBackground(Color.blue);

        JPanel layerOne = new JPanel();
        layerOne.add(new JLabel("This is layer 1"));
        westPanel.add(layerOne, new Integer(0), 0);

        JPanel layerTwo = new JPanel();
        layerTwo.add(new JLabel("This si layer 2"));
        westPanel.add(layerTwo, new Integer(1), 0);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);

        JPanel eastPanel = new JPanel();
        eastPanel.setPreferredSize(new Dimension(200,0));
        eastPanel.setBackground(Color.red);

        mainPane = new JPanel(new BorderLayout());
        mainPane.add(westPanel, BorderLayout.WEST);
        mainPane.add(centerPanel, BorderLayout.CENTER);
        mainPane.add(eastPanel, BorderLayout.EAST);

        mainFrame = new JFrame("Learning to use JLayeredPane");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setBounds(200, 200, 800, 500);
        mainFrame.setContentPane(mainPane);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        initGui();
    }
}

What this results in:


回答1:


JLayeredPane uses a null layout and so you are responsible for stating the size and location of all components added to it. If not they will default to a location of [0, 0] and a size of [0, 0].




回答2:


try this, its working

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Main {
    private static JFrame mainFrame = new JFrame();
    private static JPanel mainPane = new JPanel();
    public Main(){}

    public static void initGui(){
        JLayeredPane westPanel = new JLayeredPane();
        westPanel.setLayout(null);
        westPanel.setPreferredSize(new Dimension(200,0));
        westPanel.setBackground(Color.blue);

        JPanel layerOne = new JPanel();
        layerOne.add(new JLabel("This is layer 1"));
        layerOne.setBounds(0, 0, 100, 100);
        westPanel.add(layerOne, new Integer(0), 0);

        JPanel layerTwo = new JPanel();
        layerTwo.add(new JLabel("This si layer 2"));
        layerTwo.setBounds(0, 100, 100, 100);
        westPanel.add(layerTwo, new Integer(1), 0);

        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);

        JPanel eastPanel = new JPanel();
        eastPanel.setPreferredSize(new Dimension(200,0));
        eastPanel.setBackground(Color.red);

        mainPane = new JPanel();
        mainPane.setLayout(new BorderLayout());
        mainPane.add(westPanel, BorderLayout.WEST);
        mainPane.add(centerPanel, BorderLayout.CENTER);
        mainPane.add(eastPanel, BorderLayout.EAST);

        mainFrame = new JFrame("Learning to use JLayeredPane");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setBounds(200, 200, 800, 500);
        mainFrame.setContentPane(mainPane);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        initGui();
    }
}


来源:https://stackoverflow.com/questions/20162151/java-swing-jlayeredpane-not-showing-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!