Swingطريقة تغيير نوع و حجم ولون خط عناصر الـ JComboBox
المثال التالي يعلمك طريقة تغيير نوع و حجم و لون خط عناصر كائن الـ JComboBox.
مثال
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.Color;
import java.awt.Font;
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 في الـ comboBox هنا قمنا بتحديد حجم و موقع الـ
Font newFont = new Font("Arial", Font.BOLD, 16); // حجمه 16 Arial يمثل نوع خط عريض إسمه Font هنا أنشأنا كائن من الكلاس
comboBox.setFont(newFont); // newFont يستخدم الـ comboBox هنا جعلنا الـ
comboBox.setForeground(Color.cyan); // comboBox هنا قمنا بتغير لون كلمات الـ
comboBox.setBackground(Color.gray); // comboBox هنا قمنا بتغير لون خلفية الـ
frame.add(comboBox); // frame في الـ comboBox هنا أضفنا الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}
import javax.swing.JFrame;
import javax.swing.JComboBox;
import java.awt.Color;
import java.awt.Font;
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 في الـ comboBox هنا قمنا بتحديد حجم و موقع الـ
Font newFont = new Font("Arial", Font.BOLD, 16); // حجمه 16 Arial يمثل نوع خط عريض إسمه Font هنا أنشأنا كائن من الكلاس
comboBox.setFont(newFont); // newFont يستخدم الـ comboBox هنا جعلنا الـ
comboBox.setForeground(Color.cyan); // comboBox هنا قمنا بتغير لون كلمات الـ
comboBox.setBackground(Color.gray); // comboBox هنا قمنا بتغير لون خلفية الـ
frame.add(comboBox); // frame في الـ comboBox هنا أضفنا الـ
frame.setVisible(true); // هنا جعلنا النافذة مرئية
}
}
ستظهر لك النافذة التالية عند التشغيل. قم بالنقر على الـ Combo Box لتظهر لك الخيارات التي بداخله أيضاً.