Java JLabel 位于另一个 JLabel 之上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1513178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JLabel on top of another JLabel
提问by annaoj
Is it possible to add a JLabel on top of another JLabel? Thanks.
是否可以在另一个 JLabel 之上添加 JLabel?谢谢。
采纳答案by coobird
The short answer is yes, as a JLabel
is a Container
, so it can accept a Component
(a JLabel
is a subclass of Component
) to add into the JLabel
by using the add
method:
简短的回答是肯定的,因为 aJLabel
是 a Container
,因此它可以接受 a Component
(aJLabel
是 的子类Component
)以JLabel
使用以下add
方法添加到 中:
JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);
In the above code, the insideLabel
is added to the outsideLabel
.
在上面的代码中,insideLabel
被添加到outsideLabel
.
However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.
但是,在视觉上,会显示带有文本“Hello”的标签,因此无法真正看到标签中包含的标签。
So, the question comes down what one really wants to accomplish by adding a label on top of another label.
因此,问题归结为通过在另一个标签之上添加标签真正想要实现的目标。
Edit:
编辑:
From the comments:
来自评论:
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..:|
好吧,我想做的是首先从文件中读取某个分数,然后在 jlabel 中显示该分数。我想到的是将分数分成 3 部分,然后为三部分中的每一个使用一个标签。其次,我希望能够拖动分数,所以我想我可以使用另一个 jlabel,并将 3'mini jlabels' 放在大 jlabel 上。我不知道这是否会奏效..:|
It sounds like one should look into how to use layout managers in Java.
听起来应该研究如何在 Java 中使用布局管理器。
A good place to start would be Using Layout Managersand A Visual Guide to Layout Managers, both from The Java Tutorials.
一个很好的起点是使用布局管理器和布局管理器的可视化指南,两者都来自Java 教程。
It sounds like a GridLayout
could be one option to accomplish the task.
听起来GridLayout
可能是完成任务的一种选择。
JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
In the above example, the JPanel
is made to use a GridLayout
as the layout manager, and is told to make a row of JLabel
s.
在上面的例子中,JPanel
使用 aGridLayout
作为布局管理器,并被告知制作一行JLabel
s。
回答by Omry Yadan
it's a matter of layout. you can do that using null layout (with hard coded locations) or with a custom layout.
这是布局问题。您可以使用空布局(使用硬编码位置)或自定义布局来做到这一点。
回答by camickr
The answer to your original question is yes for the reasons given that any Component can be added to a Container.
考虑到任何组件都可以添加到容器中,您最初问题的答案是肯定的。
The reason you don't see the second label is because by default a JLabel uses a null layout manager and the size of the second label is (0, 0) so there is nothing to paint. So all you need to do is set the bounds of the second label and away you go.
您没有看到第二个标签的原因是因为默认情况下 JLabel 使用空布局管理器并且第二个标签的大小是 (0, 0) 所以没有什么可绘制的。所以你需要做的就是设置第二个标签的边界,然后就可以了。
You can't use a layout manager if you want to drag components around because as soon as you resize the frame etc, the layout manager will be invoked and the components will be repositioned based on the layout manager of the component.
如果您想拖动组件,则不能使用布局管理器,因为一旦调整框架大小等,布局管理器将被调用,并且组件将根据组件的布局管理器重新定位。
回答by Saher Ahwal
you can use a JLayeredPane
and set it's border to No Border.
您可以使用 aJLayeredPane
并将其边框设置为无边框。
回答by Ahd Radwan
you can add put them above each others by using the horizontal or vertical gap (hgap,vgap) the attributes of the layoutJPanel p = new JPanel(new GridLayout(2, 1,-40,0));
//the 40 is the hgap , make it the same with the label height .
您可以通过使用水平或垂直间隙(hgap,vgap)布局的属性来添加将它们放在彼此之上JPanel p = new JPanel(new GridLayout(2, 1,-40,0));
//the 40 is the hgap , make it the same with the label height .