加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 05:45:50 所属栏目:Java 来源:网络整理
导读:我一直在查看Swing CardLayout的文档,似乎没有任何方法可以确定哪个卡当前显示在课堂上.然而,必须有一种方法来询问布局当前显示哪张卡,对吧? 由于项目的限制,我不能简单地扩展它,并添加此功能的子类,所以如果没有这样的功能,这是否意味着我坚持跟踪组件的状
我一直在查看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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读