java常見(jiàn)事件響應(yīng)方法實(shí)例匯總
本文實(shí)例匯總了java中常見(jiàn)的事件響應(yīng)方法,包括容器類監(jiān)聽(tīng)、監(jiān)聽(tīng)器類、AbstractAction、反射等。以方便大家參考。具體方法如下:
首先,在Java圖形用戶界面中,處理事件時(shí)所必須的步驟是:
1、創(chuàng)建接受響應(yīng)的組件(控件)
2、實(shí)現(xiàn)相關(guān)事件監(jiān)聽(tīng)接口
3、注冊(cè)事件源的動(dòng)作監(jiān)聽(tīng)器
4、事件觸發(fā)時(shí)的事件處理
相應(yīng)的可以通過(guò)以下的集中方式來(lái)作出事件響應(yīng)。
一、容器類監(jiān)聽(tīng)
效果:?jiǎn)螕舸绑w中的三個(gè)按鈕,實(shí)現(xiàn)相應(yīng)的相應(yīng)時(shí)間。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//聲明 類時(shí),添加“implements ActionListener”實(shí)現(xiàn)監(jiān)聽(tīng)的接口,如果要對(duì)多種監(jiān)聽(tīng)方式進(jìn)行監(jiān)聽(tīng),則用逗號(hào)間隔開(kāi)
// 比如“implements ActionListener,KeyListener”
class ButtonListener extends JFrame implements ActionListener{
JButton ok, cancel,exit; //創(chuàng)建接受響應(yīng)的組建,就是三個(gè)按鈕
public ButtonListener(String title){
super(title);
this.setLayout(new FlowLayout());
ok = new JButton("確定");
cancel = new JButton("返回");
exit = new JButton("退出");
//下面三個(gè)語(yǔ)句 為按鈕分別 注冊(cè)監(jiān)聽(tīng)器
ok.addActionListener(this);
cancel.addActionListener(this);
exit.addActionListener(this);
getContentPane().add(ok);
getContentPane().add(cancel);
getContentPane().add(exit);
}
//完成 事件觸發(fā)時(shí)的事件處理
public void actionPerformed(ActionEvent e){
if(e.getSource()==ok)
System.out.println("確定");
if(e.getSource()==cancel)
System.out.println("返回");
if(e.getSource()==exit)
System.exit(0);;
}
public static void main(String args[]) {
ButtonListener pd=new ButtonListener("ActionEvent Demo");
pd.setSize(250,100);
pd.setVisible(true);
}
}
二、監(jiān)聽(tīng)類實(shí)現(xiàn)
效果:?jiǎn)螕舸绑w中的三個(gè)按鈕,實(shí)現(xiàn)相應(yīng)的相應(yīng)時(shí)間。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ButtonListener1 extends JFrame { //這里沒(méi)有實(shí)現(xiàn)監(jiān)聽(tīng)
JButton ok, cancel,exit;
public ButtonListener1(String title){
super(title);
this.setLayout(new FlowLayout());
ok = new JButton("確定");
cancel = new JButton("返回");
exit = new JButton("退出");
ok.addActionListener(new MyListener());
cancel.addActionListener(new MyListener());;
exit.addActionListener(new MyListener());;
getContentPane().add(ok);
getContentPane().add(cancel);
getContentPane().add(exit);
}
public static void main(String args[]) {
ButtonListener pd=new ButtonListener("ActionEvent Demo");
pd.setSize(250,100);
pd.setVisible(true);
}
}
//監(jiān)聽(tīng)動(dòng)作事件
class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getActionCommand()=="確定")
System.out.println("確定");
if(e.getActionCommand()=="返回")
System.out.println("返回");
if(e.getActionCommand()=="退出")
System.exit(0);;
}
}
三、使用 AbstractAction類實(shí)現(xiàn)監(jiān)聽(tīng)
效果:?jiǎn)螕舨藛危鞒鲰憫?yīng)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
//此類繼承AbstractAction,必須實(shí)現(xiàn)actionPerformed()方法。
class AbstractEvent extends AbstractAction{
//private static final long serialVersionUID = 1L;
AbstractEvent(){
}
public void actionPerformed(ActionEvent e){
//彈出確認(rèn)對(duì)話框
if (e.getActionCommand()=="open"){
JOptionPane.showMessageDialog(null, "打開(kāi)");
}else if (e.getActionCommand()=="close"){
JOptionPane.showMessageDialog(null, "關(guān)閉");
}else if (e.getActionCommand()=="run"){
JOptionPane.showMessageDialog(null, "運(yùn)行");
}else if (e.getActionCommand()=="stop"){
JOptionPane.showMessageDialog(null, "停止");
}
}
}
public class TestAbstractEvent {
private static JMenuBar menubar;
private static JFrame frame;
//指定MenuEvent的具體處理程序是AbstractEvent類完成的。
final Action MenuEvent=new AbstractEvent();
public TestAbstractEvent(){
frame=new JFrame("menu");
frame.getContentPane().setLayout(new BorderLayout());
menubar=new JMenuBar();
JMenu menuFile=new JMenu("file");
//實(shí)例化一個(gè)菜單項(xiàng),并添加監(jiān)聽(tīng)openAction,
JMenuItem menuItemopen=new JMenuItem("open");
menuItemopen.addActionListener(MenuEvent);
JMenuItem menuItemclose=new JMenuItem("close");
menuItemclose.addActionListener(MenuEvent);
menuFile.add(menuItemopen);
menuFile.add(menuItemclose);
JMenu menuTool=new JMenu("tool");
JMenuItem menuItemrun=new JMenuItem("run");
menuItemrun.addActionListener(MenuEvent);
JMenuItem menuItemstop=new JMenuItem("stop");
menuItemstop.addActionListener(MenuEvent);
menuTool.add(menuItemrun);
menuTool.add(menuItemstop);
menubar.add(menuFile);
menubar.add(menuTool);
menubar.setVisible(true);
frame.add(menubar,BorderLayout.NORTH);
frame.setSize(400,200);
frame.setVisible(true);
}
public static void main(String[] args){
new TestAbstractEvent();
}
}
四、 AbstractAction類 + 反射 的方法
效果:?jiǎn)螕艄ぞ邫诘娜齻€(gè)按鈕,通過(guò)按鈕的名稱,反射得到 與按鈕名稱相同的類實(shí)現(xiàn)響應(yīng)。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
class ViewAction extends AbstractAction{
private String ActionName="";
//private JFrame frame=null;
private Action action=null;
public ViewAction(){
}
public ViewAction(String ActionName){
this.ActionName=ActionName;
//this.frame=frame;
}
@Override
public void actionPerformed(ActionEvent e) {
Action action=getAction(this.ActionName);
action.execute();
}
private Action getAction(String ActionName){
try{
if (this.action==null){
Action action=(Action)Class.forName(ActionName).newInstance();
this.action=action;
}
return this.action;
}catch(Exception e){
return null;
}
}
}
public class TestAE extends JFrame {
public JToolBar bar=new JToolBar();
String buttonName[]={"b1","b2","b3"};
public TestAE(){
super("事件");
for (int i=0;i<buttonName.length;i++){
ViewAction action=new ViewAction(buttonName[i]);
JButton button=new JButton(buttonName[i]);
button.addActionListener(action);
bar.add(button);
}
this.getContentPane().add(bar,BorderLayout.NORTH);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String [] args){
new TestAE();
}
}
interface Action{
void execute();
}
class b1 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b1");
}
}
class b2 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b2");
}
}
class b3 implements Action{
public void execute(){
JOptionPane.showMessageDialog(null, "單擊了 b3");
}
}
上述實(shí)例備有較為詳盡的注釋,應(yīng)該不難理解。希望本文所述實(shí)例對(duì)大家能夠有所幫助。
- JavaWeb響應(yīng)下載功能實(shí)例代碼(包含工具類)
- java中添加按鈕并添加響應(yīng)事件的方法(推薦)
- java搭建一個(gè)Socket服務(wù)器響應(yīng)多用戶訪問(wèn)
- javaweb如何實(shí)現(xiàn)請(qǐng)求和響應(yīng)
- Java中HttpServletResponse響應(yīng)中文出現(xiàn)亂碼問(wèn)題
- Java Web請(qǐng)求與響應(yīng)實(shí)例詳解
- javasciprt下jquery函數(shù)$.post執(zhí)行無(wú)響應(yīng)的解決方法
- JAVA發(fā)送HTTP請(qǐng)求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實(shí)例代碼
- JavaWeb Refresh響應(yīng)頭代碼實(shí)例詳解
相關(guān)文章
如何解決Spring MVC中響應(yīng)亂碼問(wèn)題
這篇文章主要介紹了如何解決Spring MVC中響應(yīng)亂碼問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
使用Mybatis如何實(shí)現(xiàn)刪除多個(gè)數(shù)據(jù)
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)刪除多個(gè)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Redis Lettuce連接redis集群實(shí)現(xiàn)過(guò)程詳細(xì)講解
這篇文章主要介紹了Redis Lettuce連接redis集群實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
springboot啟動(dòng)報(bào)錯(cuò):application?startup?failed問(wèn)題
這篇文章主要介紹了springboot啟動(dòng)報(bào)錯(cuò):application?startup?failed問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java List出現(xiàn)All elements are null問(wèn)題及解決
這篇文章主要介紹了java List出現(xiàn)All elements are null問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-11-11
Spring Security組件一鍵接入驗(yàn)證碼登錄和小程序登錄的詳細(xì)過(guò)程
這篇文章主要介紹了Spring Security 一鍵接入驗(yàn)證碼登錄和小程序登錄,簡(jiǎn)單介紹一下這個(gè)插件包的相關(guān)知識(shí),本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-04-04
springboot整合vue實(shí)現(xiàn)上傳下載文件
這篇文章主要為大家詳細(xì)介紹了springboot整合vue實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11

