Java 中组件的重用没有正确修剪未重用的组件
我有一个名为 calcResPanel
的 javax.swing.JPanel (使用带有 1
列的 java.awt.GridLayout
和不定 (0
) 行),用于接收和显示一组 BHSelectableLabel
(扩展 javax.swing.JTextField
),共同表示这存储在称为结果
的String
列表中的文本。我想我不妨给它以下行为:
- 第一次,它只会添加新的行为
- 接下来的时间,它会:
- 将已添加的尽可能多的标签的文本更改为
结果
中尽可能多的值的文本 - 如果还有任何未更改的标签,请将其删除,因为它们不是必需的。否则,根据需要添加任意数量的新标签。
- 将已添加的尽可能多的标签的文本更改为
这对我来说很有意义。 如果这个算法不是我应该做的,那么现在停止阅读并用更好的算法发布答案。但是,如果您同意,请告诉我我的代码做错了什么:
int i, r, l;
for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
if (i < l)
((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
else
calcResPanel.add(new BHSelectableLabel(results.get(i)));
for (;i < l; i++)//If there are excess, unused lables, remove them
calcResPanel.remove(i);
此代码的问题在于它不一致地在 calcResPane 中留下多余的标签。如果您认为这个算法在概念上很好,那么请告诉我我的代码有什么问题导致它留下多余的标签?
回答
也是一个如此简单的答案。我觉得自己很聪明^^;
int i, r, l;
for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
if (i < l)
((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
else
calcResPanel.add(new BHSelectableLabel(results.get(i)));
for (;i < l; i++)//If there are excess, unused lables, remove them
calcResPanel.remove(r);
I have a javax.swing.JPanel
called calcResPanel
(using a java.awt.GridLayout
with 1
column and indefinite (0
) rows) which is to receive and display a set of BHSelectableLabel
s (which extend javax.swing.JTextField
) with collectively represent the text stored in the list of String
s called results
. I figured that I might as well give it the following behavior:
- The first time, it will only add new ones
- Following times, it will:
- Change the text of as many labels that are already added as possible to be that of as many of the values in
results
as possible - If there are any labels left that haven't been changed, remove those, as they are not necessary. Else, add as many new labels as needed.
- Change the text of as many labels that are already added as possible to be that of as many of the values in
This makes sense to me. If this algorithm is not what I should be doing, then stop reading now and post an answer with a better algorithm. However, if you agree, then tell me what I've done wrong with my code:
int i, r, l;
for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
if (i < l)
((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
else
calcResPanel.add(new BHSelectableLabel(results.get(i)));
for (;i < l; i++)//If there are excess, unused lables, remove them
calcResPanel.remove(i);
The problem with this code is that it inconsistently leaves excess labels in calcResPane
. If you think this algorithm is good in concept, then please tell me what is wrong with my code that makes it leave excess labels?
Answer
Such a simple answer, too. I feel SO smart ^^;
int i, r, l;
for (i=0, r = results.length(), l = calcResPanel.getComponentCount(); i < r; i++)
if (i < l)
((BHSelectableLabel)calcResPanel.getComponent(i)).setText(results.get(i));
else
calcResPanel.add(new BHSelectableLabel(results.get(i)));
for (;i < l; i++)//If there are excess, unused lables, remove them
calcResPanel.remove(r);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你永远不能像这样删除,因为你会跳过每第二个项目。假设您有 5 个项目,您尝试将它们全部删除:
第一次通过循环 i = 0,因此您删除了项目 0,只剩下 1、2、3、4。
下次通过循环 i = 1 ,所以你删除了第 2 项,剩下了 1, 3, 4。
我希望你能明白这个模式。
解决方案是从末尾移除项目,一次一个。
You can never do a remove like that because you skip every 2nd item. Lets say you have 5 items and you try to delete them all:
The first time through the loop i = 0, so you remove item 0 and you are left with 1, 2, 3, 4.
Next time throught the loop i = 1, so you remove item 2 and you are left with 1, 3, 4.
I hope you get the pattern.
The solution is to remove items from the end, one at a time.