java中用事件适配器处理鼠标事件的方法?
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example6_10
{
JFrame win;
JTextField text;
public Example6_10()
{
win=new JFrame("事件适配器及内部类示例");
text=new JTextField(30);
}
public void inFrame()
{
JLabel label=new JLabel("单击并拖曳鼠标");
win.add(label,BorderLayout.NORTH);
win.add(text,BorderLayout.SOUTH);
win.addMouseMotionListener(new MyMouseMotionListener());
win.setSize(300,200);
win.setVisible(true);
}
class MyMouseMotionListener extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
String s;
s="Mouse dragging:x="+e.getX()+"Y="+e.getY();
text.setText(s);
}
}
public static void main(String[] args)
{
Example6_10 w=new Example6_10();
w.inFrame();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询