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

How to write the implementation code of Java Swing BoxLayout box layout

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

Share

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

Today, I will talk to you about how to write the implementation code of Java Swing BoxLayout box 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

BoxLayout, box layout manager. It arranges several components horizontally or vertically. Swing provides a container component Box that implements BoxLayout. Using the static methods provided by Box, you can quickly create horizontal / vertical box containers (Box), as well as invisible components that fill the gaps between components. Nesting a combination of horizontal and vertical boxes can achieve an effect similar to that of GridBagLayout, but not that complex.

Create a horizontal / vertical container (Box):

/ / create a horizontal container Box hBox = Box.createHorizontalBox (); / / create a vertical container Box vBox = Box.createVerticalBox ()

There are no gaps and centers between components within the Box by default, and if you want to add gaps between components (or head / tail), you can add an invisible component that affects the layout. Box provides three invisible components for filling gaps: glue, struts, and rigidAreas.

Create a gelatinous (wide / high scalable) invisible component (glue):

/ / create a horizontal gelatinous invisible component to fill the remaining horizontal space (if there is more than one component, split the remaining space equally) Component hGlue = Box.createHorizontalGlue (); / / create a vertical gelatinous invisible component to fill the vertical remaining space (if there is more than one component, divide the remaining space equally) Component vGlue = Box.createVerticalGlue () / / create a horizontal and vertical gelatinous invisible component to fill the remaining horizontal and vertical space (if there is more than one of the components, divide the remaining space equally) Component glue = Box.createGlue ()

Create a fixed width or height invisible component (struts):

/ / create a fixed width invisible component (for horizontal box) Component hStrut = Box.createHorizontalStrut (int width); / / create a fixed height invisible component (for vertical box) Component vStrut = Box.createVerticalStrut (int height)

Create a fixed width and height invisible component (rigidAreas):

/ / create a fixed width and height invisible component Component rigidArea = Box.createRigidArea (new Dimension (int width, int height))

two。 Code example

Package com.xiets.swing;import javax.swing.*;public class Main {public static void main (String [] args) {JFrame jf = new JFrame ("Test window"); jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); JButton btn01 = new JButton ("Button01"); JButton btn02 = new JButton ("Button02"); JButton btn03 = new JButton ("Button03"); JButton btn04 = new JButton ("Button04"); JButton btn05 = new JButton ("Button05") / / create the first horizontal container Box hBox01 = Box.createHorizontalBox (); hBox01.add (btn01); hBox01.add (btn02); hBox01.add (btn03); / / create the second horizontal container Box hBox02 = Box.createHorizontalBox (); hBox02.add (btn04); hBox02.add (Box.createHorizontalGlue ()); / / add a horizontal gelatinous invisible component to fill the remaining horizontal space hBox02.add (btn05) / / create a vertical container and place the above two horizontal boxes (Box nested) Box vBox = Box.createVerticalBox (); vBox.add (hBox01); vBox.add (hBox02); / / set the vertical container to the window jf.setContentPane (vBox); jf.pack (); jf.setLocationRelativeTo (null); jf.setVisible (true);}}

After reading the above, do you have any further understanding of how to write the implementation code of Java Swing BoxLayout box 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