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

java常見事件響應方法實例匯總

 更新時間:2014年08月19日 10:39:17   投稿:shichen2014  
這篇文章主要介紹了java常見事件響應方法,對于初學者有很好的參考借鑒價值,分享給大家,需要的朋友可以參考下

本文實例匯總了java中常見的事件響應方法,包括容器類監(jiān)聽、監(jiān)聽器類、AbstractAction、反射等。以方便大家參考。具體方法如下:

首先,在Java圖形用戶界面中,處理事件時所必須的步驟是

1、創(chuàng)建接受響應的組件(控件)
2、實現(xiàn)相關事件監(jiān)聽接口
3、注冊事件源的動作監(jiān)聽器
4、事件觸發(fā)時的事件處理

相應的可以通過以下的集中方式來作出事件響應。

一、容器類監(jiān)聽 
 
效果:單擊窗體中的三個按鈕,實現(xiàn)相應的相應時間。 
 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
//聲明 類時,添加“implements ActionListener”實現(xiàn)監(jiān)聽的接口,如果要對多種監(jiān)聽方式進行監(jiān)聽,則用逗號間隔開 
// 比如“implements ActionListener,KeyListener” 
 
class ButtonListener extends JFrame implements ActionListener{ 
 JButton ok, cancel,exit; //創(chuàng)建接受響應的組建,就是三個按鈕 
 public ButtonListener(String title){ 
 super(title); 
 this.setLayout(new FlowLayout()); 
 ok = new JButton("確定"); 
 cancel = new JButton("返回"); 
 exit = new JButton("退出"); 
//下面三個語句 為按鈕分別 注冊監(jiān)聽器 
 ok.addActionListener(this);   
 cancel.addActionListener(this); 
 exit.addActionListener(this); 
 getContentPane().add(ok); 
 getContentPane().add(cancel); 
 getContentPane().add(exit); 
} 
 
//完成 事件觸發(fā)時的事件處理 
 public void actionPerformed(ActionEvent e){ 
   if(e.getSource()==ok) 
    System.out.println("確定"); 
   if(e.getSource()==cancel) 
    System.out.println("返回"); 
   if(e.getSource()==exit) 
     System.exit(0);; 
 } 
 
 public static void main(String args[]) { 
   ButtonListener pd=new ButtonListener("ActionEvent Demo"); 
   pd.setSize(250,100); 
  pd.setVisible(true); 
 } 
} 
 

二、監(jiān)聽類實現(xiàn) 
 
效果:單擊窗體中的三個按鈕,實現(xiàn)相應的相應時間。 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
 
class ButtonListener1 extends JFrame { //這里沒有實現(xiàn)監(jiān)聽 
 JButton ok, cancel,exit; 
 public ButtonListener1(String title){ 
  super(title); 
  this.setLayout(new FlowLayout()); 
  ok = new JButton("確定"); 
  cancel = new JButton("返回"); 
  exit = new JButton("退出"); 
  ok.addActionListener(new MyListener()); 
  cancel.addActionListener(new MyListener());; 
  exit.addActionListener(new MyListener());; 
  getContentPane().add(ok); 
  getContentPane().add(cancel); 
  getContentPane().add(exit); 
 } 
 
 public static void main(String args[]) { 
   ButtonListener pd=new ButtonListener("ActionEvent Demo"); 
   pd.setSize(250,100); 
  pd.setVisible(true); 
 } 
} 
  //監(jiān)聽動作事件 
class MyListener implements ActionListener{ 
  public void actionPerformed(ActionEvent e){ 
   if(e.getActionCommand()=="確定") 
    System.out.println("確定"); 
   if(e.getActionCommand()=="返回") 
    System.out.println("返回"); 
   if(e.getActionCommand()=="退出") 
     System.exit(0);; 
  } 
}

三、使用 AbstractAction類實現(xiàn)監(jiān)聽

效果:單擊菜單,作出響應

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
 
//此類繼承AbstractAction,必須實現(xiàn)actionPerformed()方法。 
class AbstractEvent extends AbstractAction{ 
  //private static final long serialVersionUID = 1L; 
  AbstractEvent(){ 
  } 
  public void actionPerformed(ActionEvent e){ 
    //彈出確認對話框 
    if (e.getActionCommand()=="open"){ 
      JOptionPane.showMessageDialog(null, "打開"); 
    }else if (e.getActionCommand()=="close"){ 
      JOptionPane.showMessageDialog(null, "關閉"); 
    }else if (e.getActionCommand()=="run"){ 
      JOptionPane.showMessageDialog(null, "運行"); 
    }else if (e.getActionCommand()=="stop"){ 
      JOptionPane.showMessageDialog(null, "停止"); 
    } 
  } 
} 
public class TestAbstractEvent { 
  private static JMenuBar menubar; 
  private static JFrame frame; 
   
  //指定MenuEvent的具體處理程序是AbstractEvent類完成的。 
  final Action MenuEvent=new AbstractEvent(); 
  public TestAbstractEvent(){ 
    frame=new JFrame("menu"); 
    frame.getContentPane().setLayout(new BorderLayout()); 
    menubar=new JMenuBar(); 
    JMenu menuFile=new JMenu("file"); 
     
    //實例化一個菜單項,并添加監(jiān)聽openAction, 
    JMenuItem menuItemopen=new JMenuItem("open"); 
    menuItemopen.addActionListener(MenuEvent); 
    JMenuItem menuItemclose=new JMenuItem("close"); 
    menuItemclose.addActionListener(MenuEvent); 
    menuFile.add(menuItemopen); 
    menuFile.add(menuItemclose); 
    JMenu menuTool=new JMenu("tool"); 
    JMenuItem menuItemrun=new JMenuItem("run"); 
    menuItemrun.addActionListener(MenuEvent); 
    JMenuItem menuItemstop=new JMenuItem("stop"); 
    menuItemstop.addActionListener(MenuEvent); 
    menuTool.add(menuItemrun); 
    menuTool.add(menuItemstop); 
    menubar.add(menuFile); 
    menubar.add(menuTool); 
    menubar.setVisible(true); 
    frame.add(menubar,BorderLayout.NORTH); 
    frame.setSize(400,200); 
    frame.setVisible(true); 
  } 
  public static void main(String[] args){ 
    new TestAbstractEvent(); 
  } 
} 

四、 AbstractAction類 + 反射 的方法 
 
效果:單擊工具欄的三個按鈕,通過按鈕的名稱,反射得到 與按鈕名稱相同的類實現(xiàn)響應。 

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 
 
class ViewAction extends AbstractAction{ 
  private String ActionName=""; 
  //private JFrame frame=null; 
  private Action action=null; 
  public ViewAction(){ 
  } 
  public ViewAction(String ActionName){ 
    this.ActionName=ActionName; 
    //this.frame=frame; 
  } 
  @Override 
  public void actionPerformed(ActionEvent e) { 
    Action action=getAction(this.ActionName); 
    action.execute(); 
  } 
  private Action getAction(String ActionName){ 
    try{ 
      if (this.action==null){ 
        Action action=(Action)Class.forName(ActionName).newInstance(); 
        this.action=action; 
      } 
      return this.action; 
    }catch(Exception e){ 
    return null; 
    } 
  } 
} 
public class TestAE extends JFrame { 
  public JToolBar bar=new JToolBar(); 
  String buttonName[]={"b1","b2","b3"}; 
  public TestAE(){ 
    super("事件"); 
    for (int i=0;i<buttonName.length;i++){ 
      ViewAction action=new ViewAction(buttonName[i]); 
      JButton button=new JButton(buttonName[i]); 
      button.addActionListener(action); 
      bar.add(button); 
    } 
    this.getContentPane().add(bar,BorderLayout.NORTH); 
    this.setSize(300, 200); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
  } 
  public static void main(String [] args){ 
    new TestAE(); 
  } 
} 
interface Action{ 
  void execute(); 
} 
class b1 implements Action{ 
  public void execute(){ 
    JOptionPane.showMessageDialog(null, "單擊了 b1"); 
  } 
} 
class b2 implements Action{ 
  public void execute(){ 
    JOptionPane.showMessageDialog(null, "單擊了 b2"); 
  } 
} 
class b3 implements Action{ 
  public void execute(){ 
    JOptionPane.showMessageDialog(null, "單擊了 b3"); 
  } 
}

上述實例備有較為詳盡的注釋,應該不難理解。希望本文所述實例對大家能夠有所幫助。

相關文章

  • 解決Maven多模塊編譯慢的問題

    解決Maven多模塊編譯慢的問題

    這篇文章主要介紹了Maven多模塊編譯慢的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring中的自動裝配機制詳解

    Spring中的自動裝配機制詳解

    這篇文章主要介紹了Spring中的自動裝配機制詳解,自動裝配就是會通過Spring的上下文為你找出相應依賴項的類,通俗的說就是Spring會在上下文中自動查找,并自動給Bean裝配與其相關的屬性,需要的朋友可以參考下
    2023-08-08
  • Java Scanner如何獲取字符串和帶空格的字符串

    Java Scanner如何獲取字符串和帶空格的字符串

    這篇文章主要介紹了Java Scanner如何獲取字符串和帶空格的字符串問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 學java得這樣學,學習確實也得這樣

    學java得這樣學,學習確實也得這樣

    學java得這樣學,學習東西確實也得這樣
    2008-02-02
  • 在IntelliJ IDEA中.idea文件是什么可以刪除嗎

    在IntelliJ IDEA中.idea文件是什么可以刪除嗎

    相信有很多小伙伴,在用idea寫java代碼的時候,創(chuàng)建工程總是會出現(xiàn).idea文件,該文件也從來沒去打開使用過,那么它在我們項目里面,扮演什么角色,到底能不能刪除它呢?這篇文章主要介紹了在IntelliJ IDEA中.idea文件是什么可以刪除嗎,需要的朋友可以參考下
    2024-01-01
  • SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型

    SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型

    這篇文章主要介紹了SpringMvc @RequestParam 使用推薦使用包裝類型代替包裝類型,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • 詳解Java的Struts框架以及相關的MVC設計理念

    詳解Java的Struts框架以及相關的MVC設計理念

    這篇文章主要介紹了詳解Java的Struts框架以及相關的MVC設計理念,Struts是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Spring IOC:CreateBean環(huán)節(jié)中的流程轉(zhuǎn)換

    Spring IOC:CreateBean環(huán)節(jié)中的流程轉(zhuǎn)換

    Spring IOC 體系是一個很值得深入和研究的結構 , 只有自己真正的讀一遍 , 才能有更好的理解.這篇文章主要說明一下 CreateBean 整個環(huán)節(jié)中的大流程轉(zhuǎn)換 , 便于查找問題的原因
    2021-05-05
  • 詳解SpringBoot應用服務啟動與安全終止

    詳解SpringBoot應用服務啟動與安全終止

    這篇文章主要介紹了SpringBoot應用服務啟動與安全終止,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Java底層基于鏈表實現(xiàn)集合和映射--集合Set操作詳解

    Java底層基于鏈表實現(xiàn)集合和映射--集合Set操作詳解

    這篇文章主要介紹了Java底層基于鏈表實現(xiàn)集合和映射集合Set操作,結合實例形式詳細分析了Java使用鏈表實現(xiàn)集合和映射相關原理、操作技巧與注意事項,需要的朋友可以參考下
    2020-03-03

最新評論