Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation code of JavaSwingGridLayout grid layout

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the implementation code of JavaSwingGridLayout grid layout, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

1. Overview

GridLayout, grid layout manager. It arranges the components of the container in the form of a rectangular grid, divides the container into rectangular grids of equal size according to rows and rows, and places a component in a grid, and the width and height of the components automatically fill the grid.

Give priority to the number of rows and total: when the number of rows and columns is set to non-zero through the constructor or the setRows and setColumns methods, the specified number of columns is ignored. The number of columns is determined by the specified number of rows and the total number of components in the layout. So, for example, if three rows and two columns are specified and nine components are added to the layout, they will be displayed as three rows and three columns. Specifying the number of columns is valid for the layout only if the number of rows is set to 00:00.

GridLayout construction method:

/ / default construction, each component occupies a row one column GridLayout () / / grid layout GridLayout (int rows, int cols) / / specifies the number of rows and columns, and specifies the horizontal and vertical grid gap GridLayout (int rows, int cols, int hgap, int vgap)

two。 Code demonstration

Package com.xiets.swing;import javax.swing.*;import java.awt.*;public class Main {public static void main (String [] args) {JFrame jf = new JFrame ("Test window"); jf.setSize (200,250); jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE)

Jf.setLocationRelativeTo (null)

/ / create a grid layout with 3 rows and 3 columns

GridLayout layout = new GridLayout (3,3)

/ / set horizontal and vertical clearance

/ / layout.setHgap (10)

/ / layout.setVgap (10)

JPanel panel = new JPanel (layout)

JButton btn01 = new JButton ("Button 01")

JButton btn02 = new JButton ("Button 02")

JButton btn03 = new JButton ("Button 03")

JButton btn04 = new JButton ("Button 04")

JButton btn05 = new JButton ("Button 05")

JButton btn06 = new JButton ("Button 06")

JButton btn07 = new JButton ("Button 07")

JButton btn08 = new JButton ("Button 08")

Panel.add (btn01)

Panel.add (btn02)

Panel.add (btn03)

Panel.add (btn04)

Panel.add (btn05)

Panel.add (btn06)

Panel.add (btn07)

Panel.add (btn08)

Jf.setContentPane (panel)

Jf.setVisible (true)

}

}

After reading the above, do you have any further understanding of the implementation code of JavaSwingGridLayout grid layout? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report