Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java swing动态jpanel内容_Java_Swing_Jpanel - Fatal编程技术网

Java swing动态jpanel内容

Java swing动态jpanel内容,java,swing,jpanel,Java,Swing,Jpanel,我正在使用JavaSwing完成我的第一步,并使用Netbeans绘制布局。 Netbeans创建了一个静态布局,但我需要一点动态。 我已经设法将JScrollPane的内容替换为: jspIngredients.remove(jpIngredients); jspIngredients.setViewportView(new JPIngredients(brewRecipe)); pack(); 这很好,我自己的JPanel在之后的滚动窗格中可见 现在,我对面板有相同的要求,但它不起作用:

我正在使用JavaSwing完成我的第一步,并使用Netbeans绘制布局。 Netbeans创建了一个静态布局,但我需要一点动态。 我已经设法将
JScrollPane
的内容替换为:

jspIngredients.remove(jpIngredients);
jspIngredients.setViewportView(new JPIngredients(brewRecipe));
pack();
这很好,我自己的
JPanel
在之后的滚动窗格中可见

现在,我对面板有相同的要求,但它不起作用:

jpPrevStep.removeAll();
jpPrevStep.add(new JPBrewStep(brewRecipe.getBrewStepbyPosition(1)));
jpPrevStep.revalidate();
jpPrevStep.repaint(); 
jpPrevStep
是一个面板,它是Netbeans创建的布局的一部分。 在这个面板中,我想展示我自己的内容,这也是一个面板(名为
JPBrewStep

但是窗口中没有显示任何内容,因此
.removeAll()
可以工作,但添加新内容却不行

我还尝试通过布局而不是直接添加我的新内容:

jpCurrentStep.remove(jPanel1);
jPanel1 = new JPBrewStep(brewRecipe.getBrewStepbyPosition(1));
javax.swing.GroupLayout jpCurrentStepLayout = new javax.swing.GroupLayout(jpCurrentStep);
jpCurrentStepLayout.setHorizontalGroup(jpCurrentStepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
jpCurrentStepLayout.setVerticalGroup(jpCurrentStepLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel1,javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE));
jpCurrentStep.revalidate();
jpCurrentStep.repaint();
谢谢你的回答。 我补充说

现在它开始工作了

完整代码pice现在为:

jpPrevStep.removeAll();
jpPrevStep.setLayout(new java.awt.CardLayout());
jpPrevStep.add(new JPBrewStep(brewRecipe.getBrewStepbyPosition(1)), "card2");
jpPrevStep.revalidate();
jpPrevStep.repaint();
很抱歉,我没有粘贴更多代码,但我使用netbeans设计编辑器创建了布局,该编辑器生成了大量代码。
netbeans似乎默认使用grouplayout

让我们全面、快速了解您的问题的最好方法可能是,您创建并发布一个小而完整的程序,该程序只有必要的代码来演示您的问题,我们可以复制、粘贴、编译和运行,而无需修改。这段代码足够小,可以在你的问题中完整地以代码格式的文本发布。此外,你没有告诉我们涉及的布局经理,这是关键信息。我可以告诉您的一件事是,如果可以避免的话,您不想添加/删除使用GroupLayout的容器。将布局更改为其他更为程序员友好的布局。除了已经提供的sage建议之外:在将一个组件/面板换成另一个组件/面板时,请使用中所示的。使用卡片布局时,也可以保留当前布局。它只是需要一个额外的容器用于卡片布局。
jpPrevStep.removeAll();
jpPrevStep.setLayout(new java.awt.CardLayout());
jpPrevStep.add(new JPBrewStep(brewRecipe.getBrewStepbyPosition(1)), "card2");
jpPrevStep.revalidate();
jpPrevStep.repaint();