التعليم السعودي

طريقة إنشاء JOptionPane يمثل input dialog 

طريقة إنشاء JOptionPane يمثل input dialog
طريقة إنشاء JOptionPane يمثل input dialog
طريقة إنشاء JOptionPane يمثل input dialog
طريقة إنشاء JOptionPane يمثل input dialog
طريقة إنشاء JOptionPane يمثل input dialog

المناهج السعودية

طريقة إنشاء JOptionPane يمثل input dialog

عند النقر على الزر Search سيظهر Input Dialog تطلب من المستخدم إدخال قيمة في Text Field.
عند النقر على الزر Select سيظهر Input Dialog تطلب من المستخدم إختيار قيمة من Combo Box.

مثال

Main.java

  1. import javax.swing.JFrame;
  2. import javax.swing.JButton;
  3. import javax.swing.JLabel;
  4. import javax.swing.JOptionPane;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. publicclass Main {
  8. publicstaticvoidmain(String[] args){
  9. JFrame frame = newJFrame(“JOptionPane demo”)// أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
  10. frame.setSize(400200)// هنا قمنا بتحديد حجم النافذة. عرضها 400 و طولها 200
  11. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)// هنا جعلنا زر الخروج من النافذة يغلق البرنامج
  12. frame.setLayout(null)// في النافذة بنفسنا Buttons لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
  13. // واحد Label و Buttons هنا قمنا بتعريف خمسة إثنين
  14. JButton btn1 = newJButton(“Enter Value”);
  15. JButton btn2 = newJButton(“Select Value”);
  16. JLabel label = newJLabel(“Returned Value:”);
  17. // Frame هنا قمنا بتحديد موقع و حجم جميع الأشياء التي سنضيفها في الـ
  18. // منهم Button هنا قمنا بتحديد موقع و حجم كل
  19. btn1.setBounds(953020030);
  20. btn2.setBounds(957020030);
  21. label.setBounds(9511020030);
  22. // Frame هنا قمنا بإضافة جميع الأشياء التي قمنا بتعريفها في الـ
  23. frame.add(btn1);
  24. frame.add(btn2);
  25. frame.add(label);
  26. // مرئية Frame هنا جعلنا الـ
  27. frame.setVisible(true);
  28. // btn1 عند النقر على الـ Input Dialog هنا قلنا أنه سيتم إظهار
  29. btn1.addActionListener(newActionListener(){
  30. @Override
  31. publicvoidactionPerformed(ActionEvent e){
  32. // result ثم تخزين القيمة التي قام المستخدم بإدخالها في المتغير Input Dialog سيتم إظهار
  33. String result = JOptionPane.showInputDialog(frame, “Enter value:”“”, JOptionPane.PLAIN_MESSAGE);
  34. // Label في حال قام المستخدم بإدخال قيمة في مربع النص فإنه سيتم عرضها في الـ
  35. if(result != null)
  36. label.setText(“Returned Value: “ + result);
  37. }
  38. });
  39. // btn2 عند النقر على الـ Input Dialog هنا قلنا أنه سيتم إظهار
  40. btn2.addActionListener(newActionListener(){
  41. @Override
  42. publicvoidactionPerformed(ActionEvent e){
  43. // Input Dialog في الـ Combo Box هنا قمنا بتعريف مصفوفة من الخيارات التي نريد عرضها كـ
  44. Object[] values = {“java”“css”“c++”“android”};
  45. // result ثم تخزين القيمة التي قام المستخدم بإدخالها في المتغير Input Dialog سيتم إظهار
  46. String result = (String) JOptionPane.showInputDialog(frame, “Select value”“”, JOptionPane.PLAIN_MESSAGEnull, values, values[0]);
  47. // Label سيتم عرض القيمة التي قام المستخدم بإختيارها في الـ
  48. label.setText(“Returned Value: “ + result);
  49. }
  50. });
  51. }
  52. }

ستظهر لك النافذة التالية عند التشغيل.
كل زر يظهر Input Dialog مختلف في المحتوى.

طريقة إنشاء JOptionPane يمثل input dialog في جافا

المصدر: طريقة إنشاء JOptionPane يمثل input dialog – المناهج السعودية

مقالات ذات صلة

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *

زر الذهاب إلى الأعلى