简体   繁体   English

使用Grix,gridy删除JTextField

[英]Using Grix, gridy to remove JTextField

I want to know if its possible to remove a JTextField using GridBagLayout and removing like this. 我想知道是否有可能使用GridBagLayout删除JTextField并像这样删除。

PANEL.remove(gridx.5, gridy.5) ---< is this possible? PANEL.remove(gridx.5, gridy.5) --- <这可能吗?

Or how can I remove with this sort of logic. 或如何删除这种逻辑。

No there is no method that does that, you will need to write your own. 没有没有可以做到这一点的方法,您将需要编写自己的方法。

  1. The Container class has a method getLayout() . Container类具有方法getLayout() This will allow you to get the GridBagLayout instance for your panel. 这将允许您获取面板的GridBagLayout实例。
  2. The Container class also has a method getComponents() Container类也具有方法getComponents()

So once you get all the components in an array you iterate through the array. 因此,一旦获得了数组中的所有组件,便会遍历该数组。 For each component you would: 对于每个组件,您将:

  1. Use the getConstraints() method of the GridBagLayout . 使用GridBagLayoutgetConstraints()方法。
  2. Then check the gridx and gridy value of the GridBagConstraints object to see what clumn/row the component is in. 然后检查GridBagConstraints对象的gridxgridy值,以查看组件所在的行/行。
  3. Remove the component from the panel if it meets your criteria. 如果符合条件,则从面板上卸下该组件。

After the loop is finished you invoke revalidate() on the panel. 循环完成后,您可以在面板上调用revalidate()

Edit: 编辑:

Somewhere in your code you need to set the layout manager to the GridBagLayout. 您需要在代码中的某个位置将布局管理器设置为GridBagLayout。 Then you add components to the panel using your GridBagConstraints. 然后,使用GridBagConstraints将组件添加到面板中。

Then in the future when you want to remove the component from the panel you need to reference: 之后,当您想从面板中删除组件时,需要参考以下内容:

  1. the JPanel you added the components to 您将组件添加到的JPanel
  2. the GridBagLayout of the above panel 上面面板的GridBagLayout

I gave you the methods you need to accomplish this. 我为您提供了完成此操作所需的方法。 So did you read the API for the methods I suggest you need to use? 那么您是否阅读了我建议您使用的方法的API?

The basic code would be: 基本代码为:

GridBagLayout layout = (GridBagLayout)panel;
Component[] components = panel.getComponent();

for (each component in the array)
{
    GridBagConstraint gbc = layout.getConstraints( component )

    if (gbc.gridX == ?? && gbc.gridY = ??)
    {
        // remove the component from the panel
    }
}

panel.revalidate();

So I would suggest you create a method that passes in the row and column of the component you want to find. 因此,我建议您创建一种方法来传递要查找的组件的行和列。 Then you can change the if condition to access these parameters. 然后,您可以更改if条件来访问这些参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM