获取鼠标状态(按下,在窗口里的坐标,经过,是否在窗口)
1个回答
展开全部
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);
}
}
以上就可以解决你的要求了哈,希望对你有帮助哈。
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找下相应的类和方法就能了解我的代码了啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询