Layout Implementation
لاهمية الموضوع رآح آطول فيه شويتين
لما ابغا احدد Layout استخدم الميثود
setLayout
واضع بين القوس النوع الي ابغاه
Flow Layout
زي ما عرِفنا انه الكلاس الافتراضي للبنل فلو يعني ما اقول للبنل
كود:
setLayout(new FlowLayout())
ولكن لما ابغآ احط
FlowLayout
في فريم لازم اعرَفه
كود:
setLayout(new FlowLayout())
ليش ؟ لان الفريم الافترآَضي فيه
Border Layout
واذا ابغا اغيرها الى فلو ، فـ لازم احط الميثود لاني غيرت الافتراضي البرودر الي الشكل الجديد
الفلو
~
Panelاستخدآمه في
كود:
import java.awt.*;
import javax.swing.*;
public class FlowLayoutDemo{
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Demo");
JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Button 4");
JButton btn5 = new JButton("Button 5");
// FlowLayout is default for JPanel
JPanel panel = new JPanel();
// add buttons to the panel
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.getContentPane().add(panel);
frame.setVisible(true); } }
نتيجة البرنامج
احنآ عشان اضفنا الازرار في البنل راح يتبع الـ فلو
ورص الازرار من اليسار الي اليمين الين ما خلصت المساحة عليه
ونزل للصف الي تحت ورص ثاني
هنآ ما احتآج استخدم الميثود
setLayout
لاني استخدم البنل والفلو هو الافتراضي لها
Frame استخدآمة في
~
كود:
import java.awt.*;
import javax.swing.*;
public class JFrameExample {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout in Frame");
frame.setSize (400, 150);
//setting the wanted Layout
frame.setLayout(new FlowLayout());
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
هنآ غيرنا طريقة العرض للفريم من بوردر الى فلو
فـ استخدمنا
كود:
setLayout(new FlowLayout())
عشان يرص الازرار من اليسار لليمين
* جرب شيل سطر الميثود
setLayout
من البرنامج شوف ايش راح يتغير