最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

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

class EventListener3 extends JFrame {
  private JButton btBlue, btDialog;

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

    add(btBlue);
    add(btDialog);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  // 內(nèi)部類(lè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)部類(lè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)聽(tīng)處理——匿名內(nèi)部類(lèi)處理
 *
 * @author codebrother
 */
class EventListener2 extends JFrame {
  private JButton btBlue, btDialog;

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

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

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

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

}
// 外部類(lèi)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);
  }
}
// 外部類(lè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);
  }
}

public class ActionListenerTest
{
  public static void main(String args[])
  {
    new EventListener2();
  }
}

相關(guān)文章

最新評(píng)論

曲阜市| 禄丰县| 柘荣县| 林周县| 凤冈县| 昌黎县| 江达县| 紫云| 平利县| 张家界市| 河津市| 名山县| 乌鲁木齐市| 边坝县| 南投市| 孙吴县| 将乐县| 乐至县| 威宁| 子长县| 苏尼特右旗| 平乐县| 南郑县| 衡阳市| 从化市| 土默特右旗| 清新县| 新龙县| 江都市| 二连浩特市| 灵寿县| 共和县| 河曲县| 泽库县| 宜都市| 离岛区| 顺昌县| 宁波市| 电白县| 莱阳市| 综艺|