secretlabel.java

来自「java swing 开发代码」· Java 代码 · 共 40 行

JAVA
40
字号
// SecretLabel.java// An extension of the JLabel class that listens to mouse clicks and converts// them to ActionEvents, which in turn are reported via an EventListenersList // object//package	jswing.ch27;import java.awt.event.*;import javax.swing.*;public class SecretLabel extends JLabel {  public SecretLabel(String msg) {    super(msg);    addMouseListener(new MouseAdapter() {      public void mouseClicked(MouseEvent me) {        fireActionPerformed(new ActionEvent(SecretLabel.this,                                             ActionEvent.ACTION_PERFORMED,                                             "SecretMessage"));      }    });  }  public void addActionListener(ActionListener l) {    // We'll just use the listenerList we inherit from JComponent.    listenerList.add(ActionListener.class, l);  }  public void removeActionListener(ActionListener l) {    listenerList.remove(ActionListener.class, l);  }  protected void fireActionPerformed(ActionEvent ae) {    Object[] listeners = listenerList.getListeners(ActionListener.class);    for (int i = 0; i < listeners.length; i++) {      ((ActionListener)listeners[i]).actionPerformed(ae);    }  }}

⌨️ 快捷键说明

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