当前位置: 首页 > 教程 > Java Swing >

Java FlowLayout

精华
小牛编辑
136浏览
2023-03-14

1 Java FlowLayout的介绍

FlowLayout用于将组件依次排成一行(在流中)。它是小程序或面板的默认布局。

2 Java FlowLayout的字段

public static final int LEFT

public static final int RIGHT

public static final int CENTER

public static final int LEADING

public static final int TRAILING

3 Java FlowLayout的构造方法

构造方法 描述
FlowLayout() 创建具有居中对齐和默认5单位的水平和垂直间隙的流布局。
FlowLayout(int align) 创建具有给定对齐方式和默认5单位的水平和垂直间隙的流布局。
FlowLayout(int align, int hgap, int vgap) 创建具有给定对齐方式和给定水平和垂直间隙的流布局。

4 Java FlowLayout的案例

package cn.xnip;

/**
 * 小牛知识库网: https://www.xnip.cn
 */

import java.awt.*;
import javax.swing.*;  
  
public class MyFlowLayout{  
    JFrame f;
    MyFlowLayout(){
        f=new JFrame();
        f.setTitle("FlowLayout案例-小牛知识库网");

        JButton b1=new JButton("1");
        JButton b2=new JButton("2");
        JButton b3=new JButton("3");
        JButton b4=new JButton("4");
        JButton b5=new JButton("5");

        f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);

        f.setLayout(new FlowLayout(FlowLayout.RIGHT));
        //setting flow layout of right alignment

        f.setSize(300,300);
        f.setVisible(true);
    }
    public static void main(String[] args) {
        new MyFlowLayout();
    }
}

输出结果为: