java awt 选择框_Java AWT选择

news/2024/7/19 13:05:55 标签: java, python, js, 项目管理, c#

java awt 选择框

The Choice class provides a pop-up menu to the user. The user is capable of choosing one of the options from the list. The selected item appears at the top. This class is often used when there is a large number of options to choose from and not a lot of space available to display all the items on the screen.

Choice类向用户提供一个弹出菜单。 用户能够从列表中选择选项之一。 所选项目显示在顶部。 当有很多选项可供选择并且没有足够的空间来显示屏幕上的所有项目时,通常使用此类。

We can program to interact with the Choice object at runtime, by implementing the ItemListener interface. The class is capable of firing the ItemEvent objects, which we can handle by assigning an ItemListener. We will discuss more event handling in later sections.

通过实现ItemListener接口,我们可以编程在运行时与Choice对象进行交互。 该类能够触发ItemEvent对象,我们可以通过分配ItemListener来处理该对象。 我们将在后面的部分中讨论更多的事件处理。

Consider the following code -

考虑以下代码-

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

public class CreateChoice extends Frame {
    CreateChoice() {
        Choice c1 = new Choice();
        Choice c2 = new Choice();

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

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

        System.out.println(c2.getItemCount());
        System.out.println(c2.getSelectedItem());

        c1.setBounds(50, 50, 100, 30);
        c2.setBounds(50, 100, 100, 30);

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

        add(c1);
        add(c2);
    }


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

Output

输出量

java/Images/awt-choice-1.jpg" alt="Java AWT Choice" style="outline: none;" />




java/Images/awt-choice-2.jpg" alt="Java AWT Choice" style="outline: none;" />

As can be seen from the output, we obtain two pop up menus, each providing us with a list, with items to choose from. Initially, we have created two empty Choice objects, c1 and c2. The add() method lets us add String objects to c1 and c2.

从输出中可以看出,我们获得了两个弹出菜单,每个菜单都为我们提供了一个列表,以及可供选择的项目。 最初,我们创建了两个空的Choice对象c1c2add()方法使我们可以将String对象添加到c1c2

String objects are displayed in the list in the same order as added. The Choice class maintains an index value, using which String objects are added. This means that we can access any item on such a list directly from its index.

字符串对象以与添加时相同的顺序显示在列表中。 Choice类维护一个索引值,使用该值添加String对象。 这意味着我们可以直接从其索引访问此列表中的任何项目。

Whatever item the user clicks on, gets selected and is displayed at the top, as can be seen – Apple and C++ are displayed in the two Choice objects.

可以看到,无论用户单击什么项目,都将其选中并显示在顶部,其中AppleC ++会显示在两个Choice对象中。

Often we require to check the total number of items in a Choice object. For this, we have the getItemCount() method. It returns an integer value, representing the total count of the items. As printed on the screen, there are 5 objects in c2.

通常,我们需要检查Choice对象中的项目总数。 为此,我们有getItemCount()方法。 它返回一个整数值,代表项目的总数。 在屏幕上打印时, c2中有5个对象。

    int getItemCount()

We can also retrieve which item is presently selected by the user, using the getSelectedItem() method. It returns a String value, of the object that is selected by the user.

我们还可以使用getSelectedItem()方法检索用户当前选择的项目。 它返回用户选择的对象的字符串值。

    String getSelectedItem()


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

java awt 选择框


http://www.niftyadmin.cn/n/1256464.html

相关文章

Java实现中国式排名_厉害了,原来Excel实现中国式排名可以有这样两种方法

今天给大家分享的是实现中式排名的2种方法。首先我们要了解下什么是中式排名?中式排名即当我们的成绩排名出现并列的时候,比如并列第2,然后再后面的会是第3名,不会跳名次显示。而我们在实际使用Excel中的RANK函数计算排名的时候&a…

ORACLE锁表解表语句

查询锁表语句: select object_name, machine, s.sid, s.serial# from gv$locked_object l, dba_objects o, gv$session s where l.object_id o.object_id and l.session_id s.sid; 解锁语句: alter system kill session sid,serial#; 转载于:https://…

toradians qt_Java Math类静态double toRadians(double angle_in_degrees)与示例

toradians qt数学类静态double toRadians(double angle_in_degrees) (Math Class static double toRadians(double angle_in_degrees) ) This method is available in java.lang package. 此方法在java.lang包中可用。 This method is used to convert the angle from degrees …

12.26

我只是不想取悦任何人。 就这样吧。 转载于:https://www.cnblogs.com/dandansang/p/8111828.html

dedecms php_dedecms{dede:php}标签用法介绍,dedecmsdede_PHP教程

dedecms {dede:php}标签用法介绍,dedecmsdede最简单的输入如代码如下{dede:php}$numA 1;$numB 2;echo $numA $numB;{/dede:php}从上面语句可以看出dede:php标签可以名符其实的让在php中一样的用法,上面语句在php写法如下代码如下$numA1;$numB2;echo $…

Git 学习一

刚刚接触git,学习现骨干操作并记录一下过程中的小问题(Windows下) 1、新建git目录 创建一个目录,使用命令 git init 2、添加文件 git add a.txt 3、提交文件 git commit - m “commit a.txt” 这时有时候会出现问题 这时使用提…

c语言 函数的参数传递示例_C-用户定义的函数示例,带有参数,没有返回类型...

c语言 函数的参数传递示例Define a function with arguments and no return type in C language. 用C语言定义带有参数且没有返回类型的函数。 Here, we are function fun1 which has two arguments int and char*, while calling the function, we are passing two values, i…

php实现微信公众号分享,php实现微信公众号无限群发

利用微信客服接口进行各类消息的无限群发sendAllMsg.php&#xfeff;<?php /*Author:yf使用说明:微信公众号无限群发接口&#xff0c;使用实例:$test new SendAllMsg("你的appId","你的appSecret");$test->sendMsgToAll(); //调用群发方法注&#…