欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java添加事件监听的四种方法代码实例

程序员文章站 2024-02-28 21:44:40
java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动): /** * java事件监听处理——自身类实现actionlisten...

java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动):

/**
 * java事件监听处理——自身类实现actionlistener接口,作为事件监听器
 *
 * @author codebrother
 */
class eventlistener1 extends jframe implements actionlistener {
  private jbutton btblue, btdialog;

  public eventlistener1() {
    settitle("java gui 事件监听处理");
    setbounds(100, 100, 500, 350);
    setlayout(new flowlayout());
    btblue = new jbutton("蓝色");   
    btdialog = new jbutton("弹窗");
    
    // 将按钮添加事件监听器
    btblue.addactionlistener(this);
    btdialog.addactionlistener(this);

    add(btblue);
    add(btdialog);

    setvisible(true);
    setdefaultcloseoperation(jframe.exit_on_close);
  }
  // ***************************事件处理***************************
  @override
  public void actionperformed(actionevent e) {
    if (e.getsource() == btblue) {
      container c = getcontentpane();
      c.setbackground(color.blue);
    }
    else if (e.getsource() == btdialog) {
      jdialog dialog = new jdialog();
      dialog.setbounds(300, 200, 400, 300);
      dialog.setvisible(true);
    }
  }

}

/**
 * java事件监听处理——内部类处理
 *
 * @author codebrother
 */

class eventlistener3 extends jframe {
  private jbutton btblue, btdialog;

  // 构造方法
  public eventlistener3() {
    settitle("java gui 事件监听处理");
    setbounds(100, 100, 500, 350);
    setlayout(new flowlayout());
    btblue = new jbutton("蓝色");
    btdialog = new jbutton("弹窗");
    // 添加事件监听器对象(面向对象思想)
    btblue.addactionlistener(new coloreventlistener());
    btdialog.addactionlistener(new dialogeventlistener());

    add(btblue);
    add(btdialog);
    setvisible(true);
    setdefaultcloseoperation(jframe.exit_on_close);
  }
  // 内部类coloreventlistener,实现actionlistener接口
  class coloreventlistener implements actionlistener {
    @override
    public void actionperformed(actionevent e) {
      container c = getcontentpane();
      c.setbackground(color.blue);
    }
  }
  // 内部类dialogeventlistener,实现actionlistener接口
  class dialogeventlistener implements actionlistener {
    @override
    public void actionperformed(actionevent e) {
      jdialog dialog = new jdialog();
      dialog.setbounds(300, 200, 400, 300);
      dialog.setvisible(true);
    }
  }

}

/**
 * java事件监听处理——匿名内部类处理
 *
 * @author codebrother
 */
class eventlistener2 extends jframe {
  private jbutton btblue, btdialog;

  public eventlistener2() {
    settitle("java gui 事件监听处理");
    setbounds(100, 100, 500, 350);
    setlayout(new flowlayout());

    btblue = new jbutton("蓝色");
    btdialog = new jbutton("弹窗");
    
    // 添加事件监听器(此处即为匿名类)
    btblue.addactionlistener(new actionlistener() {
      // 事件处理
      @override
      public void actionperformed(actionevent e) {
        container c = getcontentpane();
        c.setbackground(color.blue);
      }
    });
    
    // 并添加事件监听器 
    btdialog.addactionlistener(new actionlistener() {
      @override
      public void actionperformed(actionevent e) {
        jdialog dialog = new jdialog();
        dialog.setbounds(300, 200, 400, 300);
        dialog.setvisible(true);
      }
    });

    add(btblue);
    add(btdialog);
    setvisible(true);
    setdefaultcloseoperation(jframe.exit_on_close);
  }

}

/**
 * java事件监听处理——外部类处理
 *
 * @author codebrother
 */
class eventlistener4 extends jframe {
  private jbutton btblue, btdialog;

  public eventlistener4() {
    settitle("java gui 事件监听处理");
    setbounds(100, 100, 500, 350);
    setlayout(new flowlayout());
    btblue = new jbutton("蓝色");
    btdialog = new jbutton("弹窗");
    // 将按钮添加事件监听器
    btblue.addactionlistener(new coloreventlistener(this));
    btdialog.addactionlistener(new dialogeventlistener());

    add(btblue);
    add(btdialog);
    setvisible(true);
    setdefaultcloseoperation(jframe.exit_on_close);
  }

}
// 外部类coloreventlistener,实现actionlistener接口
class coloreventlistener implements actionlistener {
  private eventlistener4 el;
  coloreventlistener(eventlistener4 el) {
    this.el = el;
  }
  @override
  public void actionperformed(actionevent e) {
    container c = el.getcontentpane();
    c.setbackground(color.blue);
  }
}
// 外部类dialogeventlistener,实现actionlistener接口
class dialogeventlistener implements actionlistener {
  @override
  public void actionperformed(actionevent e) {
    jdialog dialog = new jdialog();
    dialog.setbounds(300, 200, 400, 300);
    dialog.setvisible(true);
  }
}

public class actionlistenertest
{
  public static void main(string args[])
  {
    new eventlistener2();
  }
}