標題:
事件處理與傾聽者
[打印本頁]
作者:
tonyh
時間:
2021-8-6 20:13
標題:
事件處理與傾聽者
視窗作業系統都是採取圖形使用者介面,其程式執行流程是採用事件驅動(Event Driver)方式運作。例如程式開始執行後,等待著事件的發生,如移動滑鼠到按鈕上點按一下,就可能會執行特定方法。
Java對於事件處理方式是採用「委派事件模式」,如下圖所示:
Java將產生事件的物件稱為「事件來源」,而接收事件的物件稱為「事件傾聽者」,處理事件的方法稱為「事件處理方法」。
以本範例為例,事件來源有 tf1、btn1、btn2 三個物件,這三個物件必須分別與事件傾聽者連結在一起(即為來源物件註冊傾聽者),當事件產生時,會將來源物件以傳遞參數的方式交給事件處理方法運作。
事件處理的傾聽者介面 ActionListener 由 java.awt.event 套件所提供,使用時要先匯入。
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ch82 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Ch82()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double area=Double.parseDouble(tf1.getText())*3.3058;
String areaStr=String.valueOf(area);
//String areaStr=Double.toString(area);
tf2.setText("面積為: "+areaStr+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args) {
Ch82 app=new Ch82();
}
}
複製代碼
作者:
陳宥穎
時間:
2021-8-6 20:44
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Ch100 implements ActionListener {
private JFrame fram1;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton bt1, bt2;
Ch100()
{
bt1=new JButton("確定");
bt1.setBounds(10, 135, 92, 25);
bt1.addActionListener(this);
bt2=new JButton("清除");
bt2.setBounds(112, 135, 92, 25);
bt2.addActionListener(this);
lb1=new JLabel("1坪=3.3058平方公尺");
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
fram1=new JFrame("土地面積計算");
fram1.add(bt1);
fram1.add(bt2);
fram1.add(lb1);
fram1.add(lb2);
fram1.add(tf1);
fram1.add(tf2);
fram1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fram1.setLayout(null);
fram1.setBounds(100, 100, 220, 200);
fram1.setVisible(true);
}
public static void main(String[] args) {
Ch100 haha=new Ch100();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt1 || e.getSource()==tf1)
{
double x=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為"+x+"平方公尺");
}
if(e.getSource()==bt2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
林政瑜
時間:
2021-8-6 20:46
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Ch02 implements ActionListener{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Ch02()
{
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public static void main(String[] args) {
Ch02 app=new Ch02();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1 || e.getSource()==btn1) {
double ans=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為:"+ans+"平方公尺");
}
if(e.getSource()==btn2) {
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
沈子晏
時間:
2021-8-6 20:46
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Ch006 implements ActionListener{
JFrame fm;
JTextField tf1, tf2;
JLabel lb1, lb2;
JButton btn1, btn2;
Ch006() //建構子
{
tf1=new JTextField();
tf2=new JTextField();
lb1=new JLabel("1坪=3.3085平方公尺", JLabel.CENTER);
lb2=new JLabel("輸入坪數:");
fm=new JFrame("土地面積換算");
btn1=new JButton("確定");
btn2=new JButton("清除");
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2.setBounds(10, 85, 195, 40);
tf2.setEnabled(false);
lb1.setBounds(0, 10, 215, 30);
lb2.setBounds(10, 40, 60, 40);
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public static void main(String[] args) {
Ch006 app=new Ch006(); //物件導向化(匿名物件)
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double res=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為: "+res+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
孫嘉駿
時間:
2021-8-6 20:46
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Ch11 implements ActionListener{
JFrame fm;
JLabel lb1, lb2;
JTextField tf1, tf2;
JButton btn1, btn2;
Ch11(){
lb1=new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數: ");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setEditable(false);
tf2.setBounds(10, 85, 195, 40);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積換算");
fm.setBounds(100, 100, 220, 220);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
fm.add(fm);
}
public static void main(String[] args) {
new Ch11();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double area=Double.parseDouble(tf1.getText())*3.3058;
String areaStr=String.valueOf(area);
tf2.setText("面積為: "+areaStr+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
黃宥華
時間:
2021-8-6 20:51
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Ch13 implements ActionListener{
JFrame fm;
JLabel lb1, lb2;
JTextField tf1, tf2;
JButton btn1, btn2;
Ch13()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public static void main(String[] args) {
Ch13 app=new Ch13();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double pig=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為: "+pig+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
李宇澤
時間:
2021-8-6 20:54
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ch69 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Ch69()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double area=Double.parseDouble(tf1.getText())*3.3058;
String areaStr=String.valueOf(area);
//String areaStr=Double.toString(area);
tf2.setText("面積為: "+areaStr+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args) {
Ch69 app=new Ch69();
}
}
複製代碼
作者:
蔡忻霓
時間:
2021-8-6 20:54
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class JPA03 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
JPA03()
{
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
fm.add(btn1);
fm.add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double area=Double.parseDouble(tf1.getText())*3.3058;
String areaStr=String.valueOf(area);
tf2.setText("面積為: "+areaStr+" 平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args) {
JPA03 app=new JPA03();
}
}
複製代碼
作者:
董宸佑
時間:
2021-8-6 20:57
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Ch01 implements ActionListener{
private JFrame fm=new JFrame("土筆面積換算");
private JLabel lb1=new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
private JLabel lb2=new JLabel("輸入坪數:");
private JTextField tf1=new JTextField();
private JTextField tf2=new JTextField();
private JButton btn1=new JButton("確定");
private JButton btn2=new JButton("清除");
Ch01(){
lb1.setBounds(0, 10, 215, 30);
lb2.setBounds(10, 40, 60, 40);
tf1.setBounds(70, 45, 134, 30);
tf1.addActionListener(this);
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(btn1);
fm.add(btn2);
fm.add(tf1);
fm.add(tf2);
fm.add(lb1);
fm.add(lb2);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1 || e.getSource()==btn1){
double area=Double.parseDouble(tf1.getText())*3.3058;
String areaStr=String.valueOf(area);
tf2.setText("面積為: "+areaStr+"平方公尺");
}
if(e.getSource()==btn2){
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
劉愷鈞
時間:
2021-8-6 20:58
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Ch01 implements ActionListener {
JFrame fm;
JLabel lb1,lb2;
JTextField tf1,tf2;
JButton btn1,btn2;
Ch01()
{
lb1=new JLabel("1坪=3.358平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb1.setVisible(true);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
lb2.setVisible(true);
tf1=new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf1.setVisible(true);
tf1.addActionListener(this);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(true);
tf2.setVisible(true);
tf2.addActionListener(this);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn1.setVisible(true);
btn1.addActionListener(this);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
btn2.setVisible(true);
btn2.addActionListener(this);
fm=new JFrame("土地面積計算");
fm.setBounds(100,100,233,233);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(btn1);
fm.add(btn2);
fm.add(lb1);
fm.add(lb2);
fm.add(tf1);
fm.add(tf2);
}
public static void main(String args[])
{
new Ch01();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1 || e.getSource()==btn1)
{
double result=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為:"+result+"平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2