JavaAwtSwing布局笔记之java.awt.FlowLayout

本文详细介绍了Java AWT中的FlowLayout布局管理器,包括其特点、实例化方法、对齐参数和间隔参数的设置。通过源码分析,展示了如何创建并配置FlowLayout,以及如何通过setPreferredSize()设置组件尺寸。此外,还提供了一个测试用例,展示了不同对齐方式和间隔参数的效果。
摘要由CSDN通过智能技术生成

特点

  • 可放多子部件, 从左到右,自动换行
  • 可设置对齐方式 LEFT , LEADING, CENTER , RIGHT , TRAILING
  • 可设置子部件 横向间隔setHgap(int hgap) 和 纵向间隔setVgap(int vgap)
  • Panel和JPanel默认自带FlowLayout

实例化

源码

 /**
     * Constructs a new {@code FlowLayout} with a centered alignment and a
     * default 5-unit horizontal and vertical gap.
     */
    public FlowLayout() {
        this(CENTER, 5, 5);
    }

    /**
     * Constructs a new {@code FlowLayout} with the specified
     * alignment and a default 5-unit horizontal and vertical gap.
     * The value of the alignment argument must be one of
     * {@code FlowLayout.LEFT}, {@code FlowLayout.RIGHT},
     * {@code FlowLayout.CENTER}, {@code FlowLayout.LEADING},
     * or {@code FlowLayout.TRAILING}.
     * @param align the alignment value
     */
    public FlowLayout(int align) {
        this(align, 5, 5);
    }

    /**
     * Creates a new flow layout manager with the indicated alignment
     * and the indicated horizontal and vertical gaps.
     * <p>
     * The value of the alignment argument must be one of
     * {@code FlowLayout.LEFT}, {@code FlowLayout.RIGHT},
     * {@code FlowLayout.CENTER}, {@code FlowLayout.LEADING},
     * or {@code FlowLayout.TRAILING}.
     * @param      align   the alignment value
     * @param      hgap    the horizontal gap between components
     *                     and between the components and the
     *                     borders of the {@code Container}
     * @param      vgap    the vertical gap between components
     *                     and between the components and the
     *                     borders of the {@code Container}
     */
    public FlowLayout(int align, int hgap, int vgap) {
        this.hgap = hgap;
        this.vgap = vgap;
        setAlignment(align);
    }

实例化例子

		FlowLayout flowLayout = new FlowLayout();
		flowLayout = new FlowLayout(FlowLayout.LEADING);
		flowLayout = new FlowLayout(FlowLayout.LEFT);
		flowLayout = new FlowLayout(FlowLayout.CENTER);
		flowLayout = new FlowLayout(FlowLayout.RIGHT);
		flowLayout = new FlowLayout(FlowLayout.TRAILING);
		flowLayout = new FlowLayout(FlowLayout.CENTER, 10, 10);

参数

对齐参数

FlowLayout.LEADING : 所有行对齐容器左边

FlowLayout.LEFT : 所有行左对齐

FlowLayout.CENTER : 所有行居中

FlowLayout.RIGHT : 所有行右对齐

FlowLayout.LEADING : 所有行对齐容器右边

gap间隔参数

setHgap(int hgap) : 设置横向间隔

源码👇

	/**
     * Sets the horizontal gap between components and
     * between the components and the borders of the
     * {@code Container}.
     *
     * @param hgap the horizontal gap between components
     *             and between the components and the borders
     *             of the {@code Container}
     * @see        java.awt.FlowLayout#getHgap
     * @since      1.1
     */
    public void setHgap(int hgap) {
        this.hgap = hgap;
    }

setVgap(int vgap) : 设置纵向间隔

源码👇

    /**
     * Sets the vertical gap between components and between
     * the components and the borders of the {@code Container}.
     *
     * @param vgap the vertical gap between components
     *             and between the components and the borders
     *             of the {@code Container}
     * @see        java.awt.FlowLayout#getVgap
     * @since      1.1
     */
    public void setVgap(int vgap) {
        this.vgap = vgap;
    }

子元素可以用自身.setPreferredSize(new Dimension(width,height)); 设置尺寸大小

子元素的 setLoccation(),setSize(), setBounds()没有效果
可以用setPreferredSize(new Dimension(width,height)); 设置尺寸大小




测试代码

测试用例1

package t2205201404;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FlowLayoutTest2205201607 {
	static void pln() {System.out.println("");}
	static void pln(Object o) {System.out.println(o);}
	static void pln(Object o1, Object o2) {pln(o1+"  的结果是  "+o2);}

	public static void main(String[] args) {
		Frame frame = new Frame("FlowLayoutTest");
		frame.setBounds(100,100,1600,800);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				frame.dispose();
				System.exit(0);
			}
		});
		MenuBar menuBar = new MenuBar(); frame.setMenuBar(menuBar);
		Menu menu0 = new Menu("flowLayout.setAlignment", true);
		menuBar.add(menu0);
		MenuItem mi1 = new MenuItem("FlowLayout.CENTER"); menu0.add(mi1);
		MenuItem mi2 = new MenuItem("FlowLayout.LEADING"); menu0.add(mi2);
		MenuItem mi3 = new MenuItem("FlowLayout.LEFT"); menu0.add(mi3);
		MenuItem mi4 = new MenuItem("FlowLayout.RIGHT"); menu0.add(mi4);
		MenuItem mi5 = new MenuItem("FlowLayout.TRAILING"); menu0.add(mi5);
		
		final Panel CenterPanel = new Panel(); frame.add(CenterPanel, BorderLayout.CENTER);
		
		FlowLayout fl = (FlowLayout) CenterPanel.getLayout();
		fl.setHgap(300);
		fl.setVgap(60);
		
		final Label LabelAr[] = new Label[10];
		for(int i=0; i<LabelAr.length; i++) {
			Label lb = LabelAr[i] = new Label("Label-"+i, Label.RIGHT);
			lb.setBackground(Color.CYAN);
			lb.setPreferredSize(new Dimension(200, 100));
		}
		final Button ButtonAr[] = new Button[10];
		for(int i=0; i<ButtonAr.length; i++) {
			Button btn = ButtonAr[i] = new Button("Button-"+i);
			btn.setBackground(new Color(0, 168, 250)  );
			btn.setSize(100, 100);
			btn.setPreferredSize(new Dimension(100,100));
		}
		
		
		
		
		final Panel NorthPanel = new Panel();		frame.add(NorthPanel, BorderLayout.NORTH);
//		NorthPanel.setBackground(Color.GRAY);		NorthPanel.setForeground(Color.WHITE);
		GridLayout gridLayout010 = new GridLayout(0, 6, 10, 10);
		NorthPanel.setLayout(gridLayout010);
		
		final Label SetHgapLabel = new Label("SetHgap横向间隔", Label.RIGHT); 		NorthPanel.add(SetHgapLabel);
		final TextField SetHgapInput = new TextField("1"); 						NorthPanel.add(SetHgapInput);
		final Label SetVgapLabel = new Label("SetVgap纵向间隔", Label.RIGHT); 		NorthPanel.add(SetVgapLabel);
		final TextField SetVgapInput = new TextField("1"); 						NorthPanel.add(SetVgapInput);
		
		final Label     childPreferredWidthLabel  = new Label("ChildPreferredWidth: ", Label.RIGHT); NorthPanel.add(childPreferredWidthLabel);
		final TextField childPreferredWidthInput  = new TextField("200"); NorthPanel.add(childPreferredWidthInput);
		final Label     childPreferredHeightLabel = new Label("ChildPreferredHeight: ", Label.RIGHT); NorthPanel.add(childPreferredHeightLabel);
		final TextField childPreferredHeightInput = new TextField("100"); NorthPanel.add(childPreferredHeightInput);
		
		
		
		
		class F{
			void addComponent(){
				Dimension childPreferedSizeDimension = new Dimension(200, 100);
				try {
					int hgap = Integer.parseInt(SetHgapInput.getText()); fl.setHgap(hgap);
					int vgap = Integer.parseInt(SetVgapInput.getText()); fl.setVgap(vgap);
					childPreferedSizeDimension.width  = Integer.parseInt(childPreferredWidthInput.getText()); 
					childPreferedSizeDimension.height = Integer.parseInt(childPreferredHeightInput.getText());
				}catch(Exception thr) {thr.printStackTrace();}
				for(int i=0; i<LabelAr.length; i++) {
					LabelAr[i].setPreferredSize(childPreferedSizeDimension);
					CenterPanel.add(LabelAr[i]);
				}
				for(int i=0; i<ButtonAr.length; i++) {
					try {}catch(Throwable th) {th.printStackTrace();}
					ButtonAr[i].setPreferredSize(childPreferedSizeDimension);
					CenterPanel.add(ButtonAr[i]);
				}
				frame.setVisible(true);
			}
		}
		F f = new F();
		
		mi1.addActionListener(ev->{
			fl.setAlignment(FlowLayout.CENTER);
			f.addComponent();
		});
		mi2.addActionListener(ev->{
			fl.setAlignment(FlowLayout.LEADING);
//			frame.setLayout(fl);
			f.addComponent();
		});
		mi3.addActionListener(ev->{
			fl.setAlignment(FlowLayout.LEFT);
//			frame.setLayout(fl);
			f.addComponent();
		});
		mi4.addActionListener(ev->{
			fl.setAlignment(FlowLayout.RIGHT);
			//frame.setLayout(fl);
			f.addComponent();
		});
		mi5.addActionListener(ev->{
			fl.setAlignment(FlowLayout.TRAILING);
			f.addComponent();
		});
		
		
		
		
		fl.setAlignment(FlowLayout.CENTER);
		fl.setAlignment(FlowLayout.LEADING);
		fl.setAlignment(FlowLayout.LEFT);
		fl.setAlignment(FlowLayout.RIGHT);
		fl.setAlignment(FlowLayout.TRAILING);
		
		
//		for(int i=0; i<LabelAr.length; i++) {
//			frame.add(LabelAr[i]);
//			fl.addLayoutComponent(null, LabelAr[i]);
//		}
		f.addComponent();
		
		
//		FlowLayout flowLayout = new FlowLayout();
//		flowLayout = new FlowLayout(FlowLayout.LEADING);
//		flowLayout = new FlowLayout(FlowLayout.LEFT);
//		flowLayout = new FlowLayout(FlowLayout.CENTER);
//		flowLayout = new FlowLayout(FlowLayout.RIGHT);
//		flowLayout = new FlowLayout(FlowLayout.TRAILING);
//		flowLayout = new FlowLayout(FlowLayout.CENTER, 10, 10);
		
		
		
		

	}

}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值