Swingطريقة ترتيب محتوى الـ JFrame بواسطة الكلاس CardLayout
المثال التالي يعلمك طريقة ترتيب محتوى الـ Frame بواسطة الكلاس CardLayout.
مثال
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.CardLayout;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("CardLayout demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(300, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 300
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new CardLayout(20, 20)); // لترتيب الأشياء التي نضيفها بداخلها CardLayout هنا جعلنا النافذة تستخدم الـ
// هنا قمنا بتعريف 3 أزرار
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
// هنا قمنا بإضافة الأزرار في النافذة
frame.add(b1);
frame.add(b2);
frame.add(b3);
// هنا جعلنا النافذة مرئية
frame.setVisible(true);
}
}
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.CardLayout;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("CardLayout demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(300, 300); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 300
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
frame.setLayout(new CardLayout(20, 20)); // لترتيب الأشياء التي نضيفها بداخلها CardLayout هنا جعلنا النافذة تستخدم الـ
// هنا قمنا بتعريف 3 أزرار
JButton b1 = new JButton("1");
JButton b2 = new JButton("2");
JButton b3 = new JButton("3");
// هنا قمنا بإضافة الأزرار في النافذة
frame.add(b1);
frame.add(b2);
frame.add(b3);
// هنا جعلنا النافذة مرئية
frame.setVisible(true);
}
}
ستظهر لك النافذة التالية عند التشغيل.
لاحظ أن أول زر أضفته في النافذة هو الوحيد الذي ظهر لأن باقي الأزرار موجودة خلفه.