栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何阻止cardlayout .next()和.previous()循环

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何阻止cardlayout .next()和.previous()循环

“如果用户是在面板的开始或面板的末尾,有没有办法喜欢禁用按钮?”

根据如何使用CardLayout和CardLayout
API
,没有直接的方法可以做到这一点。

但是您可以轻松实现此功能,使int变量与当前卡号保持一致,并再次检查其值ts 0(对于第一张卡)或容器的组件数(对于最后一张卡)。例如:

public class GridBagLayoutDemo { // note pre convention!    int currentCard = 0;    Action backAction, nextAction;    ...    public GridBagLayoutDemo() {        ...        backAction = new AbstractAction("< Back") { @Override public void actionPerformed(ActionEvent e) {     currentCard--;     GridBagLayoutDemo.this.evaluateActions(); }        };        nextAction = new AbstractAction("Next >") { @Override public void actionPerformed(ActionEvent e) {     currentCard++;     GridBagLayoutDemo.this.evaluateActions(); }        };        JButton btnBack = new JButton(backAction);        ...        JButton btnNext = new JButton(nextAction);        ...    }    private void evaluateActions() {        backAction.setEnabled(currentCard > 0);        nextAction.setEnabled(currentCard < container.getComponentCount() - 1);    }    ...}

附录

仔细研究

CardLayout
实现,默认情况下实现此功能真的很容易(除非我遗漏了一些东西):

public class CardLayout implements LayoutManager2,  Serializable {        Vector vector = new Vector();        int currentCard = 0;    ...        public boolean isDisplayingFirstCard() {        return currentCard == 0;    }    public boolean isDisplayingLastCard() {        return currentCard == vector.size() - 1;    }}

不知道为什么他们没有提供这种有用的功能。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470387.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号