AWT Choice類

介紹

選擇用於控制顯示彈出菜單選擇。所選選項將顯示在頂部的菜單。

類的聲明

以下是聲明的java.awt.Choice類​​:

public class Choice extends Component implements ItemSelectable, Accessible

類的構造函數

S.N.

構造函數& 描述

1

Choice() ()
Creates a new choice menu.

類方法

S.N.

方法 & 描述

1

void add(String item)
Adds an item to this Choice menu.

2

void addItem(String item)
Obsolete as of Java 2 platform v1.1.

3

void addItemListener(ItemListener l)
Adds the specified item listener to receive item events from this Choice menu.

4

void addNotify()
Creates the Choice's peer.

5

int countItems()
Deprecated. As of JDK version 1.1, replaced by getItemCount().

6

AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this Choice.

7

String getItem(int index)
Gets the string at the specified index in this Choice menu.

8

int getItemCount()
Returns the number of items in this Choice menu.

9

ItemListener[] getItemListeners()
Returns an array of all the item listeners registered on this choice.

10

T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this Choice.

11

int getSelectedIndex()
Returns the index of the currently selected item.

12

String getSelectedItem()
Gets a representation of the current choice as a string.

13

Object[] getSelectedObjects()
Returns an array (length 1) containing the currently selected item.

14

void insert(String item, int index)
Inserts the item into this choice at the specified position.

15

protected String paramString()
Returns a string representing the state of this Choice menu.

16

protected void processEvent(AWTEvent e)
Processes events on this choice.

17

protected void processItemEvent(ItemEvent e)
Processes item events occurring on this Choice menu by dispatching them to any registered ItemListener objects.

18

void remove(int position)
Removes an item from the choice menu at the specified position.

19

void remove(String item)
Removes the first occurrence of item from the Choice menu.

20

void removeAll()
Removes all items from the choice menu.

21

void removeItemListener(ItemListener l)
Removes the specified item listener so that it no longer receives item events from this Choice menu.

22

void select(int pos)
Sets the selected item in this Choice menu to be the item at the specified position.

23

void select(String str)
Sets the selected item in this Choice menu to be the item whose name is equal to the specified string.

繼承的方法

這個類繼承的方法從以下類:

  • java.awt.Component

  • java.lang.Object

Choice 實例

選擇使用任何編輯器創建以下java程序 D:/ > AWT > com > yiibai > gui >

AwtControlDemo.java

package com.yiibai.gui; import java.awt.*; import java.awt.event.*; public class AwtControlDemo { private Frame mainFrame; private Label headerLabel; private Label statusLabel; private Panel controlPanel; public AwtControlDemo(){ prepareGUI(); } public static void main(String[] args){ AwtControlDemo awtControlDemo = new AwtControlDemo(); awtControlDemo.showChoiceDemo(); } private void prepareGUI(){ mainFrame = new Frame("Java AWT Examples"); mainFrame.setSize(400,400); mainFrame.setLayout(new GridLayout(3, 1)); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerLabel = new Label(); headerLabel.setAlignment(Label.CENTER); statusLabel = new Label(); statusLabel.setAlignment(Label.CENTER); statusLabel.setSize(350,100); controlPanel = new Panel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void showChoiceDemo(){ headerLabel.setText("Control in action: Choice"); final Choice fruitChoice = new Choice(); fruitChoice.add("Apple"); fruitChoice.add("Grapes"); fruitChoice.add("Mango"); fruitChoice.add("Peer"); Button showButton = new Button("Show"); showButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String data = "Fruit Selected: " + fruitChoice.getItem(fruitChoice.getSelectedIndex()); statusLabel.setText(data); } }); controlPanel.add(fruitChoice); controlPanel.add(showButton); mainFrame.setVisible(true); } }

編譯程序,使用命令提示符。到 D:/ > AWT 然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtControlDemo.java

如果沒有錯誤出現,這意味着編譯成功。使用下面的命令來運行程序。

D:AWT>java com.yiibai.gui.AwtControlDemo

驗證下面的輸出

AWT