java awt_Java AWT标签

java awt

Label class is used to create labels that are generally limited to single-line messages (names, short notes, etc.). It is used to identify components. In GUI we need labels to identify the use of other components. For example, we need to specify the label for a button to indicate what action it performs on being clicked.

Label类用于创建通常仅限于单行消息(名称,简短说明等)的标签。 它用于识别组件。 在GUI中,我们需要标签来标识其他组件的使用。 例如,我们需要为按钮指定标签,以指示按钮被单击后执行的操作。

Labels do not support any interaction with the user and do not fire any events. So, there is no listener class for the labels. Users cannot change the text of a label. We can create a label by creating an instance of Label class.

标签不支持与用户的任何交互,也不触发任何事件。 因此,标签没有侦听器类。 用户无法更改标签的文本。 我们可以通过创建Label类的实例来创建标签。

Consider the following code -

考虑以下代码-

import java.awt.*;
import javax.swing.*;

public class CreateLabel extends Frame{

     CreateLabel()
     {
        Label l1 = new Label("Label 1");
        Label l2 = new Label("Label 2", Label.CENTER);
         
         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
         setVisible(true);
         setSize(300,300);
         
         System.out.println(l1.getAlignment());
         System.out.println(l2.getAlignment());
         
         add(l1);
         add(l2);
     
     }
                  
        public static void main(String []args){
        CreateLabel ob = new CreateLabel();
        
     }
}

Output

输出量

Java AWT Label

As seen in the code, we have created 2 Label objects, initialized in different ways. "l1" has been initialized with just a text. We can also create a blank label by not passing any parameter at the time of object creation. In "l2", we have passed a string as well as set the alignment of the label.

如代码所示,我们创建了2个Label对象,它们以不同的方式初始化。 “ l1”仅用文本进行了初始化。 我们还可以通过在创建对象时不传递任何参数来创建空白标签。 在“ l2”中 ,我们传递了一个字符串并设置了标签的对齐方式。

There are three types of alignment – left (left-justified), center (center justified), and right (right-justified). L1 is left justified, which is the default case. L2 has been centrally justified.

共有三种对齐方式-左(左对齐),中心(中心对齐)和右(右对齐)。 L1左对齐,这是默认情况。 L2已集中说明理由。

We have set the layout of the frame as BoxLayout (aligned along the y-axis). This places all the components one below the other. We will learn more about layouts in later sections.

我们已将框架的布局设置为BoxLayout(沿y轴对齐)。 这会将所有组件都放置在另一个组件的下方。 我们将在后面的部分中详细了解布局。

The getAlignment() function is used to retrieve the present alignment of the label. As can be seen, 0 indicates that the label is left justified, 1 indicates center justified, and 2 indicates the right-justified.

getAlignment()函数用于检索标签的当前对齐方式。 可以看出,0表示标签左对齐,1表示中心对齐,而2表示右对齐。

We can also change the alignment of the label using the setAlignment(int) method of the Label class.

我们还可以使用Label类setAlignment(int)方法更改标签的对齐方式。

    void setAlignment(int alignment);

Just as in TextField and Button class, we have setText() and getText() methods to set and get the text of the label respectively.

就像TextFieldButton类一样,我们有setText()getText()方法来分别设置和获取标签的文本。

    String getText();
    void setText(String text)


翻译自: https://www.includehelp.com/java/awt-label.aspx

java awt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值