java - 如何在 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()
功能,所以我如何访问这些组件?
解决方案
它需要递归调用。
例如
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();
});
}
}
推荐阅读
- javascript - 如何重写 JavaScript 货币格式以缩写美元金额
- google-apps-script - 数据透视表按值排序
- python - 当映射到熊猫系列时,str.replace 方法在用户定义的函数中不起作用;但是,它适用于正常的函数调用。为什么?
- makefile - 文件名中的 CMake 哈希
- oracle - 只有一列的Oracle递归查询
- c - 如何在C中初始化堆中的只读内存位置?
- bash - 在变量 bash 中使用变量
- vert.x - 在vertx中,如何提供失败操作的结果
- jquery - paperjs选择项目并在鼠标单击时删除
- c# - 为什么成员分配被视为 Func
而不是行动