logo

تخطيط الحدود (مديري التخطيط)

مديري تخطيط جافا

يتم استخدام LayoutManagers لترتيب المكونات بطريقة معينة. ال مديري تخطيط جافا يسهل علينا التحكم في موضع المكونات وحجمها في نماذج واجهة المستخدم الرسومية. LayoutManager هي واجهة يتم تنفيذها بواسطة جميع فئات مديري التخطيط. هناك الفئات التالية التي تمثل مديري التخطيط:

الانضمام إلى سلسلة جافا
  1. java.awt.BorderLayout
  2. java.awt.FlowLayout
  3. java.awt.GridLayout
  4. java.awt.CardLayout
  5. java.awt.GridBagLayout
  6. javax.swing.BoxLayout
  7. javax.swing.GroupLayout
  8. javax.swing.ScrollPaneLayout
  9. javax.swing.SpringLayout وما إلى ذلك.

جافا تخطيط الحدود

يتم استخدام BorderLayout لترتيب المكونات في خمس مناطق: الشمال والجنوب والشرق والغرب والوسط. قد تحتوي كل منطقة (منطقة) على مكون واحد فقط. هذا هو التخطيط الافتراضي للإطار أو النافذة. يوفر BorderLayout خمسة ثوابت لكل منطقة:

    النهائي العام الثابت NORTH النهائي العام الثابت SOUTH النهائي العام الثابت EAST النهائي العام الثابت الغرب مركز كثافة العمليات النهائي العام الثابت

منشئو فئة BorderLayout:

    تخطيط الحدود():ينشئ تخطيطًا حدوديًا ولكن بدون وجود فجوات بين المكونات.تخطيط الحدود (int hgap، int vgap):ينشئ تخطيطًا حدوديًا مع الفجوات الأفقية والرأسية المحددة بين المكونات.

مثال لفئة BorderLayout: استخدام مُنشئ BorderLayout()

اسم الملف: Border.java

 import java.awt.*; import javax.swing.*; public class Border { JFrame f; Border() { f = new JFrame(); // creating buttons JButton b1 = new JButton('NORTH');; // the button will be labeled as NORTH JButton b2 = new JButton('SOUTH');; // the button will be labeled as SOUTH JButton b3 = new JButton('EAST');; // the button will be labeled as EAST JButton b4 = new JButton('WEST');; // the button will be labeled as WEST JButton b5 = new JButton('CENTER');; // the button will be labeled as CENTER f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction f.add(b4, BorderLayout.WEST); // b2 will be placed in the West Direction f.add(b5, BorderLayout.CENTER); // b2 will be placed in the Center f.setSize(300, 300); f.setVisible(true); } public static void main(String[] args) { new Border(); } } 

انتاج:

فئة تخطيط الحدودقم بتنزيل هذا المثال

مثال لفئة BorderLayout: استخدام مُنشئ BorderLayout(int hgap, int vgap).

يقوم المثال التالي بإدراج فجوات أفقية ورأسية بين الأزرار باستخدام مُنشئ المعلمات BorderLayout(int hgap, int الفجوة)

اسم الملف: BorderLayoutExample.java

 // import statement import java.awt.*; import javax.swing.*; public class BorderLayoutExample { JFrame jframe; // constructor BorderLayoutExample() { // creating a Frame jframe = new JFrame(); // create buttons JButton btn1 = new JButton('NORTH'); JButton btn2 = new JButton('SOUTH'); JButton btn3 = new JButton('EAST'); JButton btn4 = new JButton('WEST'); JButton btn5 = new JButton('CENTER'); // creating an object of the BorderLayout class using // the parameterized constructor where the horizontal gap is 20 // and vertical gap is 15. The gap will be evident when buttons are placed // in the frame jframe.setLayout(new BorderLayout(20, 15)); jframe.add(btn1, BorderLayout.NORTH); jframe.add(btn2, BorderLayout.SOUTH); jframe.add(btn3, BorderLayout.EAST); jframe.add(btn4, BorderLayout.WEST); jframe.add(btn5, BorderLayout.CENTER); jframe.setSize(300,300); jframe.setVisible(true); } // main method public static void main(String argvs[]) { new BorderLayoutExample(); } } 

انتاج:

فئة تخطيط الحدود

Java BorderLayout: بدون تحديد المنطقة

يمكن أن تعمل طريقة add() لفئة JFrame حتى عندما لا نحدد المنطقة. في مثل هذه الحالة، يتم عرض أحدث مكون تمت إضافته فقط في الإطار، ويتم تجاهل جميع المكونات المضافة مسبقًا. أحدث مكون يغطي المنطقة بأكملها. المثال التالي يوضح نفس الشيء.

اسم الملف: BorderLayoutWithoutRegionExample.java

 // import statements import java.awt.*; import javax.swing.*; public class BorderLayoutWithoutRegionExample { JFrame jframe; // constructor BorderLayoutWithoutRegionExample() { jframe = new JFrame(); JButton btn1 = new JButton('NORTH'); JButton btn2 = new JButton('SOUTH'); JButton btn3 = new JButton('EAST'); JButton btn4 = new JButton('WEST'); JButton btn5 = new JButton('CENTER'); // horizontal gap is 7, and the vertical gap is 7 // Since region is not specified, the gaps are of no use jframe.setLayout(new BorderLayout(7, 7)); // each button covers the whole area // however, the btn5 is the latest button // that is added to the frame; therefore, btn5 // is shown jframe.add(btn1); jframe.add(btn2); jframe.add(btn3); jframe.add(btn4); jframe.add(btn5); jframe.setSize(300,300); jframe.setVisible(true); } // main method public static void main(String argvs[]) { new BorderLayoutWithoutRegionExample(); } } 

انتاج:

فئة تخطيط الحدود