java中如何建立鼠标事件?

 我来答
H121508
推荐于2016-11-22 · TA获得超过3266个赞
知道小有建树答主
回答量:1393
采纳率:33%
帮助的人:1214万
展开全部
import javax.swing.*;
import java.awt.event.*;
public class Example6_9 implements MouseMotionListener,MouseListener,WindowListener
{
JFrame win;
JTextField text;
public static void main(String[] args)
{
Example6_9 w=new Example6_9();
w.toWin();
}
public void toWin()
{
win=new JFrame("实现三个接口的事例");
win.setSize(300,200);
win.setVisible(true);
win.add(new JLabel("单击并拖拽鼠标"));
text=new JTextField(30);
win.add(text,"South");
win.addMouseMotionListener(this);
win.addMouseListener(this);
win.addWindowListener(this);
}
public void mouseDragged(MouseEvent e)
{
String s="拖曳鼠标:X="+e.getX()+"Y="+e.getY();
text.setText(s);
}
public void mouseEntered(MouseEvent e)
{
String s="鼠标进入";
text.setText(s);
}
public void mouseExited(MouseEvent e)
{
String s="鼠标离开";
text.setText(s);
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void mouseMoved(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式