且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从具有GridBagLayout布局的JPanel获取位于特定gridx中的组件,gridy?

更新时间:2023-12-05 11:19:22

最简单的解决方案可能是使用 GridBagLayout #getConstraints(Component) 并简单地遍历所有组件,直到找到符合所需网格位置的组件...

The simplest solution might be to use GridBagLayout#getConstraints(Component) and simply loop through all the components until you find one that matches the required grid position...

Component match = null;
GridBagLayout layout = ...
for (Component comp : getComponents()) {
    GridBagConstraints gbc = layout.getConstraints(comp);
    if (gbc.gridx = x && gbc.gridy = y) {
        match = comp;
        break;
    }
}