gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:07:56 24 4
gpt4 key购买 nike

我有一个带有 gridbaglayout 布局的 jpanel,其中有几个 jtextfields、几个 jlabels、几个动态添加的 jbuttons。因此我无法知道它们的具体顺序,因此无法使用 panel.getComponent(count)。我查看了 api 是否有 getGridx(int x) 或 getGridy(int y) 之类的东西。没找到。有类似的方法吗?

最佳答案

最简单的解决方案可能是使用 GridBagLayout#getConstraints(Component)然后简单地循环遍历所有组件,直到找到与所需网格位置匹配的组件...

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

关于java - 如何从具有 GridBagLayout 布局的 JPanel 中获取位于特定 gridx、gridy 中的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46299862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com