Java programming uses the box layout manager example [Based on the swing component], java sample swing component

Source: Internet
Author: User

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 ();}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.