java Swing第3集:FlowLayout

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java Swing第3集:FlowLayout相关的知识,希望对你有一定的参考价值。

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

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Episode 3");
        frame.setLocation(100,100);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container content = frame.getContentPane(); //default fills components in from left to right
        content.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); //manually sets the flow
        FlowLayout layout = new FlowLayout(); //flowlayout starts with filling in as much horizontal space as possible, then vertical. It's real goal is to have everything in one row, but it will make a row if it must.
        layout.setAlignment(FlowLayout.RIGHT); //Sets the side the components will be placed on, it's center by default.
        layout.setHgap(20); //Horizontal Gap
        layout.setVgap(20); //Vertical Gap

        content.setLayout(layout); //Sets the pane to use FlowLayout

        for (int i = 1;i < 20;i++){
            frame.add(new JButton("Button #" + i));
        }

//        frame.pack();
        frame.setVisible(true);
        //episode 3 done lil boi(or gurl)
    }
}

以上是关于java Swing第3集:FlowLayout的主要内容,如果未能解决你的问题,请参考以下文章

java swing JPanel 怎么实现换行Flowlayout的布局管理器

swing之flowlayout

java swing下拉框两级联动

java Swing第6集:GridLayout

java Swing第5集:BoxLayout

java Swing第1集:制作JFrame