一些listene接口中只定义了一个方法,因此要实现这种接口的工作量并不大,因为只要写完这一个方法,接口也就实现了。但是如果要使用有多个方法的listener的话,就会比较麻烦了,因为必须要实现接口中定义的所有方法,而实际上很多方法我们并不需要。举例来说,如果要捕捉鼠标点击的话,那就必须写一个mouseClicked( )方法。但是由于MouseListener是一个interface,所以即使MouseListener定义的其他方法我们不使用,也得实现其所有的方法。
为了解决这个问题,有些(但不是全部)多方法的listener接口提供了适配器(adapter)。适配器会为接口提供默认的空方法。这样,你只要继承适配器,根据需要覆写方法就可以了。
例如:
package com.vitamin.UI;
![](/Images/OutliningIndicators/None.gif)
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.*;
![](/Images/OutliningIndicators/None.gif)
import javax.swing.*;
![](/Images/OutliningIndicators/None.gif)
public class HelloForm extends JFrame
{
private JLabel lbInfo = null;
private JButton btnOK = null;
![](/Images/OutliningIndicators/None.gif)
public HelloForm()
{
super();
}
public HelloForm(String title)
{
super(title);
this.initForm();
}
private void initForm()
{
this.lbInfo = new JLabel();
this.btnOK = new JButton("确定");
![](/Images/OutliningIndicators/None.gif)
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("内部匿名类测试程序");
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
}
![](/Images/OutliningIndicators/None.gif)
在附上<<TIJ>>中给出的一段处理多个事件的代码:
package com.vitamin.UI;
![](/Images/OutliningIndicators/None.gif)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import com.vitamin.Console.console;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TrackEvent extends JApplet
{
private HashMap h = new HashMap();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
class MyButton extends JButton
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void report(String field, String msg)
{
((JTextField)h.get(field)).setText(msg);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
FocusListener fl = new FocusListener()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void focusGained(FocusEvent e)
{
report("focusGained", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void focusLost(FocusEvent e)
{
report("focusLost", e.paramString());
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
KeyListener kl = new KeyListener()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void keyPressed(KeyEvent e)
{
report("keyPressed", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void keyReleased(KeyEvent e)
{
report("keyReleased", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void keyTyped(KeyEvent e)
{
report("keyTyped", e.paramString());
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MouseListener ml = new MouseListener()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseClicked(MouseEvent e)
{
report("mouseClicked", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseEntered(MouseEvent e)
{
report("mouseEntered", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseExited(MouseEvent e)
{
report("mouseExited", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mousePressed(MouseEvent e)
{
report("mousePressed", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseReleased(MouseEvent e)
{
report("mouseReleased", e.paramString());
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
MouseMotionListener mml = new MouseMotionListener()
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseDragged(MouseEvent e)
{
report("mouseDragged", e.paramString());
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void mouseMoved(MouseEvent e)
{
report("mouseMoved", e.paramString());
}
};
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public MyButton(Color color, String label)
{
super(label);
setBackground(color);
addFocusListener(fl);
addKeyListener(kl);
addMouseListener(ml);
addMouseMotionListener(mml);
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void init()
{
Container c = getContentPane();
c.setLayout(new GridLayout(event.length + 1, 2));
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
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);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
console.run(new TrackEvent(), 700, 500);
}
}另外,今天还尝试了下手工打包可执行的jar文件,可没有成功,找不到原因,郁闷。。。