我目前正在使用Netbeans GUI构建器创建一个java swing GUI。我试图动态地将一堆面板添加到另一个面板但它们没有显示出来。我已经尝试从可能有意义的每个点使用repaint(),revalidate(),updateUI()和setvisible(true)。我还测试过是否将面板添加到容器中。它们被添加,只是不显示。
以下是添加面板的代码:
public void setArrangement(LinkedList<PanelScheduleMusician> scheduledPlaces, LinkedList<PanelMusician> assignedMusicians, LinkedList<PanelMusician> freeMusicians) {
_scheduledPlaces = scheduledPlaces;
_assignedMusicians = assignedMusicians;
_freeMusicians = freeMusicians;
}
public void displayPanels() {
if(_assignedMusicians != null && _assignedMusicians.size() > 0) {
Iterator<PanelScheduleMusician> iterator = _scheduledPlaces.iterator();
for(PanelMusician p: _assignedMusicians) {
iterator.next().setMusician(p);
}
}
if(_scheduledPlaces != null && _scheduledPlaces.size() > 0) {
GroupLayout layout = new GroupLayout(panelAssignedMusician);
panelAssignedMusician.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.ParallelGroup parallelGroup = layout.createParallelGroup();
GroupLayout.SequentialGroup sequentialGroup = layout.createSequentialGroup();
for(PanelScheduleMusician p: _scheduledPlaces) {
parallelGroup.addComponent(p);
sequentialGroup.addComponent(p);
}
layout.setHorizontalGroup(parallelGroup);
layout.setVerticalGroup(sequentialGroup);
}
if(_freeMusicians != null && _freeMusicians.size() > 0) {
GroupLayout layout = new GroupLayout(panelFreeMusician);
panelFreeMusician.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
GroupLayout.ParallelGroup parallelGroup = layout.createParallelGroup();
GroupLayout.SequentialGroup sequentialGroup = layout.createSequentialGroup();
for(PanelMusician p: _freeMusicians) {
parallelGroup.addComponent(p);
sequentialGroup.addComponent(p);
}
layout.setHorizontalGroup(parallelGroup);
layout.setVerticalGroup(sequentialGroup);
}
}
public void reset() {
removeAll();
initialise();
}
private void initialise() {
initComponents();
displayPanels();
revalidate();
updateUI();
}
在这里我创建了面板并调用方法:
public FormEditDuty() {
initComponents();
LinkedList<PanelScheduleMusician> l1 = new LinkedList<>();
LinkedList<PanelMusician> l2 = new LinkedList<>();
LinkedList<PanelMusician> l3 = new LinkedList<>();
l1.add(new PanelScheduleMusician());
l1.add(new PanelScheduleMusician());
l1.add(new PanelScheduleMusician());
l1.add(new PanelScheduleMusician());
l3.add(new PanelMusician());
l3.add(new PanelMusician());
l3.add(new PanelMusician());
panelVioline1.setArrangement(l1, l2, l3);
panelVioline1.reset();
}
我创建面板的方式只是一个测试,但它需要工作才能继续前进。 如果我直接在同一个类中创建和添加面板,它可以正常工作。