Swingالإنترفيس MouseMotionListener
يستخدم هذا الإنترفيس للتنبه عندما يقوم المستخدم بتحريك الفأرة.
الإنترفيس MouseMotionListener يحتوي على دالتين يجب أن تفعل لهما Override عند إنشاء كائن منه:
- الدالة mouseMoved(MouseEvent e): يتم إستدعاءها بعد أن يقوم المستخدم بتحريك الفأرة.
- الدالة mouseDragged(MouseEvent e): يتم إستدعاءها عندما يقوم المستخدم بإبقاء إصبعه على أي زر موجود في الفأرة و تحريكها بنفس الوقت بهدف تحديد أشياء موجودة في النافذة بواسطة الفأرة.
مثال
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("MouseMotionListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel mousePosition = new JLabel("Mouse Position:"); // mousePosition إسمه Label هنا أنشأنا
frame.add(mousePosition); // frame في الـ mousePosition هنا أضفنا الكائن
// هنا نضع الأوامر التي نريد تنفيذها عند تفاعل المستخدم مع النافذة بواسطة الفأرة
frame.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) { }
@Override
public void mouseMoved(MouseEvent e) {
mousePosition.setText(" Mouse Position: ("+e.getX()+", "+e.getY() +")");
}
});
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new FlowLayout()); // لترتيب الأشياء التي أضفناها فيها FlowLayout هنا جعلنا النافذة تستخدم الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}
import java.awt.FlowLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("MouseMotionListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel mousePosition = new JLabel("Mouse Position:"); // mousePosition إسمه Label هنا أنشأنا
frame.add(mousePosition); // frame في الـ mousePosition هنا أضفنا الكائن
// هنا نضع الأوامر التي نريد تنفيذها عند تفاعل المستخدم مع النافذة بواسطة الفأرة
frame.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) { }
@Override
public void mouseMoved(MouseEvent e) {
mousePosition.setText(" Mouse Position: ("+e.getX()+", "+e.getY() +")");
}
});
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new FlowLayout()); // لترتيب الأشياء التي أضفناها فيها FlowLayout هنا جعلنا النافذة تستخدم الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}
ستظهر لك النافذة التالية عند التشغيل.
عند تحريك الفأرة فوق النافذة, سيتم إظهار مكان وقوف سهم الفأرة بالنسبة للنافذة.