如何禁用 JPanel 中的所有组件

2022-01-21 00:00:00 components java swing jpanel

在我的 JPanel 中,我有许多组件,包括其他 JPanel、JLabels、JTextAreas 和 JButton.因为我想实现一个教程模式,其中出现另一个窗口并且我的主 JPanel 中的所有内容都被禁用,因为新窗口一个一个地解释了每个功能"......我想知道如何禁用我里面的所有组件原始JPanel.我知道你可以使用:

In my JPanel I have many components, including other JPanels, JLabels, JTextAreas, and JButtons. Becuase I want to implement a tutorial mode where another window appears and everything in my main JPanel is disabled as the new window explains each 'feature' one by one... I want a to know how to disable all the components that are inside my origiinal JPanel. I know you can use:

component.setEnabled(false);

但我不想为我的 JPanel 中的每个组件编写它.我想知道是否可以使用 for 循环或其他方式禁用我的 JPanel 中的所有组件?

But I don't want to write it for each component in my JPanel. I would like to know if it's possible to disable ALL components within my JPanel with a for loop or something?

注意:嵌套的 JPanel 中也有组件,就像顺序一样

Note: There are also component in nested JPanels, like the order would be

主 JPanel ---> 嵌套 JPanel ---> 组件

Main JPanel ---> Nested JPanel ---> Component

我还希望最终组件也被禁用...

I also want the Final components to also be disabled...

谢谢!感谢所有帮助!

推荐答案

查看 禁用面板有几个解决方案.

Check out Disabled Panel for a couple of solutions.

一个使用禁用的 GlassPane 类型的方法,另一个递归地禁用组件,同时跟踪组件的当前状态,以便以后可以正确启用.

One uses a disabled GlassPane type of approach and the other recursively disables components while keep track of the components current state so it can be enable properly later.

相关文章