我需要创建一个Jpanel数组,但也可以使用按钮将正确的面板设置为按下时可见

时间:2020-12-14 22:49:57

This is the code i have for one button setting its corresponding Jpanel to visible and the other jpanels to false.

这是我为一个按钮设置的代码,将其对应的Jpanel设置为可见,将其他jpanel设置为false。

private void twickenhamButtonActionPerformed(java.awt.event.ActionEvent evt) {
    twickenhamPanel.setVisible(true);
    wembleyPanel.setVisible(false);
    ellandPanel.setVisible(false);
    sandyPanel.setVisible(false);
    mkPanel.setVisible(false);
    EtihadPanel.setVisible(false);
    villaParkPanel.setVisible(false);
    stJamesPanel.setVisible(false);
    millenniumPanel.setVisible(false);
    leicesterPanel.setVisible(false);
    kingsholmPanel.setVisible(false);
    OlympicPanel.setVisible(false);
}

But how to i add all of these jpanels to an array, but also use their corresponding buttons to set them to visible when clicked?

但是如何将所有这些jpanel添加到数组中,还可以使用相应的按钮将它们设置为单击时可见?

1 个解决方案

#1


1  

From the looks of your code, it looks like you only want one panel to be visible at a time. This seems like a perfect use of a CardLayout, that will allow you to swap view. You can map a name to each panel.

从代码的外观来看,您似乎只希望一次看到一个面板。这似乎是对CardLayout的完美使用,它将允许您交换视图。您可以将名称映射到每个面板。

private static final String TWICKENHAM = "twickenham";

CardLayout layout = new CardLayout();
JPanel mainPanel = new JPanel(layout);
mainPanel.add(twickenhamPanel, TWICKENHAM);

Then when you want to show the twickenhamPanel, you can just call layout.show(mainPanel, TWICKENHAM). It might all sound foreign to you, but you can learn more at How to Use CardLayout.

然后当你想要显示twickenhamPanel时,你可以调用layout.show(mainPanel,TWICKENHAM)。这对您来说可能听起来很陌生,但您可以在如何使用CardLayout中了解更多信息。

Also by the method signature of your actionPerformed, it looks like you are using Netbeans GUI Builder. If that's the case, you may also want to look at How to use CardLayout with Netbeans GUI Builder

同样通过actionPerformed的方法签名,看起来您正在使用Netbeans GUI Builder。如果是这种情况,您可能还想了解如何将CardLayout与Netbeans GUI Builder一起使用

#1


1  

From the looks of your code, it looks like you only want one panel to be visible at a time. This seems like a perfect use of a CardLayout, that will allow you to swap view. You can map a name to each panel.

从代码的外观来看,您似乎只希望一次看到一个面板。这似乎是对CardLayout的完美使用,它将允许您交换视图。您可以将名称映射到每个面板。

private static final String TWICKENHAM = "twickenham";

CardLayout layout = new CardLayout();
JPanel mainPanel = new JPanel(layout);
mainPanel.add(twickenhamPanel, TWICKENHAM);

Then when you want to show the twickenhamPanel, you can just call layout.show(mainPanel, TWICKENHAM). It might all sound foreign to you, but you can learn more at How to Use CardLayout.

然后当你想要显示twickenhamPanel时,你可以调用layout.show(mainPanel,TWICKENHAM)。这对您来说可能听起来很陌生,但您可以在如何使用CardLayout中了解更多信息。

Also by the method signature of your actionPerformed, it looks like you are using Netbeans GUI Builder. If that's the case, you may also want to look at How to use CardLayout with Netbeans GUI Builder

同样通过actionPerformed的方法签名,看起来您正在使用Netbeans GUI Builder。如果是这种情况,您可能还想了解如何将CardLayout与Netbeans GUI Builder一起使用