java 如何在java中使一组jbutton不可见
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12945954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a set of jbuttons invisible in java
提问by Adesh
Apart from using button.setVisible(false)
, is there an easy way to set a set of jButtons
to invisible and visible again?
除了使用之外button.setVisible(false)
,是否有一种简单的方法可以将一组设置jButtons
为不可见和再次可见?
The algorithm is as follows - when the user clicks the checkout button, a set of payment buttons(denomination buttons are displayed). Trying to research if there is an easier way to accomplish this.
算法如下——当用户点击结账按钮时,会显示一组支付按钮(显示面额按钮)。尝试研究是否有更简单的方法来实现这一点。
回答by MadProgrammer
The better solution is to have your buttons in an array or List
, but if you can't do that, you can walk the immediate container (JPanel
) looking for all the components that are instances of JButton
更好的解决方案是将你的按钮放在一个数组 or 中List
,但如果你不能这样做,你可以遍历直接容器 ( JPanel
) 寻找所有作为实例的组件JButton
for (Component child : getComponents){
if (child instanceof JButton) {
((JButton)child).setVisible(false);
}
}
This is a little heavy handed so be careful
这有点笨手笨脚所以要小心
回答by Steve Kuo
Put all your UI components (JButton
) in a collection, and create a utility method that iterates over them and set the visible state.
将所有 UI 组件 ( JButton
) 放在一个集合中,并创建一个实用程序方法来迭代它们并设置可见状态。