我一直在查看Swing
CardLayout的文档,似乎没有任何方法可以确定哪个卡当前显示在课堂上.然而,必须有一种方法来询问布局当前显示哪张卡,对吧?
由于项目的限制,我不能简单地扩展它,并添加此功能的子类,所以如果没有这样的功能,这是否意味着我坚持跟踪组件的状态外部组件(呸!)或者是否有一些其他选项埋藏在Swing深处?
解决方法
根据
How to Use CardLayout教程,
Conceptually,each component that a CardLayout manages is like a
playing card or trading card in a stack,where only the top card is
visible at any time. You can choose the card that is showing in any of
the following ways:
By asking for either the first or last card,in the order
it was added to the containerBy flipping through the deck backwards or forwardsBy specifying a card with a specific name
尝试,
Component visibleComponent = foo.getComponent(0);
其中foo是使用CardLayout的JPanel实例.
参考: