“如果用户是在面板的开始或面板的末尾,有没有办法喜欢禁用按钮?”
根据如何使用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; }}
不知道为什么他们没有提供这种有用的功能。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)