Top layer in JLayeredPane not displaying

◇◆丶佛笑我妖孽 提交于 2019-12-13 04:20:34

问题


I'm trying to create a panel that uses a JLayeredPane to have a panel centered above another, larger panel. I can't seem to get the smaller panel to display though. Any ideas as to what I'm doing wrong?

import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class MainPanel extends JPanel {
private JLayeredPane pane;
private AllPlayersPanel players; //Larger panel, uses circleLayout
private GamePanel game; //Smaller panel, simple BorderLayout

public MainPanel(){
    super();
    setSize(900, 900);
    setLayout(new BoxLayout(this,  BoxLayout.PAGE_AXIS));       

    pane = new JLayeredPane();
    pane.setPreferredSize(new Dimension(900, 900));     

    players = new AllPlayersPanel();
    players.setPreferredSize(players.getPreferredSize());
    players.setLocation(0,0);
    //players.setOpaque(false);
    pane.add(players, new Integer(0));

    game = new GamePanel();
    game.setPreferredSize(game.getPreferredSize());
    game.setLocation(385, 405);
    //game.setOpaque(false);        
    pane.add(game, new Integer(2));

    add(pane);
}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JComponent newPane = new MainPanel();
    newPane.setOpaque(true);
    frame.setContentPane(newPane);
    frame.pack();
    frame.setVisible(true);
}

}

I've tried every combination of .setOpaque() I can think of as well.

CircleLayout can be found here


回答1:


Basically, you're using setSize when you shouldn't be and preferredSize when you don't need to...

For example.

In you constructor of the MainPanel you call setSize(900, 900); when you should have overridden getPreferredSize then on the panels you are adding to the JLayeredPane you're calling setPreferredSize but the JLayeredPane has no layout manager with which to check this value, so the size of these panels remain as 0x0.

Besides, I'm not sure what you expected to gain from calling players.setPreferredSize(players.getPreferredSize());, which basically will set the preferred size to 0x0 any way :P

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class MainPanel extends JPanel {

    private JLayeredPane pane;
    private JPanel players; //Larger panel, uses circleLayout
    private JPanel game; //Smaller panel, simple BorderLayout

    public MainPanel() {
        super();
//        setSize(900, 900);
//        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        setLayout(new BorderLayout());

        pane = new JLayeredPane();
//        pane.setPreferredSize(new Dimension(900, 900));
        pane.setBorder(new LineBorder(Color.RED));

        players = new JPanel();
        players.setBackground(Color.RED);
        players.setSize(getPreferredSize());
        players.setLocation(0, 0);
        //players.setOpaque(false);
        pane.add(players, new Integer(0));

        game = new JPanel();
        game.setSize(game.getPreferredSize());
        game.setBackground(Color.BLUE);
        game.setLocation(385, 405);
        //game.setOpaque(false);        
        pane.add(game, new Integer(2));

        add(pane);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(900, 900);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        JComponent newPane = new MainPanel();
        newPane.setOpaque(true);
        frame.setContentPane(newPane);
        frame.pack();
        frame.setVisible(true);
    }
}


来源:https://stackoverflow.com/questions/16161021/top-layer-in-jlayeredpane-not-displaying

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