Java Swing - 如何禁用 JPanel?
我的 JPanel
上有多个 JComponent
,我想在按下“开始”按钮时禁用所有这些组件。
目前,我正在显式禁用所有组件,
component1.setEnabled(false);
:
:
但是有没有办法可以一次性禁用所有组件?我尝试禁用添加这些组件的 JPanel
panel.setEnabled(false);
但它不起作用。
I have several JComponent
s on a JPanel
and I want to disable all of those components when I press a Start button.
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
panel.setEnabled(false);
but it didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
该面板应该有一个
getComponents()
方法,可以在循环中使用来禁用子组件,而无需显式命名它们。The panel should have a
getComponents()
method which can use in a loop to disable the sub-components without explicitly naming them.禁用面板提供对两种方法的支持。一种是递归地禁用组件,另一种是用禁用的外观“绘制”面板。
The Disabled Panel provides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.
以下方法使用递归来实现此目的。传入任何
Container
,此方法将返回一个Component
数组,其中包含位于Container
“内部”任何位置的所有非 Container 组件。只需循环返回数组的元素并禁用组件即可。
The following method uses recursion to achieve this. Pass in any
Container
, and this method will return aComponent
array of all of the non-Container components located anywhere "inside" of theContainer
.Simply loop through the elements of the returned array and disable the components.
使用JXLayer,以及LockableUI< /a>.
Use the JXLayer, with LockableUI.
以下方法应该是您需要添加的全部内容,您可以使用
setEnableRec(panel, true)
或setEnableRec(panel, false)
调用它:The following method should be all what you need to add, you can call it with
setEnableRec(panel, true)
orsetEnableRec(panel, false)
:禁用应该递归发生:
Disabling should occur recursively: