美好的一天,
我创建了多个面板,第一个面板正在显示。我需要将面板移除,并在用户单击下一个图标时添加新面板。在下面的代码中,面板引用在动作侦听器中无法识别。我该如何解决这个问题?
int n=0;
for (int l=0; l < layOutPanelCount; l++) {
layOutPanel[l] = new JPanel();
layOutPanel[l].setLayout(null);
layOutPanel[l].setBounds(0, 0, screenWidth, screenHeight);
ImageIcon nextIcon = new ImageIcon("src/icons/next.png");
JLabel nextLabel = new JLabel(nextIcon);
nextLabel.setBounds(xPos, yPos, 48, 48);
nextLabel.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
layOutFrame.remove(layOutPanel[l]);
layOutFrame.add(layOutPanel[l + 1]);
//Here the problem occurs, the layOutPanel[] is not recognized.
}
});
layOutPanel[l].add(nextLabel);
}
layOutFrame.add(layOutPanel[1]);