- SWING 教程
- SWING - 主页
- SWING - 概述
- SWING - 环境
- SWING - 控件
- SWING - 事件处理
- SWING - 事件类
- SWING - 事件侦听器
- SWING - 事件适配器
- SWING - 布局
- SWING - 菜单
- SWING - 容器
- SWING 有用资源
- SWING - 快速指南
- SWING - 有用资源
- SWING - 讨论
SWING - FocusAdapter 类
简介
FocusAdapter 类是一个用于接收键盘焦点事件的抽象(适配器)类。此类的所有方法均为空。此类是一个便利类,用于创建侦听器对象。
类声明
以下是 java.awt.event.FocusAdapter 类的声明 −
public abstract class FocusAdapter
extends Object
implements FocusListener
类构造函数
| 序号 | 构造函数和描述 |
|---|---|
| 1 |
FocusAdapter() |
类方法
| 序号 | 方法和描述 |
|---|---|
| 1 |
void focusGained(FocusEvent e) 当组件获得键盘焦点时触发。 |
继承的方法
此类继承了以下类的这些方法 −
- java.lang.Object
FocusAdapter 实例
使用您选择的任何编辑器,在 D:/ > SWING > com > tutorialspoint > gui > 内创建以下 Java 程序。
SwingAdapterDemo.java
package com.tutorialspoint.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingAdapterDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingAdapterDemo(){
prepareGUI();
}
public static void main(String[] args){
SwingAdapterDemo swingAdapterDemo = new SwingAdapterDemo();
swingAdapterDemo.showFocusAdapterDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java SWING Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
headerLabel = new JLabel("",JLabel.CENTER );
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
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");
JButton okButton = new JButton("OK");
JButton cancelButton = new JButton("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:/ > SWING 并输入以下命令。
D:\SWING>javac com\tutorialspoint\gui\SwingAdapterDemo.java
如果未出现错误,则表示编译成功。使用以下命令运行程序。
D:\SWING>java com.tutorialspoint.gui.SwingAdapterDemo
验证以下输出。
swing_event_adapters.htm
广告