java – CardLayouts:如何判断哪张卡可见?

前端之家收集整理的这篇文章主要介绍了java – CardLayouts:如何判断哪张卡可见?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在查看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 container
  • By flipping through the deck backwards or forwards
  • By specifying a card with a specific name

尝试,

Component visibleComponent = foo.getComponent(0);

其中foo是使用CardLayout的JPanel实例.

参考:

> How to get the top card in Java’s CardLayout

猜你在找的Java相关文章