JLayeredPane的顶层不显示

JLayeredPane的顶层不显示

本文介绍了JLayeredPane的顶层不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个使用JLayeredPane的面板,使该面板居中于另一个较大的面板上方.我似乎无法显示较小的面板.关于我在做什么错的任何想法吗?

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

}

我也尝试过所有我能想到的.setOpaque()组合.

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

CircleLayout可以在此处

CircleLayout can be found here

推荐答案

基本上,您在不应该使用的情况下使用setSize,在不需要...的情况下使用preferredSize.

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

例如.

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

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.

此外,我不确定您通过调用players.setPreferredSize(players.getPreferredSize());会获得什么,这基本上可以通过任何方式将首选大小设置为0x0:P

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

这篇关于JLayeredPane的顶层不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:25