java處理按鈕點(diǎn)擊事件的方法
不同的事件源可以產(chǎn)生不同類別的事件。例如,按鈕可以發(fā)送一個(gè)ActionEvent對(duì)象,而窗口可以發(fā)送WindowEvent對(duì)象。
AWT時(shí)間處理機(jī)制的概要:
1. 監(jiān)聽器對(duì)象是一個(gè)實(shí)現(xiàn)了特定監(jiān)聽器接口(listener interface)的類的實(shí)例。
2. 事件源是一個(gè)能夠注冊(cè)監(jiān)聽器對(duì)象并發(fā)送事件對(duì)象的對(duì)象。
3. 當(dāng)事件發(fā)生時(shí),事件源將事件對(duì)象傳遞給所有注冊(cè)的監(jiān)聽器。
4. 監(jiān)聽器對(duì)象將利用事件對(duì)象中的信息決定如何對(duì)事件做出響應(yīng)。
下面是監(jiān)聽器的一個(gè)示例:
ActionListener listener = ...; JButton button = new JButton("OK"); button.addActionListener(listener);
現(xiàn)在,只要按鈕產(chǎn)生了一個(gè)“動(dòng)作事件”,listener對(duì)象就會(huì)得到通告。對(duì)于按鈕來說,正像我們想到的,動(dòng)作事件就是點(diǎn)擊按鈕。
為了實(shí)現(xiàn)ActionListener接口,監(jiān)聽器類必須有一個(gè)被稱為actionPerformed的方法,該方法接收一個(gè)ActionEvent對(duì)象參數(shù)。
class MyListener implements ActionListener { ...; public void actionPerformed(ActionEvent event) { //reaction to button click goes here } }
只要用戶點(diǎn)擊了按鈕,JButton對(duì)象就會(huì)創(chuàng)建一個(gè)ActionEvent對(duì)象,然后調(diào)用listener.actionPerformed(event)傳遞事件對(duì)象??梢詫⒍鄠€(gè)監(jiān)聽器對(duì)象添加到一個(gè)像按鈕這樣的事件源中。這樣一來,只要用戶點(diǎn)擊按鈕,按鈕就會(huì)調(diào)用所有監(jiān)聽器的actionPerformed方法。
實(shí)例:處理按鈕點(diǎn)擊事件
為了加深對(duì)事件委托模型的理解,下面以一個(gè)響應(yīng)按鈕點(diǎn)擊事件的簡單示例來說明所需要知道的細(xì)節(jié)。在這個(gè)示例中,想要在一個(gè)面板中放置三個(gè)按鈕,添加三個(gè)監(jiān)聽器對(duì)象用來作為按鈕的動(dòng)作監(jiān)聽器。
在這個(gè)情況下,只要用戶點(diǎn)擊面板上的任何一個(gè)按鈕,相關(guān)的監(jiān)聽器對(duì)象就會(huì)接收到一個(gè)ActionEvent對(duì)象,它表示有個(gè)按鈕被點(diǎn)擊了。在示例程序中,監(jiān)聽器對(duì)象將改變面板的背景顏色。
在演示如何監(jiān)聽按鈕點(diǎn)擊事件之前,首先需要講解一下如何創(chuàng)建按鈕以及如何將他們添加到面板中。
可以通過在按鈕構(gòu)造器中指定一個(gè)標(biāo)簽字符串、一個(gè)圖標(biāo)或兩項(xiàng)都指定來創(chuàng)建一個(gè)按鈕。下面是兩個(gè)示例:
JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton(new ImageIcon("blue-ball.gif"));
將按鈕添加到面板中需要調(diào)用add方法:
JButton yellowButton = new JButton("Yellow"); JButton blueButton = new JButton("Blue"); JButton redButton = new JButton("Red"); buttonPanel.add(yellowButton); buttonPanel.add(blueButton); buttonPanel.add(redButton);
至此,知道了如何將按鈕添加到面板上,接下來需要增加讓面板監(jiān)聽這些按鈕的代碼。這需要一個(gè)實(shí)現(xiàn)了ActionListener接口的類。如前所述,應(yīng)該包含一個(gè)actionPerformed方法,其簽名為:
public void actionPerformed(ActionEvent event)
當(dāng)按鈕被點(diǎn)擊時(shí),希望將面板的背景顏色設(shè)置為指定的顏色。這個(gè)顏色存儲(chǔ)在監(jiān)聽器類中:
class ColorAction implements ActionListener { public ColorAction(Color c) { backgroundColor = c; } public void actionPerformed(actionEvent event) { //set panel background color } private Color backgroundColor; }
然后,為每種顏色構(gòu)造一個(gè)對(duì)象,并將這些對(duì)象設(shè)置為按鈕監(jiān)聽器。
ColorAction yelloAction = new ColorAction(Color.YELLOW); ColorAction blueAction = new ColorAction(Color.BLUE); ColorAction redAction = new ColorAction(Color.RED); yellowButton.addActionListener(yellowAction); blueButton.addActionListener(blueAction); redButton.addActionListener(redAction);
例如,如果一個(gè)用戶在標(biāo)有“Yellow”的按鈕上點(diǎn)擊了一下,yellowAction對(duì)象的actionPerformed方法就會(huì)被調(diào)用。這個(gè)對(duì)象的backgroundColor實(shí)例域被設(shè)置為Color.YELLOW,現(xiàn)在就將面板的背景顏色設(shè)置為黃色。
這里還有一個(gè)需要考慮的問題。ColorAction對(duì)象不能訪問buttonpanel變量。可以采用兩種方式解決這個(gè)問題。一個(gè)是將面板存儲(chǔ)在ColorAction對(duì)象中,并在ColorAction的構(gòu)造器中設(shè)置它;另一個(gè)是將ColorAction作為ButtonPanel類的內(nèi)部類,如此,它的方法就自動(dòng)地?fù)碛性L問外部面板的權(quán)限了。
下面說明一下如何將ColorAction類放在ButtonFrame類內(nèi)。
class ButtonFrame extends JFrame { ... private class ColorAction implents ActionListener { ... public void actionPerformed(ActionEvent event) { buttonPanel.setBackground(backgroundColor); } private Color backgroundColor; } private Jpanel buttonPanel; }
以上這篇java處理按鈕點(diǎn)擊事件的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決MyEclipse6.5無法啟動(dòng),一直停留剛開始啟動(dòng)界面的詳解
本篇文章是對(duì)解決MyEclipse6.5無法啟動(dòng),一直停留剛開始啟動(dòng)界面的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Java實(shí)現(xiàn)同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)同步枚舉類數(shù)據(jù)到數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08一文教會(huì)你用mybatis查詢數(shù)據(jù)庫數(shù)據(jù)
MyBatis本身是一個(gè)數(shù)據(jù)庫連接框架,可以認(rèn)為是JDBC的升級(jí)版,下面這篇文章主要給大家介紹了關(guān)于mybatis查詢數(shù)據(jù)庫數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04Springboot公共字段填充及ThreadLocal模塊改進(jìn)方案
這篇文章主要為大家介紹了Springboot公共字段填充及ThreadLocal模塊改進(jìn)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Spring Boot如何使用JDBC獲取相關(guān)的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于Spring Boot如何使用JDBC獲取相關(guān)數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03