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

来源:互联网 发布:java状态机配置 编辑:程序博客网 时间:2024/06/03 17:14

一些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 + 12));
    
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(), 700500);
  }

}
另外,今天还尝试了下手工打包可执行的jar文件,可没有成功,找不到原因,郁闷。。。