Awt-focusadapter

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

AWT FocusAdapterクラス

前書き

クラス FocusAdapter は、キーボードフォーカスイベントを受け取るための抽象(アダプター)クラスです。 このクラスのすべてのメソッドは空です。 このクラスは、リスナーオブジェクトを作成するための便利なクラスです。

クラス宣言

以下は、 java.awt.event.FocusAdapter クラスの宣言です。

public abstract class FocusAdapter
   extends Object
      implements FocusListener

クラスコンストラクター

S.N. Constructor & Description
1 FocusAdapter()

クラスメソッド

S.N. Method & Description
1

void focusGained(FocusEvent e)

コンポーネントがキーボードフォーカスを取得すると呼び出されます。

2

focusLost(FocusEvent e)

コンポーネントがキーボードフォーカスを失ったときに呼び出されます。

継承されたメソッド

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

  • java.lang.Object

FocusAdapterの例

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

AwtAdapterDemo.java

package com.finddevguides.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtAdapterDemo {
   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtAdapterDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtAdapterDemo  awtAdapterDemo = new AwtAdapterDemo();
      awtAdapterDemo.showFocusAdapterDemo();
   }

   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 showFocusAdapterDemo(){

      headerLabel.setText("Listener in action: FocusAdapter");

      Button okButton = new Button("OK");
      Button cancelButton = new Button("Cancel");
      okButton.addFocusListener(new FocusAdapter(){
         public void focusGained(FocusEvent e) {
            statusLabel.setText(statusLabel.getText()
            + e.getComponent().getClass().getSimpleName()
            + " gained focus. ");
         }
      });

      cancelButton.addFocusListener(new FocusAdapter(){
         public void focusLost(FocusEvent e) {
            statusLabel.setText(statusLabel.getText()
            + e.getComponent().getClass().getSimpleName()
            + " lost focus. ");
         }
      });

      controlPanel.add(okButton);
      controlPanel.add(cancelButton);
      mainFrame.setVisible(true);
   }
}

コマンドプロンプトを使用してプログラムをコンパイルします。 D:/> AWT に移動し、次のコマンドを入力します。

D:\AWT>javac com\finddevguides\gui\AwtAdapterDemo.java

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

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

次の出力を確認します

AWT FocusAdapter