欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java添加事件監(jiān)聽的四種方法代碼實(shí)例

 更新時(shí)間:2014年09月22日 09:42:44   投稿:junjie  
這篇文章主要介紹了Java添加事件監(jiān)聽的四種方法代碼實(shí)例,本文直接給出代碼示例,并用注釋說明,需要的朋友可以參考下

Java添加事件的幾種方式(轉(zhuǎn)載了codebrother的文章,做了稍微的改動):

/**
 * Java事件監(jiān)聽處理——自身類實(shí)現(xiàn)ActionListener接口,作為事件監(jiān)聽器
 *
 * @author codebrother
 */
class EventListener1 extends JFrame implements ActionListener {
  private JButton btBlue, btDialog;

  public EventListener1() {
    setTitle("Java GUI 事件監(jiān)聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍(lán)色");   
    btDialog = new JButton("彈窗");
    
    // 將按鈕添加事件監(jiān)聽器
    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事件監(jiān)聽處理——內(nèi)部類處理
 *
 * @author codebrother
 */

class EventListener3 extends JFrame {
  private JButton btBlue, btDialog;

  // 構(gòu)造方法
  public EventListener3() {
    setTitle("Java GUI 事件監(jiān)聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍(lán)色");
    btDialog = new JButton("彈窗");
    // 添加事件監(jiān)聽器對象(面向?qū)ο笏枷?
    btBlue.addActionListener(new ColorEventListener());
    btDialog.addActionListener(new DialogEventListener());

    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // 內(nèi)部類ColorEventListener,實(shí)現(xiàn)ActionListener接口
  class ColorEventListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
      Container c = getContentPane();
      c.setBackground(Color.BLUE);
    }
  }
  // 內(nèi)部類DialogEventListener,實(shí)現(xiàn)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事件監(jiān)聽處理——匿名內(nèi)部類處理
 *
 * @author codebrother
 */
class EventListener2 extends JFrame {
  private JButton btBlue, btDialog;

  public EventListener2() {
    setTitle("Java GUI 事件監(jiān)聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());

    btBlue = new JButton("藍(lán)色");
    btDialog = new JButton("彈窗");
    
    // 添加事件監(jiān)聽器(此處即為匿名類)
    btBlue.addActionListener(new ActionListener() {
      // 事件處理
      @Override
      public void actionPerformed(ActionEvent e) {
        Container c = getContentPane();
        c.setBackground(Color.BLUE);
      }
    });
    
    // 并添加事件監(jiān)聽器 
    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事件監(jiān)聽處理——外部類處理
 *
 * @author codebrother
 */
class EventListener4 extends JFrame {
  private JButton btBlue, btDialog;

  public EventListener4() {
    setTitle("Java GUI 事件監(jiān)聽處理");
    setBounds(100, 100, 500, 350);
    setLayout(new FlowLayout());
    btBlue = new JButton("藍(lán)色");
    btDialog = new JButton("彈窗");
    // 將按鈕添加事件監(jiān)聽器
    btBlue.addActionListener(new ColorEventListener(this));
    btDialog.addActionListener(new DialogEventListener());

    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

}
// 外部類ColorEventListener,實(shí)現(xiàn)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,實(shí)現(xiàn)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();
  }
}

相關(guān)文章

  • springboot中thymeleaf模板使用詳解

    springboot中thymeleaf模板使用詳解

    這篇文章將更加全面詳細(xì)的介紹thymeleaf的使用。thymeleaf 是新一代的模板引擎,在spring4.0中推薦使用thymeleaf來做前端模版引擎。
    2017-05-05
  • Java OpenCV實(shí)現(xiàn)人臉識別過程詳解

    Java OpenCV實(shí)現(xiàn)人臉識別過程詳解

    這篇文章主要介紹了Java OpenCV實(shí)現(xiàn)人臉識別過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法

    springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法

    今天小編就為大家分享一篇springmvc接收json串,轉(zhuǎn)換為實(shí)體類List方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 使用springmvc臨時(shí)不使用視圖解析器的自動添加前后綴

    使用springmvc臨時(shí)不使用視圖解析器的自動添加前后綴

    這篇文章主要介紹了使用springmvc臨時(shí)不使用視圖解析器的自動添加前后綴,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 一篇文章弄懂JVM類加載機(jī)制過程以及原理

    一篇文章弄懂JVM類加載機(jī)制過程以及原理

    JVM原理對于初學(xué)者而言,比較晦澀難以理解,概念繁多又比較抽象,很多時(shí)候感覺看不見摸不著,還不好驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于如何通過一篇文章弄懂JVM類加載機(jī)制過程及原理的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • 詳解Java線程池的增長過程

    詳解Java線程池的增長過程

    在本篇文章里小編給大家整理的是關(guān)于Java線程池的增長過程以及相關(guān)知識點(diǎn),需要的朋友們可以參考下。
    2019-08-08
  • Mybatis Plus查詢時(shí)sql字段名大小寫報(bào)錯(cuò)的解決

    Mybatis Plus查詢時(shí)sql字段名大小寫報(bào)錯(cuò)的解決

    這篇文章主要介紹了Mybatis Plus查詢時(shí)sql字段名大小寫報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring技巧之如何動態(tài)讀取配置文件

    Spring技巧之如何動態(tài)讀取配置文件

    這篇文章主要介紹了Spring技巧之如何動態(tài)讀取配置文件的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 如何利用java中String類的substring()字符串截取最后一個(gè)字符

    如何利用java中String類的substring()字符串截取最后一個(gè)字符

    Java中的String是不可變的類型,因此substring()方法并不會改變原字符串,而是返回了一個(gè)新的字符串,這篇文章主要介紹了如何利用java中String類的substring()字符串截取最后一個(gè)字符,需要的朋友可以參考下
    2023-11-11
  • java查詢mongodb中的objectid示例

    java查詢mongodb中的objectid示例

    這篇文章主要介紹了java查詢mongodb中的objectid示例,需要的朋友可以參考下
    2014-04-04

最新評論