public class MouseClickEvent extends JFrame{
public MouseClickEvent() {
Container container = getContentPane();
container.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("释放的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("释放的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("释放的是鼠标右键!\n");
break;
}
}
@Override
public void mousePressed(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("按下的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("按下的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("按下的是鼠标右键!\n");
break;
}
}
@Override
public void mouseExited(MouseEvent arg0) {
System.out.println("光标移出应用程序窗口了!\n");
}
@Override
public void mouseEntered(MouseEvent arg0) {
System.out.println("光标进入应用程序窗口了!\n");
}
@Override
public void mouseClicked(MouseEvent arg0) {
int buttonFlag = arg0.getButton();
switch (buttonFlag) {
case MouseEvent.BUTTON1:
System.out.println("点击的是鼠标左键!\n");
break;
case MouseEvent.BUTTON2:
System.out.println("点击的是鼠标滚轮!\n");
break;
case MouseEvent.BUTTON3:
System.out.println("点击的是鼠标右键!\n");
break;
}
int clickCount = arg0.getClickCount();
System.out.println("点击次数为"+clickCount+"\n");
}
});
}
public static void main(String[] args) {
MouseClickEvent testFrame = new MouseClickEvent();
testFrame.setTitle("鼠标点击事件测试程序!");
testFrame.setVisible(true);
testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setBounds(0, 0, 500, 400);
}
}
一个类implements MouseListener后用eclipse自动生成要实现的接口方法, 就会出现下面五个
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
这就是对应的事件, 你想怎么做就在里面写
然后用addMouseListener()把这个实现了MouseListener的类关联到响应的组件上就ok了
和给button添加actionListener一样的
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
zhidingLabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
zhidingLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
/* if(getindexfromList()!=-1)
{
MessageEntity m=messagelist.get(getindexfromList());
if(m.getflag_read().equals("2"))
{
CancleMessageToTop();
}
else
{*/
MessageToTop();
//}
//}
}
});
这是我以前写的一段代码,是给置顶的label加的鼠标事件,里面重写了各种方法,比如mouseClicked(MouseEvent e)方法,就是把点击这个label要执行的代码写在这里就行了,不懂可以追问哦,帮助到你记得采纳哟