Java圖形用戶界面中,處理事件時所必須的步驟是:
一、建立接受響應的組件(控件)
二、實現相關事件監聽接口
三、註冊事件源的動做監聽器
四、事件觸發時的事件處理
相應的能夠經過如下的集中方式來做出事件響應。html
- <span style="font-size: 18px;">1、容器類監聽
-
- 效果:單擊窗體中的三個按鈕,實現相應的相應時間。
-
- </span><pre class="java" name="code">import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
-
-
- 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);
- }
- }
- </pre><br>
- <br>
- <pre></pre>
- <p><span style="font-size: 18px;">2、監聽類實現</span><br>
- <br>
- <pre style="margin: 4px 0px; font-size: 18px; background-color: rgb(240, 240, 240);" class="java" name="code"><span style="font-size: 18px;">效果:單擊窗體中的三個按鈕,實現相應的相應時間。
- </span></pre><p></p>
- <div><span style="font-size: 18px;"><br>
- </span></div>
- <pre style="font-size: 18px;" class="html" name="code">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);;
- }
-
- } </pre><br>
- <span style="font-size: 18px;">3、使用 AbstractAction類實現監聽</span><br>
- <span style="font-size: 18px;"> 這個方法,我也沒搞清,照着別人的例子作出來的<br>
- 效果:單擊菜單,做出響應</span><br>
- <pre style="font-size: 18px;" class="java" name="code">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;
-
- class AbstractEvent extends AbstractAction{
-
- 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;
-
-
- final Action MenuEvent=new AbstractEvent();
- public TestAbstractEvent(){
- frame=new JFrame("menu");
- frame.getContentPane().setLayout(new BorderLayout());
- menubar=new JMenuBar();
- JMenu menuFile=new JMenu("file");
-
-
- 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();
- }
- }</pre><br>
- <span style="font-size: 18px;">4、</span><span style="font-size: 18px;"> AbstractAction類 + 反射 的方法<br>
- <span style="font-size: 18px;">這個方法,我也沒搞清,照着別人的例子作出來的!<br>
- </span><br>
- 效果:單擊工具欄的三個按鈕,經過按鈕的名稱,反射獲得 與按鈕名稱相同的類實現響應。<br>
- <br>
- </span><pre class="java" name="code">import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import javax.swing.*;
-
- class ViewAction extends AbstractAction{
- private String ActionName="";
-
- private Action action=null;
- public ViewAction(){
- }
- public ViewAction(String ActionName){
- this.ActionName=ActionName;
-
- }
- @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");
- }
- }</pre><br>
- <br>
- <p></p>