Swingطريقة إنشاء شريط قوائم JMenuBar و إضافة قوائم JMenu فيه
المثال التالي يعلمك طريقة إضافة شريط القوائم و إضافة قوائم فيه.
مثال
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JMenuBar demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(500, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 500 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
// هنا قمنا بتعريف شريط القوائم
JMenuBar menuBar = new JMenuBar();
// هنا قمنا بتعريف 3 قوائم
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu help = new JMenu("Help");
// هنا قمنا بوضع القوائم الثلاثة في شريط القوائم
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
// frame هنا قمنا بوضع شريط القوائم في الـ
frame.setJMenuBar(menuBar);
// مرئية frame هنا جعلنا الـ
frame.setVisible(true);
}
}
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class Main {
public static void main(String[] args) {
JFrame frame = new JFrame("JMenuBar demo"); // أي قمنا بإنشاء نافذة مع وضع عنوان لها JFrame هنا أنشأنا كائن من الكلاس
frame.setSize(500, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 500 و طولها 250
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
// هنا قمنا بتعريف شريط القوائم
JMenuBar menuBar = new JMenuBar();
// هنا قمنا بتعريف 3 قوائم
JMenu file = new JMenu("File");
JMenu edit = new JMenu("Edit");
JMenu help = new JMenu("Help");
// هنا قمنا بوضع القوائم الثلاثة في شريط القوائم
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
// frame هنا قمنا بوضع شريط القوائم في الـ
frame.setJMenuBar(menuBar);
// مرئية frame هنا جعلنا الـ
frame.setVisible(true);
}
}
ستظهر لك النافذة التالية عند التشغيل.