问题描述
如何在父 JPanel
中看到添加的 JPanel
?
How to make an added JPanel
visible inside a parent JPanel
?
我正在使用Netbeans来设计我的UI。
I am using Netbeans for designing my UI.
我有一个 MainFrame.java
,其中包含两个面板;即 headerPanel
和 bodyPanel
。
I have a MainFrame.java
, which contains two panels; namely headerPanel
and bodyPanel
.
In headerPanel
我已经放了三个按钮,让它为 button1
, button2
和 button3
。
In headerPanel
I have put three buttons,let it be button1
, button2
and button3
.
此外,我创建了三个单独的文件,扩展 JPanel
,将其命名为 panel1
, panel2
和 panel3
。
Also I have created three separate files extending JPanel
, name it panel1
, panel2
and panel3
.
然后我在 MainFrame.java $中添加了
bodypanel
内的所有三个面板c $ c>在构造函数中。
Then I added all my three panels inside bodypanel
in MainFrame.java
in constructor.
bodyPanel.add(panel1);
bodyPanel.add(panel2);
bodyPanel.add(panel3);
我想点击相应的按钮,只有相应的面板应出现在 bodypanel
在大型机中,即如果我点击 button1
,则应显示 panel1
。
I want that on clicking the respective buttons only respective panels should appear in the bodypanel
in mainframe, i.e. if I click button1
then panel1
should be displayed.
我已经在 button1
鼠标监听器方法中尝试了以下代码:
I have already tried the following code in button1
mouse listener method:
bodyPanel.validate();
bodyPanel.getComponent(0).setVisible(true);
但 panel1
不会出现。我这样做是因为面板中添加的组件被分配了索引。所以首先我尝试获取组件然后使其可见。它不起作用。
But panel1
does not appear. I did it cause added components in a panel are allotted index. So first I tried to get the components and then make it visible. It did not work.
推荐答案
使用,如图所示。
这篇关于在父JPanel中显示添加的JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!