import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public static void main(String[] args) {
JFrame frame = new JFrame("FocusListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel nameLabel = new JLabel("Name"); // nameLabel إسمه Label هنا أنشأنا
JLabel passLabel = new JLabel("Password"); // passLabel إسمه Label هنا أنشأنا
JTextField nameField = new JTextField(); // nameField إسمه Text Field هنا أنشأنا
JPasswordField passField = new JPasswordField(); // passField إسمه Text Field هنا أنشأنا
nameLabel.setBounds(30, 50, 60, 25); // frame في الـ nameLabel هنا قمنا بتحديد حجم و موقع الكائن
nameField.setBounds(100, 50, 150, 25); // frame في الـ nameField هنا قمنا بتحديد حجم و موقع الكائن
passLabel.setBounds(30, 90, 60, 25); // frame في الـ passLabel هنا قمنا بتحديد حجم و موقع الكائن
passField.setBounds(100, 90, 150, 25); // frame في الـ passField هنا قمنا بتحديد حجم و موقع الكائن
frame.add(nameLabel); // frame في الـ nameLabel هنا أضفنا الكائن
frame.add(nameField); // frame في الـ nameField هنا أضفنا الكائن
frame.add(passLabel); // frame في الـ passLabel هنا أضفنا الكائن
frame.add(passField); // frame في الـ passField هنا أضفنا الكائن
// خصيصاً لتغيير لون خلفية أي مربع نص يتعامل معه المستخدم حالياً fl أنشأنا هذا الكائن
FocusListener fl = new FocusListener() {
public void focusGained(FocusEvent e) {
// هنا قلنا أنه سيتم تلوين خلفية مربع النص الحالي الذي يمكن الكتابة فيه بالأصفر
e.getComponent().setBackground(Color.yellow);
public void focusLost(FocusEvent e) {
// هنا قلنا أنه سيتم تلوين خلفية مربع النص الذي لم يعد يتفاعل معه المستخدم باللون الأبيض
e.getComponent().setBackground(Color.white);
nameField.addFocusListener(fl); // fl بالحدث nameField هنا ربطنا الكائن
passField.addFocusListener(fl); // fl بالحدث nameField هنا ربطنا الكائن
frame.setSize(300, 200); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 200
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا
frame.setVisible(true); // هنا جعلنا النافذة مرئية
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("FocusListener demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
JLabel nameLabel = new JLabel("Name"); // nameLabel إسمه Label هنا أنشأنا
JLabel passLabel = new JLabel("Password"); // passLabel إسمه Label هنا أنشأنا
JTextField nameField = new JTextField(); // nameField إسمه Text Field هنا أنشأنا
JPasswordField passField = new JPasswordField(); // passField إسمه Text Field هنا أنشأنا
nameLabel.setBounds(30, 50, 60, 25); // frame في الـ nameLabel هنا قمنا بتحديد حجم و موقع الكائن
nameField.setBounds(100, 50, 150, 25); // frame في الـ nameField هنا قمنا بتحديد حجم و موقع الكائن
passLabel.setBounds(30, 90, 60, 25); // frame في الـ passLabel هنا قمنا بتحديد حجم و موقع الكائن
passField.setBounds(100, 90, 150, 25); // frame في الـ passField هنا قمنا بتحديد حجم و موقع الكائن
frame.add(nameLabel); // frame في الـ nameLabel هنا أضفنا الكائن
frame.add(nameField); // frame في الـ nameField هنا أضفنا الكائن
frame.add(passLabel); // frame في الـ passLabel هنا أضفنا الكائن
frame.add(passField); // frame في الـ passField هنا أضفنا الكائن
// خصيصاً لتغيير لون خلفية أي مربع نص يتعامل معه المستخدم حالياً fl أنشأنا هذا الكائن
FocusListener fl = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
// هنا قلنا أنه سيتم تلوين خلفية مربع النص الحالي الذي يمكن الكتابة فيه بالأصفر
e.getComponent().setBackground(Color.yellow);
}
@Override
public void focusLost(FocusEvent e) {
// هنا قلنا أنه سيتم تلوين خلفية مربع النص الذي لم يعد يتفاعل معه المستخدم باللون الأبيض
e.getComponent().setBackground(Color.white);
}
};
nameField.addFocusListener(fl); // fl بالحدث nameField هنا ربطنا الكائن
passField.addFocusListener(fl); // fl بالحدث nameField هنا ربطنا الكائن
frame.setSize(300, 200); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 200
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}