المثال التالي يعلمك طريقة الحصول على النص الموجود في الـ Password Field عند النقر على Button.
المثال عبارة واجهة تسجيل دخول مستخدم فيها ثلاثة Labels, Text Field, Password Field و Button.
عند النقر على الـ Button سيتم جمع القيم المدخلة في الـ Text Field و الـ Password Fields.
مثال
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Main { static JFrame frame = new JFrame("JTextField demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس static JLabel name = new JLabel("Name"); // name إسمه Label هنا أنشأنا static JTextField name_data = new JTextField(); // name_data إسمه Text Field هنا أنشأنا static JLabel pass = new JLabel("Password"); // pass إسمه Label هنا أنشأنا static JPasswordField pass_data = new JPasswordField(); // pass_data إسمه Password Field هنا أنشأنا static JButton display_info = new JButton("Display user info"); // button إسمه Button هنا أنشأنا static JLabel user_info = new JLabel(); // فارغ labelResult إسمه Label هنا أنشأنا public static void main(String[] args) { name.setBounds(40, 40, 100, 30); // frame في الـ label هنا قمنا بتحديد حجم و موقع الكائن name_data.setBounds(150, 40, 150, 30); // frame في الـ textField هنا قمنا بتحديد حجم و موقع الكائن pass.setBounds(40, 100, 100, 30); // frame في الـ label هنا قمنا بتحديد حجم و موقع الكائن pass_data.setBounds(150, 100, 150, 30); // frame في الـ textField هنا قمنا بتحديد حجم و موقع الكائن display_info.setBounds(150, 160, 150, 30); // frame في الـ button هنا قمنا بتحديد حجم و موقع الكائن user_info.setBounds(150, 220, 300, 30); // frame في الـ labelResult هنا قمنا بتحديد حجم و موقع الكائن frame.add(name); // frame في الـ label هنا أضفنا الكائن frame.add(name_data); // frame في الـ label هنا أضفنا الكائن frame.add(pass); // frame في الـ label هنا أضفنا الكائن frame.add(pass_data); // frame في الـ label هنا أضفنا الكائن frame.add(display_info); // frame في الـ label هنا أضفنا الكائن frame.add(user_info); // frame في الـ label هنا أضفنا الكائن frame.setSize(460, 340); // هنا قمنا بتحديد حجم النافذة. عرضها 360 و طولها 250 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج frame.setLayout(null); // لذلك قمنا بتحديد مكان كل شيء قمنا بإضافته في النافذة Layout Manager أي لم نستخدم أي null هنا وضعنا frame.setVisible(true); // هنا جعلنا النافذة مرئية // display_info هنا نضع الأوامر التي نريد تنفيذها عند النقر على الزر display_info.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // name و تخزينه في المتغير name_data سيتم جلب النص الذي سيدخله المستخدم في الـ String name = name_data.getText(); // pass و تحويله إلى نص ثم تخزينه في المتغير pass_data سيتم جلب النص الذي سيدخله المستخدم في الـ String pass = new String(pass_data.getPassword()); // user_info كنص للكائن pass و name بعدها سيتم وضع قيم المتغيراتـ user_info.setText("Name: "+ name +" Password: "+pass); } }); } }
ستظهر لك النافذة التالية عند التشغيل.
قم بإدخال أي نص في الـ Text Field و الـ Password Field, ثم أنقر على الـ Display user info و ستظهر لك المعلومات التي قمت بإدخالها.