JPanel 数组仅显示数组中的最后一个面板
我创建了一个 JPanel 数组,其中包含一个常见的 JLabel
class gui
{
JPanel[] multpanel;
JPanel finalPane = new JPanel();
JLabel InputLabel = new JLabel("Input Files");
gui()
{
InputLabel.setLocation(50,50);
InputLabel.setSize(120,20);
int total_instances=2;
multpanel=new JPanel[total_instances];
for(int instance=0;instance<total_instances;instance++)
{
multpanel[instance]=new JPanel();
multpanel[instance].setLocation(10,0);
multpanel[instance].setSize(500,500);
multpanel[instance].setLayout(null);
multpanel[instance].add(InputLabel);
}
finalPane.add(multpanel[0]);
finalPane.add(multpanel[1]);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getContentPane().add(finalPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}
,这是我的程序的简短版本,我创建了面板数组,并且一次只有一个面板可见 我的问题是,它只显示数组中的最后一个面板,在我的情况下,显示数组的第二个面板,当我尝试显示第一个面板时,它不会显示任何内容,
就像我有大小为 5 的面板数组一样,则只显示第 5 个面板,而所有其他面板面板显示空白
这是因为我在其中添加了通用标签
请帮忙
I created a array of JPanels which contains a common JLabel
class gui
{
JPanel[] multpanel;
JPanel finalPane = new JPanel();
JLabel InputLabel = new JLabel("Input Files");
gui()
{
InputLabel.setLocation(50,50);
InputLabel.setSize(120,20);
int total_instances=2;
multpanel=new JPanel[total_instances];
for(int instance=0;instance<total_instances;instance++)
{
multpanel[instance]=new JPanel();
multpanel[instance].setLocation(10,0);
multpanel[instance].setSize(500,500);
multpanel[instance].setLayout(null);
multpanel[instance].add(InputLabel);
}
finalPane.add(multpanel[0]);
finalPane.add(multpanel[1]);
JFrame.setDefaultLookAndFeelDecorated(true);
frame.getContentPane().add(finalPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}
this a short version of my program, i creating array of panels and at a time only one panel is visible
My problem is that it only display last panel in array, in my case 2nd panel of array is displayed and when i try to display first panel it displays nothing
like if i have panel array of size five then only 5th panel is displayed and all other panels display blank
Is this because i am adding a common label in it
Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定的组件可能只有一个祖先。因此,当您向面板添加标签时,您实际上是将其从前一个面板中删除。如果您想要 5 个面板中的标签,则需要 5 个标签。
另外两个注意事项:
A given component may only have one ancestor. So, when you add a label to a panel, you're effectively removing it from the previous one. If you want a label in 5 panels, you need 5 labels.
Two additional notes:
使用正确的布局管理器(例如 BoxLayout),并且不要设置面板的大小和位置。
setLayout(null);
<-- 不建议使用它。Use proper LayoutManager e.g. BoxLayout and don't set size and location of the panels.
setLayout(null);
<-- would not recomment to use that.