keytextcomponent2.java

来自「The Definitive Guide to Java Swing, Thir」· Java 代码 · 共 55 行

JAVA
55
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.EventListener;

public class KeyTextComponent2 extends JComponent {
  private EventListenerList actionListenerList = new EventListenerList();

  public KeyTextComponent2() {
    setBackground(Color.CYAN);
    KeyListener internalKeyListener = new KeyAdapter() {
      public void keyPressed(KeyEvent keyEvent) {
        if (actionListenerList != null) {
          int keyCode = keyEvent.getKeyCode();
          String keyText = KeyEvent.getKeyText(keyCode);
          ActionEvent actionEvent = new ActionEvent(
            this,
            ActionEvent.ACTION_PERFORMED,
            keyText);
          fireActionPerformed(actionEvent);
        }
      }
    };

    MouseListener internalMouseListener = new MouseAdapter() {
      public void mousePressed(MouseEvent mouseEvent) {
        requestFocusInWindow();
      }
    };

    addKeyListener(internalKeyListener);
    addMouseListener(internalMouseListener);
  }

  public void addActionListener(ActionListener actionListener) {
    actionListenerList.add(ActionListener.class, actionListener);
  }
  public void removeActionListener(ActionListener actionListener) {
    actionListenerList.remove(ActionListener.class, actionListener);
  }

  protected void fireActionPerformed(ActionEvent actionEvent) {
    EventListener listenerList[] = 
      actionListenerList.getListeners(ActionListener.class);
    for (int i=0, n=listenerList.length; i<n; i++) {
      ((ActionListener)listenerList[i]).actionPerformed(actionEvent);
    }
  }

  public boolean isFocusable() {
    return true;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?