获取鼠标状态(按下,在窗口里的坐标,经过,是否在窗口)

 我来答
wenli158
2011-07-13 · TA获得超过125个赞
知道小有建树答主
回答量:162
采纳率:0%
帮助的人:110万
展开全部
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class TwoListener implements MouseMotionListener, MouseListener
{
private Frame frame;

private TextField textField;

public void go()
{
frame = new Frame("Two Lisreners Example");

frame.add(new Label("click"), BorderLayout.NORTH);

textField = new TextField(30);

frame.add(textField, BorderLayout.SOUTH);

frame.addMouseMotionListener(this);
frame.addMouseListener(this);
frame.addMouseListener(new MyMouseListener());

frame.setSize(300, 200);

frame.setVisible(true);
}

public static void main(String[] args)
{
TwoListener listener = new TwoListener();

listener.go();
}

@Override
public void mouseClicked(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

}

@Override
public void mouseEntered(MouseEvent e)
{
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e)
{
String str = "The mouse has left the Frame";

this.textField.setText(str);
}

@Override
public void mouseDragged(MouseEvent e)
{
String str = "x:" + e.getX() + ",y:" + e.getY();

this.textField.setText(str);
}

@Override
public void mouseMoved(MouseEvent e)
{
// TODO Auto-generated method stub

}
}

class MyMouseListener implements MouseListener
{

@Override
public void mouseClicked(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

}

@Override
public void mouseEntered(MouseEvent e)
{
String str = "The mouse has entered the Frame";

System.out.println(str);
}

@Override
public void mouseExited(MouseEvent e)
{
String str = "The mouse has exited the Frame";

System.out.println(str);
}
}
以上就可以解决你的要求了哈,希望对你有帮助哈。
追问
麻烦翻译翻译把,看不懂,英语不好。
追答
这个跟英文没关系的,自己下个java jdk的api找下相应的类和方法就能了解我的代码了啊。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式