目录

FocusAdapter

介绍 (Introduction)

FocusAdapter类是一个抽象(适配器)类,用于接收键盘焦点事件。 此类的所有方法都是空的。 此类是用于创建侦听器对象的便捷类。

类声明

以下是java.awt.event.FocusAdapter类的声明:

public abstract class FocusAdapter
   extends Object
      implements FocusListener

类构造函数

SN 构造函数和描述
1

FocusAdapter()

类方法

SN 方法和描述
1

void focusGained(FocusEvent e)

当组件获得键盘焦点时调用。

2

focusLost(FocusEvent e)

组件失去键盘焦点时调用。

方法继承

该类继承以下类中的方法:

  • java.lang.Object

FocusAdapter示例

使用您选择的任何编辑器创建以下java程序,例如D:/ 》 AWT 》 com 》 iowiki 》 gui 》

AwtAdapterDemo.java
package com.iowiki.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\iowiki\gui\AwtAdapterDemo.java

如果没有错误,那意味着编译成功。 使用以下命令运行程序。

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

验证以下输出

AWT FocusAdapter
↑回到顶部↑
WIKI教程 @2018