本文實例匯總了java中常見的事件響應方法,包括容器類監聽、監聽器類、AbstractAction、反射等。以方便大家參考。具體方法如下:
首先,在Java圖形用戶界面中,處理事件時所必須的步驟是:
1、創建接受響應的組件(控件)
2、實現相關事件監聽接口
3、注冊事件源的動作監聽器
4、事件觸發時的事件處理
相應的可以通過以下的集中方式來作出事件響應。
一、容器類監聽
效果:單擊窗體中的三個按鈕,實現相應的相應時間。
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”實現監聽的接口,如果要對多種監聽方式進行監聽,則用逗號間隔開 // 比如“implements ActionListener,KeyListener” class ButtonListener extends JFrame implements ActionListener{ JButton ok, cancel,exit; //創建接受響應的組建,就是三個按鈕 public ButtonListener(String title){ super (title); this .setLayout( new FlowLayout()); ok = new JButton( "確定" ); cancel = new JButton( "返回" ); exit = new JButton( "退出" ); //下面三個語句 為按鈕分別 注冊監聽器 ok.addActionListener( this ); cancel.addActionListener( this ); exit.addActionListener( this ); getContentPane().add(ok); getContentPane().add(cancel); getContentPane().add(exit); } //完成 事件觸發時的事件處理 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 ); } } |
二、監聽類實現
效果:單擊窗體中的三個按鈕,實現相應的相應時間。
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 { //這里沒有實現監聽 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 ); } } //監聽動作事件 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類實現監聽
效果:單擊菜單,作出響應
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" ); //實例化一個菜單項,并添加監聽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" ); } } |
上述實例備有較為詳盡的注釋,應該不難理解。希望本文所述實例對大家能夠有所幫助。