您现在的位置是:首页 > 文章详情

《Java编程思想》读书笔记(9)

日期:2017-12-26点击:520
一些listene接口中只定义了一个方法,因此要实现这种接口的工作量并不大,因为只要写完这一个方法,接口也就实现了。但是如果要使用有多个方法的listener的话,就会比较麻烦了,因为必须要实现接口中定义的所有方法,而实际上很多方法我们并不需要。举例来说,如果要捕捉鼠标点击的话,那就必须写一个mouseClicked( )方法。但是由于MouseListener是一个interface,所以即使MouseListener定义的其他方法我们不使用,也得实现其所有的方法。
为了解决这个问题,有些(但不是全部)多方法的listener接口提供了适配器(adapter)。适配器会为接口提供默认的空方法。这样,你只要继承适配器,根据需要覆写方法就可以了。
例如:

package com.vitamin.UI;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.*;

import javax.swing.*;

public class HelloForm extends JFrame
{
    private JLabel lbInfo = null;
    private JButton btnOK = null;

    public HelloForm() 
    {
        super();
    }
    public HelloForm(String title)
    {
        super(title);
        this.initForm();
    }
    
    private void initForm()
    {
        this.lbInfo = new JLabel();
        this.btnOK = new JButton("确定");

        this.btnOK.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent e) {
                lbInfo.setText("hello,world");
            }
        });
        
        
        
        Container con = this.getContentPane();
        con.setLayout(new BorderLayout());
        con.add(this.btnOK,BorderLayout.SOUTH);
        con.add(this.lbInfo,BorderLayout.NORTH);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300,300);
        this.setVisible(true);
    }
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        HelloForm hf = new HelloForm("内部匿名类测试程序");

    }

}


在附上<<TIJ>>中给出的一段处理多个事件的代码:
package com.vitamin.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.vitamin.Console.console;

public class TrackEvent extends JApplet {
  private HashMap h = new HashMap();
  private String[] event = {
    "focusGained", "focusLost", "keyPressed",
    "keyReleased", "keyTyped", "mouseClicked",
    "mouseEntered", "mouseExited", "mousePressed",
    "mouseReleased", "mouseDragged", "mouseMoved"
  };
  private MyButton
    b1 = new MyButton(Color.BLUE, "test1"),
    b2 = new MyButton(Color.RED, "test2");
  class MyButton extends JButton {
    void report(String field, String msg) {
      ((JTextField)h.get(field)).setText(msg);
    }
    FocusListener fl = new FocusListener() {
      public void focusGained(FocusEvent e) {
        report("focusGained", e.paramString());
      }
      public void focusLost(FocusEvent e) {
        report("focusLost", e.paramString());
      }
    };
    
    KeyListener kl = new KeyListener() {
      public void keyPressed(KeyEvent e) {
        report("keyPressed", e.paramString());
      }
      public void keyReleased(KeyEvent e) {
        report("keyReleased", e.paramString());
      }
      public void keyTyped(KeyEvent e) {
        report("keyTyped", e.paramString());
      }
    };
    
    MouseListener ml = new MouseListener() {
      public void mouseClicked(MouseEvent e) {
        report("mouseClicked", e.paramString());
      }
      public void mouseEntered(MouseEvent e) {
        report("mouseEntered", e.paramString());
      }
      public void mouseExited(MouseEvent e) {
        report("mouseExited", e.paramString());
      }
      public void mousePressed(MouseEvent e) {
        report("mousePressed", e.paramString());
      }
      public void mouseReleased(MouseEvent e) {
        report("mouseReleased", e.paramString());
      }
    };
    
    MouseMotionListener mml = new MouseMotionListener() {
      public void mouseDragged(MouseEvent e) {
        report("mouseDragged", e.paramString());
      }
      public void mouseMoved(MouseEvent e) {
        report("mouseMoved", e.paramString());
      }
    };
    
    public MyButton(Color color, String label) {
      super(label);
      setBackground(color);
      addFocusListener(fl);
      addKeyListener(kl);
      addMouseListener(ml);
      addMouseMotionListener(mml);
    }
  }
  public void init() {
    Container c = getContentPane();
    c.setLayout(new GridLayout(event.length + 1, 2));
    for(int i = 0; i < event.length; i++) {
      JTextField t = new JTextField();
      t.setEditable(false);
      c.add(new JLabel(event[i], JLabel.RIGHT));
      c.add(t);
      h.put(event[i], t);
    }
    c.add(b1);
    c.add(b2);
  }
  public static void main(String[] args) {
    console.run(new TrackEvent(), 700, 500);
  }
}
另外,今天还尝试了下手工打包可执行的jar文件,可没有成功,找不到原因,郁闷。。。




本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2006/06/07/419356.html,如需转载请自行联系原作者
原文链接:https://yq.aliyun.com/articles/343733
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章