Java Swing - 如何禁用 JPanel?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2713425/
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
Java Swing - How to disable a JPanel?
提问by Yatendra Goel
I have several JComponent
s on a JPanel
and I want to disable all of those components when I press a Start button.
我JComponent
在 a 上有几个s,JPanel
当我按下“开始”按钮时,我想禁用所有这些组件。
At present, I am disabling all of the components explicitly by
目前,我正在通过以下方式明确禁用所有组件
component1.setEnabled(false);
:
:
But Is there anyway by which I can disable all of the components at once? I tried to disable the JPanel
to which these components are added by
但是无论如何,我可以一次禁用所有组件吗?我试图禁用JPanel
添加这些组件的
panel.setEnabled(false);
but it didn't work.
但它没有用。
采纳答案by ZeissS
The panel should have a getComponents()
method which can use in a loop to disable the sub-components without explicitly naming them.
面板应该有一个getComponents()
方法,可以在循环中使用来禁用子组件而无需明确命名它们。
回答by Istao
Use the JXLayer, with LockableUI.
使用JXLayer,与LockableUI。
回答by camickr
The Disabled Panelprovides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.
该禁用面板提供了两种方法的支持。一个递归禁用组件,另一个用禁用的外观“绘制”面板。
回答by Joel Christophel
The following method uses recursion to achieve this. Pass in any Container
, and this method will return a Component
array of all of the non-Container components located anywhere "inside" of the Container
.
下面的方法使用递归来实现这一点。传入 any Container
,此方法将返回Component
位于Container
.
private Component[] getComponents(Component container) {
ArrayList<Component> list = null;
try {
list = new ArrayList<Component>(Arrays.asList(
((Container) container).getComponents()));
for (int index = 0; index < list.size(); index++) {
for (Component currentComponent : getComponents(list.get(index))) {
list.add(currentComponent);
}
}
} catch (ClassCastException e) {
list = new ArrayList<Component>();
}
return list.toArray(new Component[list.size()]);
}
}
Simply loop through the elements of the returned array and disable the components.
只需遍历返回数组的元素并禁用组件。
for(Component component : getComponents(container)) {
component.setEnabled(false);
}
回答by toto_tico
The following method should be all what you need to add, you can call it with setEnableRec(panel, true)
or setEnableRec(panel, false)
:
以下方法应该是您需要添加的所有内容,您可以使用setEnableRec(panel, true)
或调用它setEnableRec(panel, false)
:
private void setEnableRec(Component container, boolean enable){
container.setEnabled(enable);
try {
Component[] components= ((Container) container).getComponents();
for (int i = 0; i < components.length; i++) {
setEnableRec(components[i], enable);
}
} catch (ClassCastException e) {
}
}
回答by Karl Richter
Disabling should occur recursively:
禁用应递归发生:
Queue<Component> queue = new LinkedList<>(Arrays.asList(container.getComponents()));
while(!queue.isEmpty()) {
Component head = queue.poll();
head.setEnabled(enable);
if(head instanceof Container) {
Container headCast = (Container) head;
queue.addAll(Arrays.asList(headCast.getComponents()));
}
}