如何循环 JLabel 中元素的文本? [英] How to loop text of an element in JLabel?

查看:53
本文介绍了如何循环 JLabel 中元素的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了循环问题.这个怎么循环,遍历不同的值并在标签上显示所有值,而不是在标签上显示数组的最后一个值?

I'm having a problem with loops. How does this loop, loop through different values ​​and display all values ​​on the label, rather than displaying the last value of the array on the label?

循环代码图像.

推荐答案

下面的代码只是从您的屏幕截图中复制(编写)的.其中有一个小错误.

This below code is just copied(written) from your screenshot. which has the minor bug.

sinhvien sv = new sinhvien();
sv.setdata("CC",12);
  sv.setdata("CL",14);
   sv.setdata("CCCL",16);

     s1.add(sv);

因为您只创建了一个 sv 实例并设置了 3 次值.值 CCCL 覆盖所有其他两个以前的值.

As you have only created one instance of sv and setting the value 3 times. Value CCCL override all other two previous values.

sv.setdata("CCCL",16);

所以,在行

s1.add(sv);

您实际上只是将 sinhvien 的一个实例添加到数组列表中.

you are actually adding only one instance of sinhvien into the array list.

调试:检查数组列表大小,这会给你一些线索,为什么你会得到这种行为.在循环后使用以下代码.

Debugging: Check the array list size that will give you some clue why you are getting this behaviour. Use the below code after the loop.

//Code to get ArrayList size
System.out.println(sv1.size());

每当向 ArrayList 添加项目时,请确保每个项目都有一个新的 sinhvien 实例.

Whenever adding items into ArrayList make sure each item has a new instance of the sinhvien.

请试试下面的代码,

sinhvien sv = new sinhvien();
sv.setdata("CC",12);
sv1.add(sv);

sv = new sinhvien();
sv.setdata("CL",14);
sv1.add(sv);

sv = new sinhvien();
sv.setdata("CCCL",16);
sv1.add(sv);

注意:在 jButton1ActionPerormed 方法内部和 for 循环之前替换上面的代码.这是无处循环问题.分配问题.

Note: Replace above code inside the jButton1ActionPerormed method and before the for a loop. This is nowhere loop issue. It is assignment issue.

这篇关于如何循环 JLabel 中元素的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆