Java programming uses the box layout manager example [Based on the swing component], java sample swing component
This article describes how to use the box layout manager for Java programming. We will share this with you for your reference. The details are as follows:
Let's take a look at the running effect:
The complete code is as follows:
Package awtDemo; import java. awt. borderLayout; import java. awt. container; import javax. swing. box; import javax. swing. JButton; import javax. swing. JFrame; import javax. swing. JLabel; import javax. swing. JScrollPane; import javax. swing. JTextArea; import javax. swing. JTextField;/*** use the box layout manager ** @ author HAN **/public class BoxLayout_1 extends JFrame {/*****/private static final long serialVersionUID = 689692 5750596855002L; public BoxLayout_1 () {// TODO Auto-generated constructor stub Container container = getContentPane (); Box box = Box. createVerticalBox (); container. add (box, BorderLayout. NORTH); box. add (Box. createVerticalStrut (5); Box topicBox = Box. createHorizontalBox (); box. add (topicBox); topicBox. setAlignmentX (1); topicBox. add (Box. createHorizontalStrut (5); JLabel topicLabel = new JLabel ("topic :"); TopicBox. add (topicLabel); topicBox. add (Box. createHorizontalStrut (5); JTextField topicTextField = new JTextField (30); topicBox. add (topicTextField); Box box2 = Box. createVerticalBox (); container. add (box2, BorderLayout. CENTER); Box contentBox = Box. createHorizontalBox (); contentBox. setAlignmentX (1); box2.add (Box. createVerticalStrut (5); box2.add (contentBox); contentBox. add (Box. createHorizontalStru T (5); JLabel contentLabel = new JLabel ("content:"); contentLabel. setAlignmentY (0); contentBox. add (contentLabel); contentBox. add (Box. createHorizontalStrut (5); StringBuilder stringBuilder = new StringBuilder (); String contentString = new String ("align right of components using the box layout manager" + "and align up, and control the spacing between components! "); StringBuilder. append (contentString); stringBuilder. append ("\ n"); stringBuilder. append (contentString); contentString = stringBuilder. toString (); JTextArea contentTextArea = new JTextArea (contentString, 3, 30); contentTextArea. setLineWrap (true); JScrollPane scrollPane = new JScrollPane (); scrollPane. setAlignmentY (0); scrollPane. setViewportView (contentTextArea); contentBox. add (scrollPane); contentBox. add (Box. createHorizontalStrut (5); // System. out. println (contentTextArea. requestFocusInWindow (); box2.add (Box. createVerticalStrut (5); JButton submitButton = new JButton ("OK"); box2.add (submitButton); submitButton. setAlignmentX (1); box2.add (Box. createVerticalStrut (5);}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub BoxLayout_1 frame = new BoxLayout_1 (); frame. setTitle ("www.jb51.net-use the box layout manager"); frame. setVisible (true); frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. pack ();}}