激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java常見事件響應方法實例匯總

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

2019-11-26 15:20shichen2014 JAVA教程

這篇文章主要介紹了java常見事件響應方法,對于初學者有很好的參考借鑒價值,分享給大家,需要的朋友可以參考下

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

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
//聲明 類時,添加“implements ActionListener”實現監(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)聽類實現 
 
效果:單擊窗體中的三個按鈕,實現相應的相應時間。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
class ButtonListener1 extends JFrame { //這里沒有實現監(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類實現監(jiān)聽

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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,必須實現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類 + 反射 的方法 
 
效果:單擊工具欄的三個按鈕,通過按鈕的名稱,反射得到 與按鈕名稱相同的類實現響應。 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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");
  }
}

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区在线观看视频 | 激情亚洲一区二区 | 成人资源在线观看 | 在线视频观看国产 | 麻豆视频国产在线观看 | 亚洲精品一二三区 | 欧美视屏一区二区 | 美女黄视频在线观看 | 羞羞视频免费观看网站 | 激情视频在线播放 | 天天看夜夜爽 | 日本久久久网站 | 老司机免费福利午夜入口ae58 | 27xxoo无遮挡动态视频 | 国产精品自拍片 | 91精品国产综合久久久欧美 | 精品中文一区 | 欧美一级做一a做片性视频 日韩黄色片免费看 | 成年人免费高清视频 | 奇米影视888狠狠狠777不卡 | 看国产毛片 | 久久久久久久国产a∨ | 久久成人免费观看 | 亚洲精品成人18久久久久 | 免费专区 - 91爱爱 | 成人黄色小视频网站 | 羞羞的视频免费在线观看 | 国产精品久久999 | 成人毛片在线免费观看 | 日本人乱人乱亲乱色视频观看 | 中文字幕www. | 香蕉久久久精品 | 亚洲欧美日韩久久精品第一区 | 日韩精品中文字幕在线观看 | 看一级大毛片 | 精品成人av一区二区在线播放 | 羞羞网站在线看 | 精品在线观看一区二区 | 国产亚洲精品久久久久久久久 | 91成人午夜性a一级毛片 | 日韩黄a|