import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
public static void main(String[] args) {
JFrame frame = new JFrame("MouseListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel mousePosition = new JLabel(" Mouse Position:"); // mousePosition إسمه Label هنا أنشأنا
JLabel mouseStatus = new JLabel(" Mouse Status:"); // mouseStatus إسمه Label هنا أنشأنا
frame.add(mousePosition); // frame في الـ mousePosition هنا أضفنا الكائن
frame.add(mouseStatus); // frame في الـ mouseStatus هنا أضفنا الكائن
// هنا نضع الأوامر التي نريد تنفيذها عند تفاعل المستخدم مع النافذة بواسطة الفأرة
frame.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
mousePosition.setText(" Mouse Position: ("+e.getX()+", "+e.getY() +")");
mouseStatus.setText(" Mouse Status: Mouse Clicked");
public void mousePressed(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Pressed");
public void mouseReleased(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Released");
public void mouseEntered(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Entered");
public void mouseExited(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Exited");
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new GridLayout(2, 1)); // لترتيب الأشياء التي أضفناها فيها GroupLayout هنا جعلنا النافذة تستخدم الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("MouseListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel mousePosition = new JLabel(" Mouse Position:"); // mousePosition إسمه Label هنا أنشأنا
JLabel mouseStatus = new JLabel(" Mouse Status:"); // mouseStatus إسمه Label هنا أنشأنا
frame.add(mousePosition); // frame في الـ mousePosition هنا أضفنا الكائن
frame.add(mouseStatus); // frame في الـ mouseStatus هنا أضفنا الكائن
// هنا نضع الأوامر التي نريد تنفيذها عند تفاعل المستخدم مع النافذة بواسطة الفأرة
frame.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
mousePosition.setText(" Mouse Position: ("+e.getX()+", "+e.getY() +")");
mouseStatus.setText(" Mouse Status: Mouse Clicked");
}
@Override
public void mousePressed(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Pressed");
}
@Override
public void mouseReleased(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Released");
}
@Override
public void mouseEntered(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Entered");
}
@Override
public void mouseExited(MouseEvent e) {
mouseStatus.setText(" Mouse Status: Mouse Exited");
}
});
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new GridLayout(2, 1)); // لترتيب الأشياء التي أضفناها فيها GroupLayout هنا جعلنا النافذة تستخدم الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}