天天看點

常用的流式布局FlowLayout

流式布局FlowLayout

package Test;

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

public class FlowLayoutTest extends JFrame{
    //Jpanel 預設布局就是流式布局FlowLayout
    //排序方式左對齊,水準間距80,垂直間距30
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.LEFT,80,30));
    JButton jb1=new JButton("測試按鈕1");
    JButton jb2=new JButton("測試按鈕2");
    JButton jb3=new JButton("測試按鈕3");
    JButton jb4=new JButton("測試按鈕4");
    JButton jb5=new JButton("測試按鈕5");
    JButton jb6=new JButton("測試按鈕6");
    JButton jb7=new JButton("測試按鈕7");
    JButton jb8=new JButton("測試按鈕8");
    JButton jb9=new JButton("測試按鈕9");
    JButton jb10=new JButton("測試按鈕10");
    //建立構造函數
    public  FlowLayoutTest(){
        super("測試流式布局");
        Container contentPane =getContentPane();
        jPanel.add(jb1);
        jPanel.add(jb2);
        jPanel.add(jb3);
        jPanel.add(jb4);
        jPanel.add(jb5);
        jPanel.add(jb6);
        jPanel.add(jb7);
        jPanel.add(jb8);
        jPanel.add(jb9);
        jPanel.add(jb10);
        contentPane.add(jPanel);

        setSize(600,400); //視窗大小
        //關閉退出程式
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);//視窗不可伸縮
        setVisible(true);//視窗可視化

    }
    public static void main(String[] args) {
        new FlowLayoutTest();
    }


}