JLabel在另一个JLabel之上 [英] JLabel on top of another JLabel

查看:120
本文介绍了JLabel在另一个JLabel之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在另一个JLabel之上添加JLabel?谢谢。

解决方案

简短的回答是肯定的,作为 JLabel 是一个 Container ,因此它可以接受 组件 JLabel 组件的子类)使用 JLabel 中.html #add(java.awt.Component)rel =noreferrer> 添加 方法:

  JLabel outsideLabel = new JLabel(Hello); 
JLabel insideLabel = new JLabel(World);
outsideLabel.add(insideLabel);

在上面的代码中, insideLabel 是添加到 outsideLabel



然而,在视觉上,显示带有Hello文本的标签,因此无法真正看到标签中包含的标签。



因此,问题在于通过在另一个标签上添加标签来实现真正想要实现的目标。 / p>




修改:



来自评论:


好吧,我想做的是先,
从文件中读取一定比例,
然后在
jlabel中显示该分数。我想到的是
将分数分成3部分,然后
为这三部分中的每一部分使用一个标签。
然后第二,我希望能够拖动
的分数,所以我想我可以使用
另一个jlabel,并将3'mini
jlabels'放在大jlabel上。我不知道
知道这是否会起作用..:


这听起来应该如何调查如何在Java中使用布局管理器。



一个好的起点是使用布局管理器布局管理器的可视指南,均来自 Java教程



听起来像是一个 GridLayout 可以是完成任务的一个选项。

  JPanel p = new JPanel(new GridLayout(0,1)); 
p.add(new JLabel(One));
p.add(new JLabel(Two));
p.add(新JLabel(三));

在上面的例子中, JPanel 使用 GridLayout 作为布局管理器,并被告知要生成一行 JLabel s。


Is it possible to add a JLabel on top of another JLabel? Thanks.

解决方案

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:

JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);

In the above code, the insideLabel is added to the outsideLabel.

However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.

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..:|

It sounds like one should look into how to use layout managers in Java.

A good place to start would be Using Layout Managers and A Visual Guide to Layout Managers, both from The Java Tutorials.

It sounds like a GridLayout could be one option to accomplish the task.

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 JLabels.

这篇关于JLabel在另一个JLabel之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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