Awt-choice

提供:Dev Guides
移動先:案内検索

AWT選択クラス

前書き

選択肢コントロールは、選択肢のポップアップメニューを表示するために使用されます。 選択した選択肢は、メニューの上部に表示されます。

クラス宣言

以下は java.awt.Choice クラスの宣言です。

public class Choice
   extends Component
      implements ItemSelectable, Accessible

クラスコンストラクター

S.N. Constructor & Description
1

Choice() ()

新しい選択メニューを作成します。

クラスメソッド

S.N. Method & Description
1

void add(String item)

この選択肢メニューにアイテムを追加します。

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)

指定した位置の選択メニューからアイテムを削除します。

19

void remove(String item)

選択メニューから最初に出現したアイテムを削除します。

20

void removeAll()

選択メニューからすべてのアイテムを削除します。

21

void removeItemListener(ItemListener l)

このChoiceメニューから項目イベントを受け取らないように、指定された項目リスナーを削除します。

22

void select(int pos)

このChoiceメニューで選択したアイテムを、指定した位置のアイテムに設定します。

23

void select(String str)

このChoiceメニューで選択したアイテムを、指定した文字列と同じ名前のアイテムに設定します。

継承されたメソッド

このクラスは、次のクラスからメソッドを継承します。

  • java.awt.Component
  • java.lang.Object

選択例

たとえば、 D:/> AWT> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。

AwtControlDemo.java

package com.finddevguides.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\finddevguides\gui\AwtControlDemo.java

エラーが発生しない場合は、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。

D:\AWT>java com.finddevguides.gui.AwtControlDemo

次の出力を確認します

AWT Choice