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

1.Java事件监听处理——自身类实现ActionListener接口,作为事件监听器

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);
    }
  }
 
}


2. 内部类

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);
    }
  }
 
}


3. 匿名内部类


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);
  }
 
}

4. 外部类

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();
  }
}




  • 9
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值