標題:
事件處理與傾聽者
[打印本頁]
作者:
tonyh
時間:
2022-10-22 19:07
標題:
事件處理與傾聽者
本帖最後由 tonyh 於 2022-10-22 20:09 編輯
視窗作業系統都是採取圖形使用者介面,其程式執行流程是採用事件驅動(Event Driver)方式運作。例如程式開始執行後,等待著事件的發生,如移動滑鼠到按鈕上點按一下,就可能會執行特定方法。
Java對於事件處理方式是採用「委派事件模式」,如下圖所示:
Java將產生事件的物件稱為「事件來源」,而接收事件的物件稱為「事件傾聽者」,處理事件的方法稱為「事件處理方法」。
以本範例為例,事件來源有 tf1、btn1、btn2 三個物件,這三個物件必須分別與事件傾聽者連結在一起(即為來源物件註冊傾聽者),當事件產生時,會將來源物件以傳遞參數的方式交給事件處理方法運作。
事件處理的傾聽者介面 ActionListener 由 java.awt.event 套件所提供,使用時要先匯入。
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 Ch81 implements ActionListener{
JFrame fm;
JLabel lb1, lb2;
JTextField tf1, tf2;
JButton btn1, btn2;
Ch81() //建構子
{
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);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn2=new JButton("清除");
btn1.setBounds(10, 135, 92, 25);
btn2.setBounds(112, 135, 92, 25);
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);
btn1.addActionListener(this);
btn2.addActionListener(this);
tf1.addActionListener(this);
}
public static void main(String[] args) {
new Ch81();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1 || e.getSource()==tf1)
{
double area=Double.parseDouble(tf1.getText())*3.3058;
/*tf2.setText(String.valueOf(area));
tf2.setText(Double.toString(area));
tf2.setText(area+"");*/
tf2.setText("面積為:"+area+"平方公尺");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
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();
}
}
複製代碼
作者:
余柏緯
時間:
2022-10-22 19:41
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 C511 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
C511()
{
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) {
C511 app=new C511();
}
}
複製代碼
作者:
王秉鈞
時間:
2022-10-22 20:08
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.*;;
import java.awt.event.ActionEvent;
public class Ch39 implements ActionListener{
private JFrame fm;
private JLabel lb1,lb2;
private JTextField tf1,tf2;
private JButton btn1,btn2;
Ch39()
{
lb1=new JLable("1坪=3.3058cm*cm",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.addActionListener(false);
btn1=new JButton("yes");
btn1.setBounds(10, 135, 92, 25);
btn1.addActionListener(this);
btn2=new JButton("no");
btn2.setBounds(112, 135, 92, 25);
btn2.addActionListener(this);
fm=new JFrame("ground cm*cm count");
fm.setBounds(100, 100, 220, 200);
fm.setVisible(true);
fm.Resizeable(false);
fm.Layout(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 sresStr=String.valueOf(area);
tf2.setText("cm*cm="+areaStr+"cm*cm");
}
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
}
public static void main(String[] args)
{
Ch39 app=new Ch39();
}
}
複製代碼
作者:
黃子倢
時間:
2022-10-22 20:09
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 P3 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
P3()
{
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);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
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);
tf1.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
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) {
P3 app=new P3();
}
}
複製代碼
作者:
張博竣
時間:
2022-10-22 20:14
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 Ch72 implements ActionListener
{
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Ch72()
{
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) {
Ch72 app=new Ch72();
}
}
複製代碼
作者:
許志捷
時間:
2022-10-22 20:14
package lol;
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 Main implements ActionListener{
JFrame fm;
JLabel lb1;
JLabel lb2;
JButton bt1;
JButton bt2;
JTextField tf1;
JTextField tf2;
void area()
{
fm=new JFrame("土地面積計算");
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb2=new JLabel("輸入坪數:");
bt1=new JButton("確定");
bt2=new JButton("清除");
tf1=new JTextField();
tf2=new JTextField();
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);
bt1.setBounds(10, 135, 92, 25);
bt1.addActionListener(this);
bt2.setBounds(112, 135, 92, 25);
bt2.addActionListener(this);
fm.setBounds(100,100,220,200);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.setVisible(true);
fm.add(lb1);
fm.add(lb2);
fm.add(bt1);
fm.add(bt2);
fm.add(tf1);
fm.add(tf2);
}
Main()
{
area();
}
public static void main(String[] args) {
new Main();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==tf1||e.getSource()==bt1)
{
double a=Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為: "+a+"平方公尺");
}
if(e.getSource()==bt2)
{
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
丁肇志
時間:
2022-10-22 20:15
import java.awt.event.*;
import javax.swing.*;
public class Ch81 implements ActionListener {
JFrame fm;
JLabel lb1, lb2;
JTextField tf1, tf2;
JButton bt1, bt2;
void init() {
lb1 = new JLabel("1坪=3.3058平方公尺", JLabel.CENTER);
lb2 = new JLabel("輸入坪數:");
lb1.setBounds(0, 10, 215, 30);
lb2.setBounds(10, 40, 60, 40);
tf1 = new JTextField();
tf2 = new JTextField();
tf1.setBounds(70, 45, 134, 30);
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
bt1 = new JButton("確定");
bt2 = new JButton("清除");
bt1.setBounds(10, 135, 92, 25);
bt2.setBounds(112, 135, 92, 25);
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(bt1);
fm.add(bt2);
}
void act() {
bt1.addActionListener(this);
bt2.addActionListener(this);
tf1.addActionListener(this);
}
Ch81() {
init();
act();
}
public static void main(String[] args) {
new Ch81();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == bt1 || e.getSource() == tf1) {
Double area;
area = Double.parseDouble(tf1.getText())*3.3058;
tf2.setText("面積為:" + area + "平方公尺");
}
else if (e.getSource() == bt2) {
tf1.setText("");
tf2.setText("");
}
}
}
複製代碼
作者:
許洧熏
時間:
2022-10-22 20:20
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 Ch01 implements ActionListener {
private JFrame fm;
private JLabel lb1, lb2;
private JTextField tf1, tf2;
private JButton btn1, btn2;
Ch01()
{
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.addActionListener(this);
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);
}
@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) {
Ch01 app=new Ch01();
}
}
複製代碼
作者:
曾元瑜
時間:
2022-10-22 20:21
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Ch81 implements ActionListener {
JTextField t1, t2;
JLabel lb1,lb2;
JButton btn1,btn2;
JFrame fm;
void initialize(){
t1=new JTextField();
t2=new JTextField();
t2.setEditable(false);
t1.setBounds(70, 45, 134, 30);
t2.setBounds(10, 85, 195, 40);
lb1=new JLabel("1坪=3.3058平方公尺",JLabel.CENTER);
lb1.setBounds(0, 10, 215, 30);
lb2=new JLabel("輸入坪數:");
lb2.setBounds(10, 40, 60, 40);
btn1=new JButton("確定");
btn1.setBounds(10, 135, 92, 25);
btn2=new JButton("清除");
btn2.setBounds(112, 135, 92, 25);
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(t1);
fm.add(t2);
fm.add(btn1);
fm.add(btn2);
}
void act(){
btn1.addActionListener(this);
btn2.addActionListener(this);
t1.addActionListener(this);
}
Ch81(){
initialize();
act();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==t1 || e.getSource()==btn1)
{
double area=Double.parseDouble(t1.getText())*3.3058;
String areaStr=String.valueOf(area);
t2.setText("面積為: "+areaStr+" 平方公尺");
}
if(e.getSource()==btn2)
{
t1.setText("");
t2.setText("");
}
}
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
Ch81 app = new Ch81();
}
}
複製代碼
作者:
王法棣
時間:
2022-10-22 20:26
package ch07;
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 Ch81 implements ActionListener{
JFrame fm;
JLabel lb1, lb2;
JTextField tf1, tf2;
JButton btn1, btn2;
void initialize()
{
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);
tf2=new JTextField();
tf2.setBounds(10, 85, 195, 40);
tf2.setEditable(false);
tf1.addActionListener(this);
btn1=new JButton("確定");
btn2=new JButton("清除");
btn1.setBounds(10, 135, 92, 25);
btn2.setBounds(112, 135, 92, 25);
btn1.addActionListener(this);
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);
}
Ch81() //建構子
{
initialize();
}
public static void main(String[] args) {
new Ch81();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn2)
{
tf1.setText("");
tf2.setText("");
}
if(e.getSource()==btn1 || e.getSource()==tf1)
{
tf2.setText("面積為"+(Double.parseDouble(tf1.getText())*3.3058)+"公尺");
}
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2