gpt4 book ai didi

java - JLayeredPane 中的顶层未显示

转载 作者:行者123 更新时间:2023-12-01 14:39:12 26 4
gpt4 key购买 nike

我正在尝试创建一个面板,该面板使用 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

最佳答案

基本上,当你不应该使用setSize时,你会使用setSize,而当你不需要时,你会使用preferredSize...

例如。

MainPanel 的构造函数中,当您应该覆盖 getPreferredSize 时,您可以调用 setSize(900, 900);,然后在面板上正在添加到您正在调用 setPreferredSizeJLayeredPane,但 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);
}
}

关于java - JLayeredPane 中的顶层未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16161021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com