返回列表 發帖

事件處理與傾聽者

本帖最後由 tonyh 於 2021-8-6 11:36 編輯

視窗作業系統都是採取圖形使用者介面,其程式執行流程是採用事件驅動(Event Driver)方式運作。例如程式開始執行後,等待著事件的發生,如移動滑鼠到按鈕上點按一下,就可能會執行特定方法。

Java對於事件處理方式是採用「委派事件模式」,如下圖所示:



Java將產生事件的物件稱為「事件來源」,而接收事件的物件稱為「事件傾聽者」,處理事件的方法稱為「事件處理方法」。

以本範例為例,事件來源有 tf1、btn1、btn2 三個物件,這三個物件必須分別與事件傾聽者連結在一起(即為來源物件註冊傾聽者),當事件產生時,會將來源物件以傳遞參數的方式交給事件處理方法運作。

事件處理的傾聽者介面 ActionListener 由 java.awt.event 套件所提供,使用時要先匯入。

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.JTextField;
  4. import javax.swing.JButton;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. public class Ch82 implements ActionListener
  8. {
  9.        
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.        
  15.         Ch82()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(this);
  25.                 tf2=new JTextField();
  26.                 tf2.setBounds(10, 85, 195, 40);
  27.                 tf2.setEditable(false);
  28.                
  29.                 btn1=new JButton("確定");
  30.                 btn1.setBounds(10, 135, 92, 25);
  31.                 btn1.addActionListener(this);
  32.                 btn2=new JButton("清除");
  33.                 btn2.setBounds(112, 135, 92, 25);
  34.                 btn2.addActionListener(this);
  35.                
  36.                 fm=new JFrame("土地面積計算");
  37.                 fm.setBounds(100, 100, 220, 200);
  38.                 fm.setVisible(true);
  39.                 fm.setResizable(false);
  40.                 fm.setLayout(null);
  41.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 fm.add(lb1);
  43.                 fm.add(lb2);
  44.                 fm.add(tf1);
  45.                 fm.add(tf2);
  46.                 fm.add(btn1);
  47.                 fm.add(btn2);
  48.         }
  49.        
  50.         public void actionPerformed(ActionEvent e)
  51.         {
  52.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  53.                 {
  54.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  55.                         String areaStr=String.valueOf(area);
  56.                         //String areaStr=Double.toString(area);
  57.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  58.                 }       
  59.                 if(e.getSource()==btn2)
  60.                 {
  61.                         tf1.setText("");
  62.                         tf2.setText("");
  63.                 }
  64.         }

  65.         public static void main(String[] args) {
  66.                 Ch82 app=new Ch82();
  67.         }

  68. }
複製代碼

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.JTextField;
  4. import javax.swing.JButton;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. public class Ch82 implements ActionListener
  8. {
  9.         
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.         
  15.         Ch82()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(this);
  25.                 tf2=new JTextField();
  26.                 tf2.setBounds(10, 85, 195, 40);
  27.                 tf2.setEditable(false);
  28.                
  29.                 btn1=new JButton("確定");
  30.                 btn1.setBounds(10, 135, 92, 25);
  31.                 btn1.addActionListener(this);
  32.                 btn2=new JButton("清除");
  33.                 btn2.setBounds(112, 135, 92, 25);
  34.                 btn2.addActionListener(this);
  35.                
  36.                 fm=new JFrame("土地面積計算");
  37.                 fm.setBounds(100, 100, 220, 200);
  38.                 fm.setVisible(true);
  39.                 fm.setResizable(false);
  40.                 fm.setLayout(null);
  41.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 fm.add(lb1);
  43.                 fm.add(lb2);
  44.                 fm.add(tf1);
  45.                 fm.add(tf2);
  46.                 fm.add(btn1);
  47.                 fm.add(btn2);
  48.         }
  49.         
  50.         public void actionPerformed(ActionEvent e)
  51.         {
  52.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  53.                 {
  54.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  55.                         String areaStr=String.valueOf(area);
  56.                         //String areaStr=Double.toString(area);
  57.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  58.                 }        
  59.                 if(e.getSource()==btn2)
  60.                 {
  61.                         tf1.setText("");
  62.                         tf2.setText("");
  63.                 }
  64.         }

  65.         public static void main(String[] args) {
  66.                 Ch82 app=new Ch82();
  67.         }

  68. }
複製代碼
高睿辰是幹話之王

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.JTextField;
  4. import javax.swing.JLabel;

  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. public class Ch81 implements ActionListener
  8. {
  9.         private JFrame fm;
  10.         private JButton btn1, btn2;
  11.         private JTextField tf1, tf2;
  12.         private JLabel lb1, lb2;
  13.        
  14.                 Ch81()
  15.                 {
  16.                          fm=new JFrame("...");
  17.              lb1=new JLabel("1 坪 = 3.3058 m2",JLabel.CENTER);
  18.              lb2=new JLabel("Enter 坪數: ");
  19.              btn1=new JButton("Enter");
  20.              btn2=new JButton("Clear");
  21.              tf1=new JTextField();
  22.              tf2=new JTextField();
  23.             
  24.              tf1.setBounds(70, 45, 134, 30);
  25.              tf1.addActionListener(this);
  26.              tf2.setBounds(10, 85, 195, 40);
  27.              tf2.setEditable(false);
  28.              lb1.setBounds(0, 10, 215, 30);
  29.              lb2.setBounds(10, 40, 60, 40);
  30.              btn1.setBounds(10, 135, 92, 25);
  31.              btn1.addActionListener(this);
  32.              btn2.setBounds(112, 135, 92, 25);
  33.              btn2.addActionListener(this);
  34.             
  35.              fm.setBounds(100, 100, 220, 200);
  36.              fm.setVisible(true);
  37.              fm.setResizable(false);
  38.              fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.              fm.setLayout(null);
  40.              fm.add(btn1);
  41.              fm.add(btn2);
  42.              fm.add(tf1);
  43.              fm.add(tf2);
  44.              fm.add(lb1);
  45.              fm.add(lb2);   
  46.                 }
  47.          
  48.                 public void actionPerformed(ActionEvent e)
  49.             {
  50.                     if(e.getSource()==btn2)
  51.                     {
  52.                             tf1.setText("");
  53.                             tf2.setText("");
  54.                     }
  55.                     if(e.getSource()==btn1 || e.getSource()==tf1)
  56.                     {
  57.                             double area=Double.parseDouble(tf1.getText())*3.3058;               
  58.                             String areaStr=String.valueOf(area);
  59.                 tf2.setText("面積為: "+areaStr+" 平方公尺");
  60.                     }
  61.             
  62.             }
  63.                
  64.                
  65.      public static void main(String[] args) {
  66.          Ch81 app=new Ch81();
  67.    
  68.            }
  69. }
複製代碼

TOP

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;
  7. public class Ch76 implements ActionListener
  8. {
  9.         JFrame fm;
  10.         JTextField tf2, tf1;
  11.         JLabel lb1, lb2;
  12.         JButton btn1, btn2;
  13.        
  14.             Ch76()
  15.             {
  16.                     tf1=new JTextField();
  17.                         tf1.setBounds(70, 45, 134, 30);
  18.                         tf1.addActionListener(this);
  19.                         tf2=new JTextField();
  20.                         tf2.setBounds(10, 85, 195, 40);
  21.             tf2.setEditable(false);
  22.                         tf2.addActionListener(this);
  23.                        
  24.                         lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  25.                         lb1.setBounds(0, 10, 215, 30);
  26.             lb2=new JLabel("輸入坪數:");
  27.                         lb2.setBounds(10, 40, 60, 40);
  28.                        
  29.                         btn1=new JButton("確定");
  30.                         btn1.setBounds(10, 135, 92, 25);
  31.                         btn1.addActionListener(this);
  32.                         btn2=new JButton("清除");
  33.                         btn2.setBounds(112, 135, 92, 25);
  34.                         btn2.addActionListener(this);

  35.                         fm=new JFrame("單位換算");
  36.             fm.setBounds(100, 100, 220, 200);
  37.             fm.setVisible(true);
  38.             fm.setResizable(false);
  39.             fm.setLayout(null);
  40.             fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  41.             fm.add(lb1);
  42.             fm.add(lb2);
  43.             fm.add(tf1);
  44.             fm.add(tf2);
  45.             fm.add(btn1);
  46.             fm.add(btn2);

  47.             }
  48.                 public static void main(String[] args)
  49.                 {
  50.                         Ch76 app=new Ch76();
  51.         }
  52.                 public void actionPerformed(ActionEvent e)
  53.                 {
  54.                         if(e.getSource()==btn2)
  55.                         {
  56.                                 tf1.setText("");
  57.                                 tf2.setText("");
  58.                         }
  59.                         if(e.getSource()==btn1 || e.getSource()==tf1)
  60.                         {
  61.                                 Double area=Double.parseDouble(tf1.getText())*3.3058;
  62.                 tf2.setText("面積為: "+area+" 平方公尺");
  63.                         }
  64.                 }

  65. }
複製代碼

TOP

  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;

  3. import javax.swing.JButton;
  4. import javax.swing.JFrame;
  5. import javax.swing.JLabel;
  6. import javax.swing.JTextField;
  7. public class Ch82 implements ActionListener{
  8.         JLabel lb1,lb2;
  9.         JTextField tf1,tf2;
  10.         JButton btn1,btn2;
  11.         JFrame fm;
  12.         Ch82() {
  13.                 lb1 = new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  14.                 lb1.setBounds(0, 10, 215, 30);
  15.                 lb2 = new JLabel("輸入坪數:");
  16.                 lb2.setBounds(10, 40, 60, 40);
  17.                
  18.                 tf1 = new JTextField();
  19.                 tf1.setBounds(70, 45, 134, 30);
  20.                 tf1.addActionListener(this);
  21.                 tf2 = new JTextField();
  22.                 tf2.setBounds(10, 85, 195, 40);
  23.                 tf2.setEditable(false);
  24.                
  25.                 btn1 = new JButton("確定");
  26.                 btn1.setBounds(10, 135, 92, 25);
  27.                 btn1.addActionListener(this);
  28.                 btn2 = new JButton("清除");
  29.                 btn2.setBounds(112, 135, 92, 25);
  30.                 btn2.addActionListener(this);
  31.                
  32.                 fm = new JFrame("土地面積計算");
  33.                 fm.setBounds(100, 100, 220, 200);
  34.                 fm.setVisible(true);
  35.                 fm.setResizable(false);
  36.                 fm.setLayout(null);
  37.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  38.                 fm.add(lb1);
  39.                 fm.add(lb2);
  40.                 fm.add(tf1);
  41.                 fm.add(tf2);
  42.                 fm.add(btn1);
  43.                 fm.add(btn2);
  44.         }
  45.        
  46.         public static void main(String[] args) {
  47.                 Ch82 app = new Ch82();
  48.         }

  49.         @Override
  50.         public void actionPerformed(ActionEvent e) {
  51.                  if(e.getSource()==tf1 || e.getSource()==btn1)
  52.          {
  53.                  double area=Double.parseDouble(tf1.getText())*3.3058;
  54.                  tf2.setText("面積為: "+area+" 平方公尺");
  55.          }        
  56.          if(e.getSource()==btn2)
  57.          {
  58.                  tf1.setText("");
  59.                  tf2.setText("");
  60.          }
  61.                
  62.         }

  63. }
複製代碼

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.JTextField;
  4. import javax.swing.JButton;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. public class Ch75 implements ActionListener
  8. {
  9.         
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.         
  15.         Ch75()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(this);
  25.                 tf2=new JTextField();
  26.                 tf2.setBounds(10, 85, 195, 40);
  27.                 tf2.setEditable(false);
  28.                
  29.                 btn1=new JButton("確定");
  30.                 btn1.setBounds(10, 135, 92, 25);
  31.                 btn1.addActionListener(this);
  32.                 btn2=new JButton("清除");
  33.                 btn2.setBounds(112, 135, 92, 25);
  34.                 btn2.addActionListener(this);
  35.                
  36.                 fm=new JFrame("土地面積計算");
  37.                 fm.setBounds(100, 100, 220, 200);
  38.                 fm.setVisible(true);
  39.                 fm.setResizable(false);
  40.                 fm.setLayout(null);
  41.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 fm.add(lb1);
  43.                 fm.add(lb2);
  44.                 fm.add(tf1);
  45.                 fm.add(tf2);
  46.                 fm.add(btn1);
  47.                 fm.add(btn2);
  48.         }
  49.         
  50.         public void actionPerformed(ActionEvent e)
  51.         {
  52.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  53.                 {
  54.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  55.                         String areaStr=String.valueOf(area);
  56.                        
  57.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  58.                 }        
  59.                 if(e.getSource()==btn2)
  60.                        
  61.                 {
  62.                         tf1.setText("");
  63.                         tf2.setText("");
  64.                         
  65.                 }
  66.         }

  67.         public static void main(String[] args) {
  68.                 Ch75 app=new Ch75();
  69.                
  70.         }

  71. }
複製代碼
七日不見 如隔一周

TOP

  1. import javax.swing.JFrame;
  2. import javax.swing.JLabel;
  3. import javax.swing.JTextField;
  4. import javax.swing.JButton;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.ActionEvent;
  7. public class Ch82 implements ActionListener
  8. {
  9.         
  10.         private JFrame fm;
  11.         private JLabel lb1, lb2;
  12.         private JTextField tf1, tf2;
  13.         private JButton btn1, btn2;
  14.         
  15.         Ch82()
  16.         {
  17.                 lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
  18.                 lb1.setBounds(0, 10, 215, 30);
  19.                 lb2=new JLabel("輸入坪數:");
  20.                 lb2.setBounds(10, 40, 60, 40);
  21.                
  22.                 tf1=new JTextField();
  23.                 tf1.setBounds(70, 45, 134, 30);
  24.                 tf1.addActionListener(this);
  25.                 tf2=new JTextField();
  26.                 tf2.setBounds(10, 85, 195, 40);
  27.                 tf2.setEditable(false);
  28.                
  29.                 btn1=new JButton("確定");
  30.                 btn1.setBounds(10, 135, 92, 25);
  31.                 btn1.addActionListener(this);
  32.                 btn2=new JButton("清除");
  33.                 btn2.setBounds(112, 135, 92, 25);
  34.                 btn2.addActionListener(this);
  35.                
  36.                 fm=new JFrame("土地面積計算");
  37.                 fm.setBounds(100, 100, 220, 200);
  38.                 fm.setVisible(true);
  39.                 fm.setResizable(false);
  40.                 fm.setLayout(null);
  41.                 fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  42.                 fm.add(lb1);
  43.                 fm.add(lb2);
  44.                 fm.add(tf1);
  45.                 fm.add(tf2);
  46.                 fm.add(btn1);
  47.                 fm.add(btn2);
  48.         }
  49.         
  50.         public void actionPerformed(ActionEvent e)
  51.         {
  52.                 if(e.getSource()==tf1 || e.getSource()==btn1)
  53.                 {
  54.                         double area=Double.parseDouble(tf1.getText())*3.3058;
  55.                         String areaStr=String.valueOf(area);
  56.                         //String areaStr=Double.toString(area);
  57.                         tf2.setText("面積為: "+areaStr+" 平方公尺");
  58.                 }        
  59.                 if(e.getSource()==btn2)
  60.                 {
  61.                         tf1.setText("");
  62.                         tf2.setText("");
  63.                 }
  64.         }

  65.         public static void main(String[] args) {
  66.                 Ch82 app=new Ch82();
  67.         }

  68. }
複製代碼

TOP

返回列表