java中如何建立鼠标事件?
1个回答
展开全部
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){}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询