Java Swing - 如何禁用 JPanel?

发布于 2024-08-30 10:29:44 字数 291 浏览 1 评论 0原文

我的 JPanel 上有多个 JComponent,我想在按下“开始”按钮时禁用所有这些组件。

目前,我正在显式禁用所有组件,

component1.setEnabled(false);
:
:

但是有没有办法可以一次性禁用所有组件?我尝试禁用添加这些组件的 JPanel

panel.setEnabled(false);

但它不起作用。

I have several JComponents 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

青春有你 2024-09-06 10:29:44

该面板应该有一个 getComponents() 方法,可以在循环中使用来禁用子组件,而无需显式命名它们。

The panel should have a getComponents() method which can use in a loop to disable the sub-components without explicitly naming them.

自我难过 2024-09-06 10:29:44

禁用面板提供对两种方法的支持。一种是递归地禁用组件,另一种是用禁用的外观“绘制”面板。

The Disabled Panel provides support for two approaches. One to recursively disable components, the other to "paint" the panel with a disabled look.

你怎么这么可爱啊 2024-09-06 10:29:44

以下方法使用递归来实现此目的。传入任何 Container,此方法将返回一个 Component 数组,其中包含位于 Container“内部”任何位置的所有非 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()]);
        }
    }

只需循环返回数组的元素并禁用组件即可。

for(Component component : getComponents(container)) {
    component.setEnabled(false);
}

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.

    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);
}
薯片软お妹 2024-09-06 10:29:44

使用JXLayer,以及LockableUI< /a>.

Use the JXLayer, with LockableUI.

勿挽旧人 2024-09-06 10:29:44

以下方法应该是您需要添加的全部内容,您可以使用 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) {

    }
}

The following method should be all what you need to add, you can call it with setEnableRec(panel, true) or 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) {

    }
}
终难遇 2024-09-06 10:29:44

禁用应该递归发生:

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()));
    }
}

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()));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文