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

طريقة إضافة أي شيء في الـ JPanel 

طريقة إضافة أي شيء في الـ JPanel
طريقة إضافة أي شيء في الـ JPanel
طريقة إضافة أي شيء في الـ JPanel
طريقة إضافة أي شيء في الـ JPanel

طريقة إضافة أي شيء في الـ JPanel

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

طريقة إضافة أي شيء في الـ JPanel

المثال التالي يعلمك طريقة إضافة أي شيء في الـ Panel.

مثال

Main.java

  1. import javax.swing.JFrame;
  2. import javax.swing.JPanel;
  3. import javax.swing.JButton;
  4. import java.awt.Color;
  5. publicclass Main {
  6. publicstaticvoidmain(String[] args){
  7. // هنا قمنا بإنشاء النافذة مع تحديد بعض خصائصها
  8. JFrame frame = newJFrame(“JPanel demo”);
  9. frame.setSize(300240);
  10. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. frame.setLayout(null);
  12. // Frame لونه رمادي و حددنا موقعه بداخل الـ Panel هنا قمنا بتعريف
  13. JPanel panel = newJPanel();
  14. panel.setBackground(Color.gray);
  15. panel.setBounds(5050180110);
  16. // Frame في الـ Panel هنا وضعنا الـ
  17. frame.add(panel);
  18. // Buttons هنا قمنا بتعريف 4
  19. JButton button1 = newJButton(“Button 1”);
  20. JButton button2 = newJButton(“Button 2”);
  21. JButton button3 = newJButton(“Button 3”);
  22. JButton button4 = newJButton(“Button 4”);
  23. // منهم Button هنا قمنا بإعطاء لون خلفية مختلف لكل
  24. button1.setBackground(Color.orange);
  25. button2.setBackground(Color.yellow);
  26. button3.setBackground(Color.green);
  27. button4.setBackground(Color.pink);
  28. // Panel في الـ Buttons هنا قمنا بوضع الـ
  29. panel.add(button1);
  30. panel.add(button2);
  31. panel.add(button3);
  32. panel.add(button4);
  33. // مرئية Frame هنا جعلنا الـ
  34. frame.setVisible(true);
  35. }
  36. }

ستظهر لك النافذة التالية عند التشغيل.

طريقة إضافة أشياء component في ال JPanel في جافا

 
المصدر: طريقة إضافة أي شيء في الـ JPanel – المناهج السعودية

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

اترك تعليقاً

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

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