- AWT 教程
- AWT - 首页
- AWT - 概述
- AWT - 环境
- AWT - 控件
- AWT - 事件处理
- AWT - 事件类
- AWT - 事件监听器
- AWT - 事件适配器
- AWT - 布局
- AWT - 容器
- AWT - 菜单
- AWT - 图形
- AWT 有用资源
- AWT - 快速指南
- AWT - 有用资源
- AWT - 讨论
AWT Choice 类
介绍
选择控件用于显示弹出式选择菜单。选定的选择显示在菜单顶部。
类声明
以下是java.awt.Choice类的声明
public class Choice
extends Component
implements ItemSelectable, Accessible
类构造函数
| 序号 | 构造函数及描述 |
|---|---|
| 1 | Choice() () 创建一个新的选择菜单。 |
类方法
| 序号 | 方法及描述 |
|---|---|
| 1 | void add(String item) 向此 Choice 菜单添加一个项目。 |
| 2 | void addItem(String item) 自 Java 2 平台 v1.1 起已过时。 |
| 3 | void addItemListener(ItemListener l) 添加指定的项目监听器以接收来自此 Choice 菜单的项目事件。 |
| 4 | void addNotify() 创建 Choice 的对等体。 |
| 5 | int countItems() 已弃用。从 JDK 版本 1.1 开始,由 getItemCount() 替换。 |
| 6 | AccessibleContext getAccessibleContext() 获取与此 Choice 关联的 AccessibleContext。 |
| 7 | String getItem(int index) 获取此 Choice 菜单中指定索引处的字符串。 |
| 8 | int getItemCount() 返回此 Choice 菜单中的项目数量。 |
| 9 | ItemListener[] getItemListeners() 返回在此选择上注册的所有项目监听器的数组。 |
| 10 | <T extends EventListener> T[] getListeners(Class<T> listenerType) 返回当前在此 Choice 上注册为 FooListeners 的所有对象的数组。 |
| 11 | int getSelectedIndex() 返回当前所选项目的索引。 |
| 12 | String getSelectedItem() 获取当前选择的字符串表示形式。 |
| 13 | Object[] getSelectedObjects() 返回一个包含当前所选项目的数组(长度为 1)。 |
| 14 | void insert(String item, int index) 在指定位置将项目插入此选择中。 |
| 15 | protected String paramString() 返回表示此 Choice 菜单状态的字符串。 |
| 16 | protected void processEvent(AWTEvent e) 处理此选择上的事件。 |
| 17 | protected void processItemEvent(ItemEvent e) 通过将事件分派给任何已注册的 ItemListener 对象来处理在此 Choice 菜单上发生的项目事件。 |
| 18 | void remove(int position) 从指定位置的 Choice 菜单中删除一个项目。 |
| 19 | void remove(String item) 从 Choice 菜单中删除 item 的第一次出现。 |
| 20 | void removeAll() 从 Choice 菜单中删除所有项目。 |
| 21 | void removeItemListener(ItemListener l) 移除指定的项目监听器,使其不再接收来自此 Choice 菜单的项目事件。 |
| 22 | void select(int pos) 将此 Choice 菜单中的所选项目设置为指定位置处的项目。 |
| 23 | void select(String str) 将此 Choice 菜单中的所选项目设置为名称等于指定字符串的项目。 |
继承的方法
此类继承自以下类的方法
java.awt.Component
java.lang.Object
Choice 示例
使用您选择的任何编辑器创建以下 Java 程序,例如在D:/ > AWT > com > tutorialspoint > gui >
AwtControlDemo.javapackage com.tutorialspoint.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 com\tutorialspoint\gui\AwtControlDemo.java
如果没有任何错误,则表示编译成功。使用以下命令运行程序。
D:\AWT>java com.tutorialspoint.gui.AwtControlDemo
验证以下输出