Swing-focusadapter
提供:Dev Guides
SWING-FocusAdapterクラス
前書き
クラス FocusAdapter は、キーボードフォーカスイベントを受け取るための抽象(アダプター)クラスです。 このクラスのすべてのメソッドは空です。 このクラスは、リスナーオブジェクトを作成するための便利なクラスです。
クラス宣言
以下は java.awt.event.FocusAdapter クラスの宣言です-
public abstract class FocusAdapter
extends Object
implements FocusListener
クラスコンストラクター
Sr.No. | Constructor & Description |
---|---|
1 | FocusAdapter() |
クラスメソッド
Sr.No. | Method & Description |
---|---|
1 |
void focusGained(FocusEvent e) コンポーネントがキーボードフォーカスを取得すると呼び出されます。 |
継承されるメソッド
このクラスは、次のクラスからメソッドを継承します-
- java.lang.Object
FocusAdapterの例
たとえば、 D:/> SWING> com> finddevguides> gui> の任意のエディターを使用して、次のJavaプログラムを作成します。
SwingAdapterDemo.java
package com.finddevguides.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\finddevguides\gui\SwingAdapterDemo.java
エラーが発生しない場合、コンパイルが成功したことを意味します。 次のコマンドを使用してプログラムを実行します。
D:\SWING>java com.finddevguides.gui.SwingAdapterDemo
次の出力を確認します。