import java.awt.Button;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class ButtonEvent extends WindowAdapter {
public ButtonEvent() {
Frame f =new Frame();
f.setSize(100, 100);
Button b = new Button("eeee");
b.addMouseListener(new MouseAdapter(){ //Button b 注册鼠标监听器,并且这里用了内部类
public void mousePressed(MouseEvent e){
if(e.getButton()== e.BUTTON1)
{
System.out.println("左击");
}
if(e.getButton()== e.BUTTON2)
{
System.out.println("中击");
}
if(e.getButton()==e.BUTTON3)
{
System.out.println("右击");
}
}
});
f.add(b);
f.addWindowListener (this); //窗体f--注册窗体事件监听器
f.setVisible(true);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public static void main(String[] args) {
new ButtonEvent();
}
}
分享一下,希望能对你有用。