I have a jpanel with gridbaglayout layout, in it i have several jtextfields, several jlabels, several jbuttons which get added dynamically. Therefore I cannot know their specific orders, hence cannot use panel.getComponent(count). I looked in the api if there is something like getGridx(int x) or getGridy(int y). Didn't find. Is there anything similar to those methods?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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;
}
}