JLayeredPane中的顶层未显示

时间:2013-04-23 04:33:17

标签: java swing jpanel jlayeredpane

我正在尝试创建一个使用JLayeredPane的面板,使面板位于另一个更大的面板上方。我似乎无法让较小的面板显示出来。关于我做错了什么的想法?

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);
}

}

我已经尝试了.setOpaque()的每个组合,我也能想到。

可以找到CircleLayout here

1 个答案:

答案 0 :(得分:1)

基本上,当你不应该使用setSize时,preferredSize当你不需要时... {/ p>

例如。

在你应该覆盖MainPanel时调用setSize(900, 900);的{​​{1}}的构造函数中,然后在您要添加到getPreferredSize所调用的JLayeredPane的面板上setPreferredSize 1}}但是JLayeredPane没有用于检查此值的布局管理器,因此这些面板的大小保持为0x0。

此外,我不确定你打算通过调用players.setPreferredSize(players.getPreferredSize());获得什么,基本上将首选大小设置为0x0: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);
    }
}