java Swing第6集:GridLayout

Posted

tags:

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

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

public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Episode 6");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();

        JPanel panel = new JPanel();
        GridLayout layout = new GridLayout(4, 5, 20, 20); //Creating a grid layout. last 2 params for gap sizes
        //For grid layouts, if there is a number greater than 1 set for rows(first param), then the grid will ALWAYS have that amount of rows. The columns, on the other hand, can change.
        //Now, if there is a 0 for rows, and a number for columns, there will always be that number of columns, and the rows will be added or unadded accordingly
        panel.setLayout(layout);

        for (int i = 0;i<10;i++){ //Generating buttons
            panel.add(new JButton("Button"));
        }

        contentPane.add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}

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

Java Swing - JPanel 和 GridLayout 边距/填充

在JAVAswing布局设置FlowLayout和GridLayout中了怎么调节组件的大小。

java Swing第5集:BoxLayout

java Swing第3集:FlowLayout

java Swing第1集:制作JFrame

java Swing第4集:BorderLayout和CardLayout