Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement the Box layout Manager of Java Swing Class

Shulou Source: shulou.com Published: 2022-06-01 12:50:09 09月28日 Update

Today Xiaobian to share with you how to achieve the Java Swing class box layout manager of the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you will learn something after reading this article, let's take a look at it.

Box layout Manager

The BoxLayout box layout manager is often used in conjunction with Box containers, while the Box class has the following two static methods:

CreateHorizontalBox () Horizontal means horizontal, that is, BoxLayout places components horizontally from left to right.

CreateVerticalBox () Vertical means vertical, which means that BoxLayout places the components vertically from top to bottom.

In addition, Box provides static methods for determining the spacing between components

Complete code: import javax.swing.*;import java.awt.*;public class demo03 {public static void main (String [] args) {JFrame jf=new JFrame ("BoxLayout example"); Box b1=Box.createHorizontalBox (); / create horizontal container Box b2=Box.createVerticalBox (); / / create horizontal container jf.add (b1) / / add the outer horizontal container to the form b1.add (Box.createVerticalStrut); / / add the vertical frame b1.add (new JButton ("west")); b1.add (Box.createHorizontalStrut (140)); / / add the horizontal frame b1.add (new JButton ("east")); b1.add (Box.createHorizontalGlue ()) / / add horizontal glue b1.add (b2); / / add nested vertical container / / add fixed area b2.add (Box.createRigidArea (new Dimension (100j) 20)); b2.add (new JButton ("north")); / / add button 3 b2.add (Box.createVerticalGlue ()) / / add vertical component b2.add (new JButton ("south")); / / add button 4 b2.add (Box.createVerticalStrut (40)); / / add 40-long vertical frame / / set window closing action, title, size position, visibility and other jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); jf.setBounds (100100400200); jf.setVisible (true) }} these are all the contents of this article entitled "how to implement the Box layout Manager of the Java Swing Class". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Containers layout management level knowledge articles components frames horizontal content meaning buttons directions methods static different large hem between that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux NVidia MariaDB Redmi MySQL