设置的JLabel的文本使用数组/循环 [英] Setting text of JLabel with an array/loop

查看:626
本文介绍了设置的JLabel的文本使用数组/循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能设置一个JLabel的文本与循环?例如:

How can I set the text of a JLabel with a loop? For example:

String cur[]= {"A","B","C"};
JLabel lblA,lblB,lblC;

for(i=0;i < cur.length;i++){
  lbl+cur[i].setText("something");
}

我应该在LBL + CUR [I]的一部分去那么它设置的JLabel的文字?

what should go in the "lbl+cur[i]" part so it sets the text of the JLabels?

感谢

推荐答案

您不能动态地创建变量名这样的。

You can't dynamically create variable names like that.

如果您想设置一个标签的价值在一个循环中,那么你需要创建的JLabel数组创建一个字符串数组是一样的。

If you want to set the value of a label in a loop then you need to create an array of JLabels the same way you create an array of Strings.

JLabel[] labels = new JLabel[cur.length];

for (int i = 0 i < cur.length; i++)
{
    labels[i] = new JLabel( cur[i] );
}

这篇关于设置的JLabel的文本使用数组/循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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