集册 Java实例教程 使用内部类来监听事件

使用内部类来监听事件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

550
使用内部类来监听事件

import java.awt.event.ActionEvent;/** n o w j a   v  a . c o m - 时  代  Java 提 供 **/

import java.awt.event.ActionListener;


import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;


public class ClickMe2 extends JFrame {

  public static void main(String[] args) {

    new ClickMe2();

  }


  private JButton button1;/** from 时 代 J a v a - N o w J a v a . c o m**/


  public ClickMe2() {

    this.setSize(200, 100);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setTitle("I'm Listening");


    ClickListener cl = new ClickListener();


    JPanel panel1 = new JPanel();

    button1 = new JButton("Click Me!");

    button1.addActionListener(cl);

    panel1.add(button1);

    this.add(panel1);


    this.setVisible(true);

  }


  private class ClickListener implements ActionListener {

    private int clickCount = 0;


    public void actionPerformed(ActionEvent e) {

      if (e.getSource() == button1) {

  
展开阅读全文