اذهب إلى المحتوى
  • 0

كيف أضيف عناصر إلى FlowLayout في جافا swing؟

Eng.Amal

السؤال

Recommended Posts

  • 0

تتيح FlowLayout في جافا وضع العناصر بشكل أفقي واحد تلو الآخر ابتداءً من اليسار إلى اليمين حتى انتهاء السطر ومن ثم الانتقال للسطر التالي وهي الشكل الافتراضي default layout أي أنه في حال عدم تحديد أي شكل للواجهة فإن الواجهة ستأخذ شكل FlowLayout وتقوم بترتيب العناصر على أساسها.

 يتم إنشاء FlowLayout عن طريق إنشاء غرض من الصف FlowLayout ويمكن إنشاء FlowLayout  في المُنشئ Constructor الخاص بالغرض JPanel كما في المثال التالي:

JPanel flowPanel = new JPanel(new FlowLayout());

المثال التالي يوضح كيفية إضافة العناصر إلى FlowLayot وطريقة عرضها:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class myFrame extends JFrame {
	
	JPanel pnl = new JPanel(new FlowLayout());
	JTextField txt = new JTextField(20);
	JButton btn1 = new JButton("Enter");
	JButton btn2 = new JButton("Save");
	JButton btn3 = new JButton("Add");
	JButton btn4 = new JButton("Close");
	JTextArea txtArea = new JTextArea("Hello World");

	public myFrame() {
		
		pnl.add(txt);
		pnl.add(btn1);
		pnl.add(btn2);
		pnl.add(btn3);
		pnl.add(btn4);
		pnl.add(txtArea);
		this.add(pnl);
	}
	public static void main(String[] args) {
		myFrame test = new myFrame();
		
		test.setSize(500, 500);
		test.setTitle("JFrame Example");
		test.setVisible(true);
		test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

flow_layout.thumb.png.e1723c46e7f23d3630

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...