Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

Swingطريقة إنشاء JComboBox يحتوي على عناصر

المثال التالي يعلمك طريقة إنشاء كائن من الكلاس JComboBox يحتوي على عناصر.


مثال

Main.java
import javax.swing.JFrame;
import javax.swing.JComboBox;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(null); // في النافذة بنفسنا Combo Box لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا
String[] items = { "java", "php", "c", "c++" , "html" }; // هنا قمنا بتعريف مصفوفة من الكلمات
JComboBox comboBox = new JComboBox( items ); // له كعناصر items و وضعنا عناصر المصفوفة Combo Box أي قمنا بإنشاء JComboBox هنا أنشأنا كائن من الكلاس
comboBox.setBounds(90, 40, 100, 30); // frame في الـ Combo Box هنا قمنا بتحديد حجم و موقع الـ
frame.add(comboBox); // frame في الـ comboBox هنا أضفنا الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}
import javax.swing.JFrame; import javax.swing.JComboBox; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("JComboBox demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج frame.setLayout(null); // في النافذة بنفسنا Combo Box لذلك سنقوم بتحديد مكان الـ Layout Manager أي لم نستخدم أي null هنا وضعنا String[] items = { "java", "php", "c", "c++" , "html" }; // هنا قمنا بتعريف مصفوفة من الكلمات JComboBox comboBox = new JComboBox( items ); // له كعناصر items و وضعنا عناصر المصفوفة Combo Box أي قمنا بإنشاء JComboBox هنا أنشأنا كائن من الكلاس comboBox.setBounds(90, 40, 100, 30); // frame في الـ Combo Box هنا قمنا بتحديد حجم و موقع الـ frame.add(comboBox); // frame في الـ comboBox هنا أضفنا الـ frame.setVisible(true); // هنا جعلنا النافذة مرئية } }

ستظهر لك النافذة التالية عند التشغيل. قم بالنقر على الـ Combo Box لتظهر لك الخيارات التي بداخله أيضاً.

طريقة إضافة عناصر في ال JComboBox