📜  Java AWT |选择班

📅  最后修改于: 2022-05-13 01:54:53.985000             🧑  作者: Mango

Java AWT |选择班

Choice 类是Java Abstract Window Toolkit(AWT) 的一部分。 Choice 类为用户呈现一个弹出菜单,用户可以从弹出菜单中选择一个项目。所选项目出现在顶部。 Choice 类继承了 Component。

Choice 类的构造函数

  • Choice() :创建一个新的空选择菜单。

选择类的不同方法

MethodExplanation
add(String s)adds an item to this Choice menu.
addItemListener(ItemListener l)adds the specified item listener to receive item events from this Choice menu.
addNotify()creates the Choice’s peer.
getAccessibleContext()gets the AccessibleContext associated with this Choice.
getItem(int i)gets the string at the specified index in this Choice menu
getItemCount()returns the number of items in this Choice menu.
getItemListeners()returns an array of all the item listeners registered on this choice.
getListeners(Class l)returns an array of all the objects currently registered as FooListeners upon this Choice.
getSelectedIndex()returns the index of the currently selected item.
getSelectedItem()gets a representation of the current choice as a string.
insert(String s, int i)inserts the item into this choice at the specified position.
paramString()returns a string representing the state of this Choice menu.
processEvent(AWTEvent e)processes events on this choice.
processItemEvent(ItemEvent e)processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.
remove(int p)removes an item from the choice menu at the specified position.
remove(String s)removes the first occurrence of item from the Choice menu.
removeAll()removes all items from the choice menu.
select(int p)sets the selected item in this Choice menu to be the item at the specified position.

下面的程序说明了Java AWT 中的 Choice 类:

  • 程序创建一个简单的选择并向其中添加元素
    // Java Program to create a simple 
    // choice and add elements to it .
    import java.awt.*;
    import javax.swing.*;
      
    class choice {
      
        // choice
        static Choice c;
      
        // frame
        static JFrame f;
      
        // default constructor
        choice()
        {
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // create a frame
            f = new JFrame("choice");
      
            // create e panel
            JPanel p = new JPanel();
      
            // create a choice
            c = new Choice();
      
            // add element to the list
            c.add("Andrew");
            c.add("Arnab");
            c.add("Ankit");
      
            // add choice to panel
            p.add(c);
      
            // add panel to the frame
            f.add(p);
      
            // show the frame
            f.show();
            f.setSize(300, 300);
        }
    }
    

    输出 :

  • 程序创建一个简单的选择并将 ItemListener 添加到它
    // Java  Program to create a simple
    // choice and add ItemListener to it
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
      
    class choice implements ItemListener {
      
        // choice
        static Choice c;
      
        // frame
        static JFrame f;
      
        // label
        static Label l;
      
        // default constructor
        choice()
        {
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // create a frame
            f = new JFrame("choice");
      
            // object
            choice ch = new choice();
      
            // create e panel
            JPanel p = new JPanel();
      
            // create a choice
            c = new Choice();
      
            // add element to the list
            c.add("Andrew");
            c.add("Arnab");
            c.add("Ankit");
      
            // add itemListener to it
            c.addItemListener(ch);
      
            // create a label
            l = new Label();
      
            // set the label text
            l.setText(c.getSelectedItem() + " selected");
      
            // add choice to panel
            p.add(c);
            p.add(l);
      
            // add panel to the frame
            f.add(p);
      
            // show the frame
            f.show();
            f.setSize(300, 300);
        }
      
        // if an item is selected
        public void itemStateChanged(ItemEvent e)
        {
            l.setText(c.getSelectedItem() + " selected");
        }
    }
    

    输出 :

  • 程序创建一个选项并手动向其中添加元素(使用 add(String s)函数)

    // Java Program to create a choice and
    // manually add elements to it 
    // (using add(String s) function)
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
      
    class choice implements ItemListener, ActionListener {
      
        // choice
        static Choice c;
      
        // frame
        static JFrame f;
      
        // label
        static Label l;
      
        // textfield
        static TextField tf;
      
        // default constructor
        choice()
        {
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // create a frame
            f = new JFrame("choice");
      
            // object
            choice ch = new choice();
      
            // create e panel
            JPanel p = new JPanel();
      
            // create a choice
            c = new Choice();
      
            // add element to the list
            c.add("Andrew");
      
            // create a textfield
            tf = new TextField(7);
      
            // create a button
            Button b = new Button("ok");
      
            // add actionListener
            b.addActionListener(ch);
      
            // add itemListener to it
            c.addItemListener(ch);
      
            // create a label
            l = new Label();
            Label l1 = new Label("add names");
      
            // set the label text
            l.setText(c.getSelectedItem() + " selected");
      
            // add choice to panel
            p.add(c);
            p.add(l);
            p.add(l1);
            p.add(tf);
            p.add(b);
      
            // add panel to the frame
            f.add(p);
      
            // show the frame
            f.show();
            f.setSize(250, 300);
        }
      
        // if an item is selected
        public void itemStateChanged(ItemEvent e)
        {
            l.setText(c.getSelectedItem() + " selected");
        }
      
        // if button is pressed
        public void actionPerformed(ActionEvent e)
        {
            // add item to the choice
            c.add(tf.getText());
        }
    }
    

    输出 :

注意:程序可能无法在在线 IDE 中运行,请使用离线 IDE。

参考: https: Java/awt/Choice.html