Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以添加一个JLabel数组,使每个元素在JLabel中都有一个背景颜色_Java - Fatal编程技术网

Java 是否可以添加一个JLabel数组,使每个元素在JLabel中都有一个背景颜色

Java 是否可以添加一个JLabel数组,使每个元素在JLabel中都有一个背景颜色,java,Java,这是我的示例代码,我想将hpBar放在hpDetails中,这样当玩家受到伤害/治疗时,更改状态就很容易,也更有效。但是当我把它放在hpBar里面时,它并没有显示hpBar,它只是留下了空间。我真的需要知道是否有可能做到这一点 this.hpDetails=newjlabel(“”) this.hpDetails.setOpaque(true); this.hpBar=newjlabel[5]; 对于(int i=0;iNo.AJLabel不能有子JLabels(或者实际上是一个具有多个子数组)

这是我的示例代码,我想将hpBar放在hpDetails中,这样当玩家受到伤害/治疗时,更改状态就很容易,也更有效。但是当我把它放在hpBar里面时,它并没有显示hpBar,它只是留下了空间。我真的需要知道是否有可能做到这一点

this.hpDetails=newjlabel(“”)

this.hpDetails.setOpaque(true);
this.hpBar=newjlabel[5];

对于(int i=0;iNo.A
JLabel
不能有子
JLabel
s(或者实际上是一个具有多个子数组)。您需要查看一个布局管理器,它允许您在一行中逐个布局多个
JLabel
s

您通常会创建一个
JPanel
,给它一个
FlowLayout
(默认设置),并将所有
JLabel
添加到面板中。
另一种方法是使用“真实的”进度条而不是尝试DIY。

你是在问一个
JLabel
是否可以包含其他
JLabel
s?是的,但另一个JLabel是一个数组,每个元素都有一个背景色hpBar是一个JLabel数组,每个元素的背景色都是红色,我想把这个hpBar放在hpDetails里面,这是一个普通的jlabeloh我明白了,谢谢,谢谢,我想我真的别无选择,只能感谢所有的xD。谢谢你的回答!!
 this.hpDetails.setOpaque(true);

 this.hpBar=new JLabel[5];

 for(int i=0;i<5;i++){

     this.hpBar[i] = new JLabel("   ");

     this.hpBar[i].setOpaque(true);

     this.hpBar[i].setBackground(Color.RED);

     this.hpDetails.setText(hpDetails.getText() + this.hpBar[i]);

 } 
 this.add(this.hpDetails);