Java AWT列表

The List is a GUI component used to display a list of text items. It contains a set of String values that the user can choose from. It is a 'list' that allows the user to select one or more options. The programmer has the choice to configure 'single select' or 'multiple select' option of a list.

列表是一个GUI组件,用于显示文本项列表。 它包含一组用户可以选择的字符串值。 这是一个“列表”,允许用户选择一个或多个选项。 程序员可以选择配置列表的“单选”或“多选”选项。

An object of List class generates an ItemEvent object when an item is selected from the list. This event can be handled by implementing the ItemListener interface. Any class implementing this interface can interact with the List object at runtime and handle selecting and unselecting of items from a list. We will study more about event handling in later sections.

从列表中选择一个项目时,List类的对象会生成一个ItemEvent对象。 可以通过实现ItemListener接口来处理此事件。 任何实现此接口的类都可以在运行时与List对象进行交互,并处理从列表中选择和取消选择项的操作。 我们将在后面的部分中详细研究事件处理。

Consider the following code -

考虑以下代码-

import java.awt.*;

public class CreateList {
    CreateList() {
        Frame f = new Frame();
        List l1 = new List(5, true);
        List l2 = new List(2);

        l1.add("Apple");
        l1.add("Banana");
        l1.add("Mango");

        l2.add("C++");
        l2.add("Java");
        l2.add("Ruby");
        l2.add("Javascript");
        l2.add("Python");

        System.out.println(l1.getItem(1));
        System.out.println(l2.getItemCount());
        l2.remove(3);
        System.out.println(l2.getItemCount());

        l1.setBounds(50, 50, 100, 60);
        l2.setBounds(50, 200, 100, 60);

        f.setLayout(null);
        f.setVisible(true);
        f.setSize(300, 300);

        f.add(l1);
        f.add(l2);
    }

    public static void main(String[] args) {
        CreateList ob = new CreateList();
    }
}

Output

输出量

Java AWT List




Java AWT List

We created two List objects l1 and l2. L1 has been given an initial size of 5 rows but has been populated with only 3 items. L2 has been given an initial size of 2 rows but has been populated with 5 items. The first parameter of the object constructor specifies the row size. The second parameter is a boolean value which specifies whether multiple options can be chosen by the user. The default value is false, meaning that the user can choose only one item from the list, as is the case with l2. If we set this parameter as true, the user will have the freedom to choose multiple items from the list, as can be seen in the case of l1.

我们创建了两个List对象l1l2L1的初始大小为5行,但仅填充了3行。 L2的初始大小为2行,但已填充了5个项目。 对象构造函数的第一个参数指定行大小。 第二个参数是一个布尔值,它指定用户是否可以选择多个选项。 默认值为false,这意味着用户只能从列表中选择一项,就像l2一样 。 如果将此参数设置为true,则用户将可以从列表中自由选择多个项目,如l1所示

We can add various text items to a list object using the add() method. A list can hold String values that are passed as a parameter during the call to the add() method. Items are displayed in the same order in which they are added to the list. Items have an index attached to them, using which we can access them directly.

我们可以使用add()方法将各种文本项添加到列表对象。 列表可以包含在调用add()方法期间作为参数传递的String值。 项目以添加到列表的顺序显示。 项目附有一个索引,我们可以使用它直接访问它们。

To access an item at a specified index, we use the getItem() method, in which we pass the index of the item to fetch. The getItem() returns a String value, of the item at that particular index.

要访问指定索引处的项目,我们使用getItem()方法,在该方法中,我们将项目的索引传递给fetch。 getItem()返回该字符串在特定索引处的值。

We can also retrieve the number of items added in a list by using the getItemCount() method. As can be seen, it returns an integer value, specifying the number of elements present in the list.

我们还可以使用getItemCount()方法检索列表中添加的项目数。 可以看出,它返回一个整数值,指定列表中存在的元素数。

If we want to remove a particular item from the list, we can use the remove() method. We can pass either the String value that is to be removed or the index of the element to be removed.

如果要从列表中删除特定项目,可以使用remove()方法。 我们可以传递要删除的String值或要删除的元素的索引。

As can be seen from the output of the code, initially we get 5 items present in l2. However, after using the remove() method, we get 4 elements remaining in the list.

从代码的输出可以看出,最初我们在l2中得到5个项目。 但是,在使用remove()方法之后,列表中还剩下4个元素。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值