当添加到多个其他 JPanel 时,JPanel 消失
JPanel disappears when added to more than one other JPanel
希望大家能帮我解开这个谜团。我创建了一个 JPanel,其中包含一个 "Go Back" 按钮,并且有一个我喜欢的漂亮布局。我想将此 JPanel(从这里开始我将其称为 homeButtonPanel)添加到其他几个 JPanel,因为我希望它们都有一个 "Go Back" 按钮。
我将homeButtonPanel添加到JPanel gameRoom,然后添加到JPanel gamePlay。当 gameRoom 在主 JFrame 中显示时,没有显示 homeButtonPanel。当 gamePlay 显示在主 JFrame 中时,显示的是 homeButtonPanel 。这么长时间我都想不通。
经过如此多的困惑和沮丧,我意识到当我注释掉将 homeButtonPanel 添加到 gamePlay 面板的行时,homeButtonPanel 将显示在 gameRoom 面板上。
为什么我无法将此 JPanel 添加到多个附加 JPanel 中?
(也供参考,如果重要的话,我使用 CardLayout 在显示的 JPanel 之间切换)
//Set up of the GameRoom Panel
//**********************************************************************
JPanel gameRoom = new JPanel();
//create welcome label
JLabel welcomeGameRoom = new JLabel("Welcome to the GameRoom");
//create the go home button (and its panel)
JPanel homeButtonHolder= new JPanel();
JButton goHome = new JButton("Go Home");
goHome.setVisible(true);
homeButtonHolder.add(goHome);
//add the go home holder to the gameplay panel
gameRoom.add(homeButtonHolder);
//add the welcome label to the gameplay panel
gameRoom.add(welcomeGameRoom);
//add the gameroom panel to the card panel
//Set up of the GamePlay Panel
//**********************************************************************
JPanel gamePlay = new JPanel();
JLabel welcomeGamePlay = new JLabel("Welcome to the Game");
//add the go home holder to the gameplay panel
//*****This is the line that is the issue ***************
gamePlay.add(homeButtonHolder);
//add the welcome label to the gameplay panel
gamePlay.add(welcomeGamePlay);
I added homeButtonPanel to JPanel gameRoom, and then to JPanel gamePlay.
一个组件只能有一个父组件,所以不能将同一个组件添加到多个面板。
因此您需要创建两个 "homeButtonPanel" 实例,然后向每个面板添加一个实例。
另一种选择是让您的主面板使用 BorderLayout。然后使用 CardLayout 将面板添加到 BorderLayout 的中心。然后可以将 "homeButtonPane" 添加到此面板的 PAGE_END,所以现在即使在交换面板时,homeButtonPanel 也属于 CardLayout 中的两个面板。
希望大家能帮我解开这个谜团。我创建了一个 JPanel,其中包含一个 "Go Back" 按钮,并且有一个我喜欢的漂亮布局。我想将此 JPanel(从这里开始我将其称为 homeButtonPanel)添加到其他几个 JPanel,因为我希望它们都有一个 "Go Back" 按钮。
我将homeButtonPanel添加到JPanel gameRoom,然后添加到JPanel gamePlay。当 gameRoom 在主 JFrame 中显示时,没有显示 homeButtonPanel。当 gamePlay 显示在主 JFrame 中时,显示的是 homeButtonPanel 。这么长时间我都想不通。
经过如此多的困惑和沮丧,我意识到当我注释掉将 homeButtonPanel 添加到 gamePlay 面板的行时,homeButtonPanel 将显示在 gameRoom 面板上。
为什么我无法将此 JPanel 添加到多个附加 JPanel 中?
(也供参考,如果重要的话,我使用 CardLayout 在显示的 JPanel 之间切换)
//Set up of the GameRoom Panel
//**********************************************************************
JPanel gameRoom = new JPanel();
//create welcome label
JLabel welcomeGameRoom = new JLabel("Welcome to the GameRoom");
//create the go home button (and its panel)
JPanel homeButtonHolder= new JPanel();
JButton goHome = new JButton("Go Home");
goHome.setVisible(true);
homeButtonHolder.add(goHome);
//add the go home holder to the gameplay panel
gameRoom.add(homeButtonHolder);
//add the welcome label to the gameplay panel
gameRoom.add(welcomeGameRoom);
//add the gameroom panel to the card panel
//Set up of the GamePlay Panel
//**********************************************************************
JPanel gamePlay = new JPanel();
JLabel welcomeGamePlay = new JLabel("Welcome to the Game");
//add the go home holder to the gameplay panel
//*****This is the line that is the issue ***************
gamePlay.add(homeButtonHolder);
//add the welcome label to the gameplay panel
gamePlay.add(welcomeGamePlay);
I added homeButtonPanel to JPanel gameRoom, and then to JPanel gamePlay.
一个组件只能有一个父组件,所以不能将同一个组件添加到多个面板。
因此您需要创建两个 "homeButtonPanel" 实例,然后向每个面板添加一个实例。
另一种选择是让您的主面板使用 BorderLayout。然后使用 CardLayout 将面板添加到 BorderLayout 的中心。然后可以将 "homeButtonPane" 添加到此面板的 PAGE_END,所以现在即使在交换面板时,homeButtonPanel 也属于 CardLayout 中的两个面板。