Java 中事件监听器接口和事件适配器类有什么区别?
一个**EventListener**接口定义了特定事件的事件处理程序必须实现的方法,而一个**Event Adapter**类则提供了**EventListener**接口的默认实现。
事件监听器
- **事件监听器**是每个组件处理事件的支柱。
- 特定**EventListener**的每个方法都将有一个参数作为实例,该实例是**EventObject**类的子类。
- **EventListener**接口需要被扩展,并且它将在**java.util**包中定义。
- 一些**EventListener**接口有**ActionListener**、**KeyListener**、**MouseListener**、**FocusListener**、**ItemListener**等等。
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyListenerTest implements KeyListener, ActionListener { JFrame frame; JTextField tf; JLabel lbl; JButton btn; public KeyListenerTest() { frame = new JFrame(); lbl = new JLabel(); tf = new JTextField(15); tf.addKeyListener(this); btn = new JButton("Clear"); btn.addActionListener(this); JPanel panel = new JPanel(); panel.add(tf); panel.add(btn); frame.setLayout(new BorderLayout()); frame.add(lbl, BorderLayout.NORTH); frame.add(panel, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(300, 200); frame.setVisible(true); } @Override public void keyTyped(KeyEvent ke) { lbl.setText("You have typed "+ ke.getKeyChar()); } @Override public void keyPressed(KeyEvent ke) { lbl.setText("You have pressed "+ ke.getKeyChar()); } @Override public void keyReleased(KeyEvent ke) { lbl.setText("You have released "+ ke.getKeyChar()); } @Override public void actionPerformed(ActionEvent ae) { tf.setText(""); } public static void main(String args[]) { new KeyListenerTest(); } }
输出
事件适配器
- 抽象类可以被称为**事件适配器**来接收各种事件。
- 一个**事件适配器**类提供了**EventListener**接口中所有方法的默认实现。
- 一些事件适配器类有**FocusAdapter**、**KeyAdapter**、**MouseAdapter**、**WindowAdapter**等等。
示例
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyAdapterTest { private JFrame frame; private JLabel headLabel; private JLabel msgLabel; private JPanel controlPanel; public KeyAdapterTest() { initializeUI(); } private void initializeUI() { frame = new JFrame("KeyAdapter class"); frame.setSize(350, 275); frame.setLayout(new GridLayout(3, 1)); headLabel = new JLabel("", JLabel.CENTER); msgLabel = new JLabel("", JLabel.CENTER); msgLabel.setSize(300, 100); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { System.exit(0); } }); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); frame.add(headLabel); frame.add(controlPanel); frame.add(msgLabel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); } private void showMouseApapter() { headLabel.setText("KeyAdapter Test"); final JTextField textField = new JTextField(10); JButton displayButton = new JButton("Display"); displayButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { msgLabel.setText("You entered : " + textField.getText()); } }); textField.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_ENTER) { msgLabel.setText("You entered : " + textField.getText()); } } }); controlPanel.add(textField); controlPanel.add(displayButton); frame.setVisible(true); } public static void main(String[] args) { KeyAdapterTest test = new KeyAdapterTest(); test.showMouseApapter(); } }
输出
广告