首页 > 解决方案 > 如何在 Java 中获取 JFrame 的子子组件

问题描述

我有一个JFrame看起来像这样的:

JFrame
|-Top Level JPanel
|--JPanel
|---All of the JPanel Components
|--JPanel
|---All of the JPanel Components
|--JPanel
|---All of the JPanel Components

正如您所看到的,有一个容器面板,其中包含三个子 JPanel 和这些子 JPanel 下方的一些组件,这些是我需要访问的组件。

我不能这样做的原因是因为我需要在子 JPanel 之一的按钮中访问它。

这是我当前的代码:

        this.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                CreateNewProjectMenuFrame createNewProjectMenuFrame = (CreateNewProjectMenuFrame) SwingUtilities.getWindowAncestor((Component) event.getSource());
                
                ArrayList<Component> createNewProjectMenuFrameComponents = new ArrayList<Component>(Arrays.asList(createNewProjectMenuFrame.getComponents()));
                ArrayList<Component> createNewProjectMenuFrameChildComponents;
                for (Component component : createNewProjectMenuFrameComponents) {
                    createNewProjectMenuFrameChildComponents.addAll(new ArrayList<Component>(Arrays.asList(component.getComponents())))
                }
                
                ProjectData projectData = new ProjectData();
                
                if (createNewProjectMenuFrame != null) {
                    createNewProjectMenuFrame.dispose();  // dispose of it
                }
            }
        });

但是,该Component对象没有getComponents()功能,所以我如何访问这些组件?

标签: javaswing

解决方案


它需要递归调用。

禁用所有容器

例如

    public void enableComponents(Container container, boolean enable) {
        Component[] components = container.getComponents();
        for (Component component : components) {
            component.setEnabled(enable);
            if (component instanceof Container) {
                enableComponents((Container)component, enable);
            }
        }
    }

完整代码

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class RecursiveCallInContainer {

    public void enableComponents(Container container, boolean enable) {
        Component[] components = container.getComponents();
        for (Component component : components) {
            component.setEnabled(enable);
            if (component instanceof Container) {
                enableComponents((Container) component, enable);
            }
        }
    }

    RecursiveCallInContainer() {
        JPanel gui = new JPanel(new BorderLayout());

        final JPanel container = new JPanel(new BorderLayout());
        gui.add(container, BorderLayout.CENTER);

        JToolBar tb = new JToolBar();
        container.add(tb, BorderLayout.NORTH);
        for (int ii = 0; ii < 3; ii++) {
            tb.add(new JButton("Button"));
        }

        JTree tree = new JTree();
        tree.setVisibleRowCount(6);
        container.add(new JScrollPane(tree), BorderLayout.WEST);

        container.add(new JTextArea(5, 20), BorderLayout.CENTER);

        final JCheckBox enable = new JCheckBox("Enable", true);
        enable.addActionListener((ActionEvent ae) -> {
            enableComponents(container, enable.isSelected());
        });
        gui.add(enable, BorderLayout.SOUTH);

        JOptionPane.showMessageDialog(null, gui);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new RecursiveCallInContainer();
        });
    }
}

推荐阅读