Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the streaming layout of JavaSwingFlowLayout

Shulou Source: shulou.com Published: 2022-06-02 11:23:52 10月03日 Update

JavaSwingFlowLayout streaming layout implementation, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

1. Overview

FlowLayout, streaming layout manager. Arrange the components horizontally, fill one row, and continue to arrange on the next line. The direction of arrangement (left to right or right to left) depends on the container's componentOrientation property (which belongs to Component), and its possible values are as follows:

ComponentOrientation.LEFT_TO_RIGHT (default) ComponentOrientation.RIGHT_TO_LEFT

The alignment of components in the same row (horizontal) is determined by the align property of FlowLayout, whose possible values are as follows:

FlowLayout.LEFT: align left FlowLayout.CENTER: align (default) FlowLayout.RIGHT: align right FlowLayout.LEADING: align to the start edge of the container direction, for example, for left to right direction, align FlowLayout.TRAILING to the left side: align to the end edge of the container direction, for example, align to the right side for the left to right direction.

The construction method of FlowLayout:

/ / default center alignment, horizontal and vertical gaps are 5 units FlowLayout () / / specify alignment, default horizontal and vertical gaps are 5 units FlowLayout (int align) / / specify how to align them, horizontal and vertical gaps FlowLayout (int align, int hgap, int vgap)

two。 Code example

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 content panel that specifies the use of streaming layout

JPanel panel = new JPanel (new FlowLayout ())

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")

Panel.add (btn01)

Panel.add (btn02)

Panel.add (btn03)

Panel.add (btn04)

Panel.add (btn05)

Jf.setContentPane (panel)

Jf.setVisible (true)

/ / PS: finally, set it to display (draw) before all the added components will be displayed

}

}

After reading the above, have you mastered the implementation of JavaSwingFlowLayout streaming layout? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Direction button horizontal layout line content container property way method component gap unit more problem helpless for this code reason depends on Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple vpn MySQL Docker