[英]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. 没有没有可以做到这一点的方法,您将需要编写自己的方法。
Container
class has a method getLayout()
. Container
类具有方法getLayout()
。 This will allow you to get the GridBagLayout
instance for your panel. GridBagLayout
实例。 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:
对于每个组件,您将:
getConstraints()
method of the GridBagLayout
. GridBagLayout
的getConstraints()
方法。 gridx
and gridy
value of the GridBagConstraints
object to see what clumn/row the component is in. GridBagConstraints
对象的gridx
和gridy
值,以查看组件所在的行/行。 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: 之后,当您想从面板中删除组件时,需要参考以下内容:
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.