用戶在窗體上對組件進行一定動作,比如鼠標點擊,會產生一些相應的事件,如ActionEvents,ChangeEvents,ItemEvents等,來響應用戶的鼠標點擊行爲。通過實現ActionListener接口來監聽這些事件並作出處理
1.if語句判斷是哪個事件源
代碼示例:
package actionEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * 在這個例子中,利用一個ActionListener來監聽事件源產生的事件 * 用一些if語句來決定是哪個事件源 1. 新建一個組件(如JButton)。 2. 將該組件添加到相應的面板(如JPanel)。 3. 註冊監聽器以監聽事件源產生的事件(如通過ActionListener來響應用戶點擊按鈕)。 4. 定義處理事件的方法(如在ActionListener中的actionPerformed中定義相應方法)。 */ public class ActionDemo { private static JFrame jframe; private static JPanel jpanel; private JButton button1; private JButton button2; public static void main(String[] args){ jframe=new JFrame(); new ActionDemo(); jframe.add(jpanel); jframe.pack();//根據組件大小自適應窗口大小 jframe.setVisible(true);//窗口可顯示 //點擊窗口關閉時退出應用程序 jframe.addWindowListener(new WindowAdapter(){ public void windowClosing(){ System.exit(0); } }); } public ActionDemo(){ jpanel=new JPanel();//實例化面板 button1=new JButton("按鈕1");//實例化按鈕 button2=new JButton("按鈕2"); SimpleListener sl=new SimpleListener();//實例化監聽類 button1.addActionListener(sl);//添加監聽給按鈕 button2.addActionListener(sl); jpanel.add(button1);//按鈕添加到面板 jpanel.add(button2); } private class SimpleListener implements ActionListener{ //實現此方法,實現窗體上發生點擊按鈕事件後的處理 @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println(e.getSource()); String butName=e.getActionCommand(); if(butName.equals("按鈕1")){ JOptionPane.showMessageDialog(jframe, "按鈕1被點擊"); }else if(butName.equals("按鈕2")){ JOptionPane.showMessageDialog(jframe, "按鈕2被點擊"); }else{ JOptionPane.showMessageDialog(jframe, "UnKnow event"); } } } }
2.通過匿名內部類的方式註冊監聽
代碼示例:
package actionEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * 在這個例子中,利用一個ActionListener來監聽事件源產生的事件 ,用匿名內部類的方式 * 1. 新建一個組件(如JButton)。 * * 2. 將該組件添加到相應的面板(如JPanel)。 * * 3. 註冊監聽器以監聽事件源產生的事件(如通過ActionListener來響應用戶點擊按鈕)。 * * 4. 定義處理事件的方法(如在ActionListener中的actionPerformed中定義相應方法)。 */ public class ActionDemo { private static JFrame jframe; private static JPanel jpanel; private JButton button1; private JButton button2; public static void main(String[] args) { jframe = new JFrame(); new ActionDemo(); jframe.add(jpanel); jframe.pack();// 根據組件大小自適應窗口大小 jframe.setVisible(true);// 窗口可顯示 // 點擊窗口關閉時退出應用程序 jframe.addWindowListener(new WindowAdapter() { public void windowClosing() { System.exit(0); } }); } public ActionDemo() { jpanel = new JPanel();// 實例化面板 button1 = new JButton("按鈕1");// 實例化按鈕 button2 = new JButton("按鈕2"); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(jframe, "按鈕1被點擊"); } }); button2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(jframe, "按鈕2被點擊"); } }); jpanel.add(button1);// 按鈕添加到面板 jpanel.add(button2); } }
3.給每一個組件創建一個監聽器
代碼示例:
package actionEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * 在這個例子中,利用一個ActionListener來監聽事件源產生的事件 ,給每個組件建立一個監聽 * 1. 新建一個組件(如JButton)。 * * 2. 將該組件添加到相應的面板(如JPanel)。 * * 3. 註冊監聽器以監聽事件源產生的事件(如通過ActionListener來響應用戶點擊按鈕)。 * * 4. 定義處理事件的方法(如在ActionListener中的actionPerformed中定義相應方法)。 */ public class ActionDemo { private static JFrame jframe; private static JPanel jpanel; private JButton button1; private JButton button2; public static void main(String[] args) { jframe = new JFrame(); new ActionDemo(); jframe.add(jpanel); jframe.pack();// 根據組件大小自適應窗口大小 jframe.setVisible(true);// 窗口可顯示 // 點擊窗口關閉時退出應用程序 jframe.addWindowListener(new WindowAdapter() { public void windowClosing() { System.exit(0); } }); } public ActionDemo() { jpanel = new JPanel();// 實例化面板 button1 = new JButton("按鈕1");// 實例化按鈕 button2 = new JButton("按鈕2"); button1.addActionListener(new but1Listener()); button2.addActionListener(new but2Listener()); jpanel.add(button1);// 按鈕添加到面板 jpanel.add(button2); } private class but1Listener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(jframe, "按鈕1被點擊"); } } private class but2Listener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(jframe, "按鈕2被點擊"); } } }
總結:這幾種方式各有優缺,但實際差別不大,具體使用哪種根據程序複雜度以及具體場景而定