Java 中组件的重用没有正确修剪未重用的组件

发布于 2024-12-09 14:56:05 字数 1510 浏览 0 评论 0原文

我有一个名为 calcResPanel 的 javax.swing.JPanel (使用带有 1 列的 java.awt.GridLayout 和不定 (0) 行),用于接收和显示一组 BHSelectableLabel(扩展 javax.swing.JTextField),共同表示这存储在称为结果String列表中的文本。我想我不妨给它以下行为:

  1. 第一次,它只会添加新的行为
  2. 接下来的时间,它会:
    1. 将已添加的尽可能多的标签的文本更改为结果中尽可能多的值的文本
    2. 如果还有任何未更改的标签,请将其删除,因为它们不是必需的。否则,根据需要添加任意数量的新标签。

这对我来说很有意义。 如果这个算法不是我应该做的,那么现在停止阅读并用更好的算法发布答案。但是,如果您同意,请告诉我我的代码做错了什么:

  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 BHSelectableLabels (which extend javax.swing.JTextField) with collectively represent the text stored in the list of Strings called results. I figured that I might as well give it the following behavior:

  1. The first time, it will only add new ones
  2. Following times, it will:
    1. 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
    2. 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.

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-12-16 14:56:05
for (;i < l; i++)//If there are excess, unused lables, remove them     
    calcResPanel.remove(i); 

你永远不能像这样删除,因为你会跳过每第二个项目。假设您有 5 个项目,您尝试将它们全部删除:

第一次通过循环 i = 0,因此您删除了项目 0,只剩下 1、2、3、4。

下次通过循环 i = 1 ,所以你删除了第 2 项,剩下了 1, 3, 4。

我希望你能明白这个模式。

解决方案是从末尾移除项目,一次一个。

for (;i < l; i++)//If there are excess, unused lables, remove them     
    calcResPanel.remove(i); 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文