【问题标题】:How to get the sub-sub-components of a JFrame in Java如何在Java中获取JFrame的子子组件
【发布时间】:2020-08-25 18:17:13
【问题描述】:

我有一个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() 函数,那么我如何访问这些组件?

【问题讨论】:

  • 一般来说,您会在 JPanel 类中包含更新方法。您将使用链式方法访问更新方法; getFrame().getUpperPanel().updateJLabelText(text); getFrame 是 JFrame 类中的方法,getUpperPanel 是 JFrame 类中的方法,updateJLabelText 是第一个(上)JPanel 类中的方法。
  • 我建议您在创建组件时将引用保留为组件的类字段。如果您想要更详细的建议,我将需要查看更多代码。创建一个minimal reproducible example,说明您在此处尝试执行的最少操作。

标签: java swing


【解决方案1】:

需要递归调用。

E.G.

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

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2018-05-31
    • 1970-01-01
    • 2016-06-18
    相关资源
    最近更新 更多