在使用GridBagLayout的JDialog上的特定位置查找组件

时间:2017-09-19 11:58:05

标签: java gridbaglayout jdialog

我有一个JDialog,其中包含使用GridBagLayout动态创建的组件行。我使用在此论坛上找到的方法来定位组件。

 public static GridBagConstraints makeGbc(int x, int y) {
      GridBagConstraints gbc = new GridBagConstraints();
      Insets WEST_INSETS=new Insets(5,0,5,5);
      Insets EAST_INSETS=new Insets(5,5,5,0);
      gbc.gridx = x;
      gbc.gridy = y;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;

      gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
      gbc.fill = (x == 0) ? GridBagConstraints.BOTH
            : GridBagConstraints.HORIZONTAL;

      gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
      gbc.weightx = (x == 0) ? 0.1 : 1.0;
      gbc.weighty = 1.0;
      return gbc;
   }

这样可以轻松地为每个组件指定行和列,并且它可以很好地工作。

我现在想编写一个方法,它将返回相应行和列中存在的任何组件。这是可能的,如果是这样,我将如何实现它? TIA。

这不是重复,因为我知道我想检查的位置,但我不知道那里有什么组件。我需要传递x,y坐标并返回组件。我知道我必须建立一种方法,但我不能看到如何做到这一点。

0 个答案:

没有答案
相关问题