本文實例彙整了java中常見的事件回應方法,包括容器類別監聽、監聽器類別、AbstractAction、反射等。以方便大家參考。具體方法如下:
首先,在Java圖形使用者介面中,處理事件時所必須的步驟是:
1、建立接受回應的元件(控制項)
2、實作相關事件監聽介面
3、註冊事件來源的動作監聽器
4、事件觸發時的事件處理
對應的可以透過以下的集中方式來作出事件回應。
一、容器類別監聽
效果:點選視窗中的三個按鈕,以實現對應的對應時間。
import java.awt.*; import java.awt.event.*; import javax.swing.*; //声明 类时,添加“implements ActionListener”实现监听的接口,如果要对多种监听方式进行监听,则用逗号间隔开 // 比如“implements ActionListener,KeyListener” class ButtonListener extends JFrame implements ActionListener{ JButton ok, cancel,exit; //创建接受响应的组建,就是三个按钮 public ButtonListener(String title){ super(title); this.setLayout(new FlowLayout()); ok = new JButton("确定"); cancel = new JButton("返回"); exit = new JButton("退出"); //下面三个语句 为按钮分别 注册监听器 ok.addActionListener(this); cancel.addActionListener(this); exit.addActionListener(this); getContentPane().add(ok); getContentPane().add(cancel); getContentPane().add(exit); } //完成 事件触发时的事件处理 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); } }
二、監聽類別實作
效果:點選視窗中的三個按鈕,以達到對應的對應時間。
import java.awt.*; import java.awt.event.*; import javax.swing.*; class ButtonListener1 extends JFrame { //这里没有实现监听 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); } } //监听动作事件 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類別實現監聽
效果:點擊選單,做出回應
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,必须实现actionPerformed()方法。 class AbstractEvent extends AbstractAction{ //private static final long serialVersionUID = 1L; AbstractEvent(){ } public void actionPerformed(ActionEvent e){ //弹出确认对话框 if (e.getActionCommand()=="open"){ JOptionPane.showMessageDialog(null, "打开"); }else if (e.getActionCommand()=="close"){ JOptionPane.showMessageDialog(null, "关闭"); }else if (e.getActionCommand()=="run"){ JOptionPane.showMessageDialog(null, "运行"); }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"); //实例化一个菜单项,并添加监听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類別+ 反射的方法
效果:點擊工具列的三個按鈕,透過按鈕的名稱,反射得到與按鈕名稱相同的類別實現回應。
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"); } }
上述實例備有較詳盡的註釋,應不難理解。希望本文所述實例對大家能夠有所幫助。
更多java常見事件回應方法實例彙總相關文章請關注PHP中文網!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器