简短的回答是肯定的,因为
JLabel是
Container,所以它可以接受
Component(JLabel是Component的子类)通过使用
add方法添加到JLabel中:
JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);
在上面的代码中,insideLabel被添加到outsideLabel中.
但是,在视觉上,显示带有“Hello”文本的标签,因此无法真正看到标签中包含的标签.
因此,问题在于通过在另一个标签上添加标签来实现真正想要实现的目标.
编辑:
来自评论:
well, what i wanted to do was first,
read a certain fraction from a file,
then display that fraction in a
jlabel. what i thought of was to
divide the fraction into 3 parts, then
use a label for each of the three.
then second, i want to be able to drag
the fraction, so i thought i could use
another jlabel, and place the 3’mini
jlabels’ over the big jlabel. i don’t
know if this will work though..:|
听起来应该研究如何在Java中使用布局管理器.
听起来像GridLayout可能是完成任务的一种选择.
JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
在上面的示例中,JPanel使用GridLayout作为布局管理器,并被告知要生成一行JLabel.