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 use Java BoxLayout layout Manager

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

Share

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

This article mainly introduces "how to use Java BoxLayout layout Manager". In daily operation, I believe many people have doubts about how to use Java BoxLayout layout Manager. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java BoxLayout layout Manager". Next, please follow the editor to study!

2.4.6 BoxLayout

To simplify development, Swing introduces a new layout manager: BoxLayout. BoxLayout can place GUI components vertically and horizontally, and BoxLayout provides a simple constructor as follows:

Method name the method function BoxLayout (Container target, int axis) specifies that a target container-based BoxLayout layout manager is created, and the components in the layout manager are arranged in the axis direction. Axis has BoxLayout.X_AXIS (horizontal) and BoxLayout.Y _ AXIS (vertical > two directions).

Case 1

Use Frame and BoxLayout to complete the following image:

Code 1:

Import javax.swing.*;import java.awt.*;public class BoxLayoutDemo1 {public static void main (String [] args) {/ / 1. Create the Frame object Frame frame = new Frame ("Test BoxLayout here"); / / 2. Create a BoxLayout layout manager, and specify the container as the above frame object, and specify the component arrangement direction as vertical BoxLayout boxLayout = new BoxLayout (frame, BoxLayout.Y_AXIS); frame.setLayout (boxLayout); / / 3. Add two buttons to the frame object: frame.add (new Button ("button 1"); frame.add (new Button ("button 2")); / / 4. Set the optimal size of frame, and see frame.pack (); frame.setVisible (true);}}

In the java.swing package, a new container Box is provided, and the default layout manager for this container is BoxLayout. In most cases, the Box container is used to hold multiple GUI components, and then the Box container is added as a component to other containers to form the overall window layout.

Method name the method function static Box createHorizontalBox () creates a Box container that arranges components horizontally. Static Box createVerticalBox () creates a Box container that arranges components vertically. Case 2

Using Frame and Box, complete the following image:

Code 2:

Import javax.swing.*;import java.awt.*;public class BoxLayoutDemo2 {public static void main (String [] args) {/ / 1. Create the Frame object Frame frame = new Frame ("Test BoxLayout here"); / / 2. Create a horizontal Box and add two buttons: Box hBox = Box.createHorizontalBox (); hBox.add (new Button (horizontal button one); hBox.add (horizontal button two); / / 3. Create a vertical Box and add two buttons: Box vBox = Box.createVerticalBox (); vBox.add (new Button ("vertical button one"); vBox.add ("vertical button two"); / / 4. Add the box container to the frame container frame.add (hBox,BorderLayout.NORTH); frame.add (vBox); / / 5. Set the optimal size of frame and see frame.pack (); frame.setVisible (true);}}

Through the previous two BoxLayout demonstrations, we will find that there is no gap between the components in the container managed by it, and it is not particularly beautiful, but there will be some spacing between the components in the several layouts learned before, so how to set the spacing between components using BoxLayout?

In fact, it is very simple, we only need to add intervals where the original components need to be spaced, and each interval can be a component, but the component has no content, only plays a role of separation.

In the Box class, five convenient static methods are provided to generate these spacer components:

Method name method function static Component createHorizontalGlue () create a horizontal Glue (spacing that can be stretched in both directions) static Component createVerticalGlue () create a vertical Glue (spacing that can be stretched in both directions) static Component createHorizontalStrut (int width) create a horizontal Strut (spacing that can be stretched vertically) static Component createVerticalStrut (int height) create a specified height (height fixed Vertical Strut (spacing that can be stretched horizontally)

Case 3

Using Frame and Box, complete the following image:

Code 3:

Import javax.swing.*;import java.awt.*;public class BoxLayoutDemo3 {public static void main (String [] args) {/ / 1. Create the Frame object Frame frame = new Frame ("Test BoxLayout here"); / / 2. Create a horizontal Box and add two buttons Box hBox = Box.createHorizontalBox (); hBox.add (new Button (horizontal button one)); hBox.add (Box.createHorizontalGlue ()); / / spaced hBox.add (new Button (horizontal button two)) that can be stretched in both directions; hBox.add (horizontal button (10)) / / horizontal interval is fixed, vertical direction can stretch hBox.add (new Button ("horizontal button 3")); / / 3. Create a vertical Box and add two buttons Box vBox = Box.createVerticalBox (); vBox.add (new Button ("vertical button one")); vBox.add (Box.createVerticalGlue ()); / / spaced vBox.add (new Button ("vertical button two")) that can be stretched in both directions; vBox.add (vertical button (10)) / / Vertical interval is fixed, horizontal direction can stretch vBox.add (new Button ("vertical button three")); / / 4. Add the box container to the frame container frame.add (hBox, BorderLayout.NORTH); frame.add (vBox); / / 5. Set the optimal size of frame and see frame.pack (); frame.setVisible (true);}}

At this point, the study on "how to use the Java BoxLayout layout Manager" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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