如何从具有GridBagLayout布局的JPanel获取位于特定gridx中的组件,gridy? [英] How to get component located in a specific gridx, gridy from a JPanel with GridBagLayout layout?

查看:120
本文介绍了如何从具有GridBagLayout布局的JPanel获取位于特定gridx中的组件,gridy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有gridbaglayout布局的jpanel,其中有几个jtextfields,几个jlabels,几个动态添加的jbuttons。因此我无法知道他们的具体命令,因此无法使用panel.getComponent(count)。如果有像getGridx(int x)或getGridy(int y)这样的东西,我查看了api。没找到。有什么类似的方法吗?

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?

推荐答案

最简单的解决方案可能是使用 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;
    }
}

这篇关于如何从具有GridBagLayout布局的JPanel获取位于特定gridx中的组件,gridy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆