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

问题描述

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

解决方案

简短的回答是肯定的,作为是一个,因此它可以接受( JLabel 组件的子类)使用方法:

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

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



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



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




修改:



来自评论:

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



一个好的起点是和,均来自。



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

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

在上面的例子中,使用 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:

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之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 16:15