1

我注册到 cardPanel 对象 3 JPanel。

JPanel cardPanel = new JPanel(new CardLayout());

cardPanel.add(viewPerf, "Performances");
cardPanel.add(viewFaults, "Faults");
cardPanel.add(viewGraph, "Throughput Graph");

我会得到一个 JPanel。我该怎么做这个操作?

4

1 回答 1

3

要在 a 中显示特定组件CardLayout,请使用该CardLayout#show(String)方法,并传递您在添加组件时为其提供的名称:

((CardLayout)cardPanel.getLayout()).show("Throughput Graph");

或者,您可以在创建布局时保存布局:

CardLayout cardPanelLayout = new CardLayout();
cardPanel.setLayout(cardPanelLayout);
...
cardPanelLayout.show("Throughput Graph");

编辑:要获得对当前可见组件的引用,我相信您可以这样做:

Component visibleComp = null;
for (Component comp : cardPanel.getComponents()) {
  if (comp.isVisible()) {
    visibleComp = comp;
    break;
  }
}
于 2012-07-12T12:08:58.187 回答