logo

Java BoxLayout

ה שיעור Java BoxLayout משמש לסידור הרכיבים אנכית או אופקית. למטרה זו, מחלקת BoxLayout מספקת ארבעה קבועים. הם כדלקמן:

הערה: המחלקה BoxLayout נמצאת בחבילת javax.swing.

שדות של ClassLayout Class

    גמר סטטי ציבורי אינט X_AXIS:יישור הרכיבים הוא אופקי משמאל לימין.גמר סטטי ציבורי ב-Y_AXIS:יישור הרכיבים אנכי מלמעלה למטה.גמר סטטי ציבורי LINE_AXIS:יישור הרכיבים דומה לאופן שבו מילים מיושרות בשורה, המבוססת על המאפיין ComponentOrientation של המיכל. אם המאפיין ComponentOrientation של המיכל הוא אופקי, אז הרכיבים מיושרים אופקית; אחרת, הרכיבים מיושרים אנכית. עבור כיוונים אופקיים, יש לנו שני מקרים: משמאל לימין ומימין לשמאל. אם המאפיין value ComponentOrientation של המיכל הוא משמאל לימין, אז הרכיבים מעובדים משמאל לימין, ובעבור מימין לשמאל, העיבוד של רכיבים הוא גם מימין לשמאל. במקרה של כיוונים אנכיים, הרכיבים תמיד מוצגים מלמעלה למטה.סופי סטטי ציבורי PAGE_AXIS:יישור הרכיבים דומה לאופן שבו שורות טקסט מוצבות בדף, המבוסס על המאפיין ComponentOrientation של המיכל. אם המאפיין ComponentOrientation של המיכל הוא אופקי, אז הרכיבים מיושרים אנכית; אחרת, הרכיבים מיושרים אופקית. עבור כיוונים אופקיים, יש לנו שני מקרים: משמאל לימין ומימין לשמאל. אם המאפיין value ComponentOrientation של המיכל הוא גם משמאל לימין, אז הרכיבים מעובדים משמאל לימין, ובעבור מימין לשמאל, העיבוד של הרכיבים הוא מימין לשמאל. במקרה של כיוונים אנכיים, הרכיבים תמיד מוצגים מלמעלה למטה.

בונה של כיתת BoxLayout

    BoxLayout (מיכל c, ציר פנימי):יוצר פריסת תיבה שמסדרת את הרכיבים עם הציר הנתון.

דוגמה למחלקה BoxLayout עם Y-AXIS:

שם קובץ: BoxLayoutExample1.java



 import java.awt.*; import javax.swing.*; public class BoxLayoutExample1 extends Frame { Button buttons[]; public BoxLayoutExample1 () { buttons = new Button [5]; for (int i = 0;i<5;i++) { buttons[i]="new" button ('button ' + (i 1)); adding the buttons so that it can be displayed add (buttons[i]); } will placed horizontally setlayout (new boxlayout (this, boxlayout.y_axis)); setsize(400,400); setvisible(true); main method public static void main(string args[]){ boxlayoutexample1 b="new" boxlayoutexample1(); < pre> download this example <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/layout-manager/08/java-boxlayout.webp" alt="BoxLayout class example"> <h3>Example of BoxLayout class with X-AXIS</h3> <p> <strong>FileName:</strong> BoxLayoutExample2.java</p> <pre> import java.awt.*; import javax.swing.*; public class BoxLayoutExample2 extends Frame { Button buttons[]; public BoxLayoutExample2() { buttons = new Button [5]; for (int i = 0;i<5;i++) { buttons[i]="new" button ('button ' + (i 1)); adding the buttons so that it can be displayed add (buttons[i]); } in output will aligned vertically setlayout (new boxlayout(this, boxlayout.x_axis)); setsize(400,400); setvisible(true); main method public static void main(string args[]){ boxlayoutexample2 b="new" boxlayoutexample2(); < pre> download this example <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/layout-manager/08/java-boxlayout-2.webp" alt="BoxLayout class example"> <h3>Example of BoxLayout Class with LINE_AXIS</h3> <p>The following example shows the effect of setting the value of ComponentOrientation property of the container to RIGHT_TO_LEFT. If we do not set the value of ComponentOrientation property, then the components would be laid out from left to right. Comment line 11, and see it yourself.</p> <p> <strong>FileName:</strong> BoxLayoutExample3.java</p> <pre> // import statements import java.awt.*; import javax.swing.*; public class BoxLayoutExample3 extends Frame { Button buttons[]; // constructor of the class public BoxLayoutExample3() { buttons = new Button[5]; setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); // line 11 for (int i = 0; i <5; i++) { buttons[i]="new" button ('button ' + (i 1)); adding the buttons so that it can be displayed add (buttons[i]); } componentorientation is set to right_to_left. therefore, added will rendered from right left setlayout (new boxlayout(this, boxlayout.line_axis)); setsize(400, 400); setvisible(true); main method public static void main(string argvs[]) creating an object of class boxlayoutexample3 obj="new" boxlayoutexample3(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/layout-manager/08/java-boxlayout.webp" alt="BoxLayout class example"> <h3>Example of BoxLayout Class with PAGE_AXIS</h3> <p>The following example shows how to use PAGE_AXIS.</p> <p> <strong>FileName:</strong> BoxLayoutExample4.java</p> <pre> // import statements import java.awt.*; import javax.swing.*; public class BoxLayoutExample4 extends Frame { Button buttons[]; // constructor of the class public BoxLayoutExample4() { JFrame f = new JFrame(); JPanel pnl = new JPanel(); buttons = new Button[5]; GridBagConstraints constrntObj = new GridBagConstraints(); constrntObj.fill = GridBagConstraints.VERTICAL; for (int i = 0; i <5; i++) { buttons[i]="new" button ('button ' + (i 1)); adding the buttons so that it can be displayed add(buttons[i]); } components will just like line is present on a page setlayout (new boxlayout(this, boxlayout.page_axis)); setsize(400, 400); setvisible(true); main method public static void main(string argvs[]) creating an object of class boxlayoutexample4 obj="new" boxlayoutexample4(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/layout-manager/08/java-boxlayout-2.webp" alt="BoxLayout class example"> <p>The above output shows that the buttons are aligned horizontally. Now, we will display the buttons vertically using the PAGE_AXIS.</p> <p> <strong>FileName:</strong> BoxLayoutExample5.java</p> <pre> // import statementss import java.awt.*; import javax.swing.*; public class BoxLayoutExample5 extends Frame { Button buttons[]; // constructor of the class public BoxLayoutExample5() { JFrame f = new JFrame(); buttons = new Button[5]; // Creating a Box whose alignment is horizontal Box horizontalBox = Box.createHorizontalBox(); // ContentPane returns a container Container contentPane = f.getContentPane(); for (int i = 0; i <5; i++) { buttons[i]="new" button ('button ' + (i 1)); adding the buttons to box so that it can be displayed horizontalbox.add(buttons[i]); } and borderlayout content pane contentpane.add(horizontalbox, borderlayout.north); now, rendered components are vertically. is because aligned horizontally f.setlayout (new boxlayout(contentpane, boxlayout.page_axis)); f.setsize(400, 400); f.setvisible(true); main method public static void main(string argvs[]) creating an object of class boxlayoutexample5 obj="new" boxlayoutexample5(); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/layout-manager/08/java-boxlayout-3.webp" alt="BoxLayout class example"> <hr></5;></pre></5;></pre></5;></pre></5;i++)></pre></5;i++)>