Java Swing layout management boxlayout layout _java

Source: Internet
Author: User

This article for everyone to analyze the Java Swing layout management boxlayout layout, for everyone to refer to, the specific contents are as follows

boxlayout: You can specify whether controls are positioned horizontally or vertically in the container, more flexible than flowlayout
BoxLayout is slightly different from other layout managers, you must pass a reference to the container instance to its constructor, which uses BoxLayout. In addition, you must specify how the components in BoxLayout are laid out vertically (by columns) or horizontally (by rows). Nesting multiple panels with different combinations of horizontal and vertical components is similar to gridbaglayout, but not so complicated.
1. Constructor function
BoxLayout (Container target, int axis): Creates a layout manager that will place components along a given axis.
Line_axis: Specifies that the component should be positioned according to the text line direction determined by the Componentorientation property of the target container.
Page_axis: Specifies that the component should be placed according to the flow of text in the page determined by the Componentorientation property of the target container.
X_axis: Specifies that components should be positioned from left to right.
Y_axis: Specifies that components should be placed from top to bottom.
2. Common Methods
Getaxis (): Returns the axis used for layout components.
Getlayoutalignmentx (Container target): Returns the alignment of the container along the x-axis.
Getlayoutalignmenty (Container target): Returns the alignment of the container along the Y axis
Gettarget (): Returns a container that uses this layout manager.
3. Examples

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >import Java.awt.Container; 
 
Import Javax.swing.BoxLayout; 
Import Javax.swing.JButton; 
Import Javax.swing.JFrame; 
Import Javax.swing.UIManager; 
 
public class Boxlayoutdemo {public 
  static void Main (string[] args) { 
    try { 
      Uimanager.setlookandfeel ( Uimanager.getsystemlookandfeelclassname ()); 
    } catch (Exception e) { 
    } 
    JFrame frame = new JFrame ("BoxLayout Test"); 
    Frame.setdefaultcloseoperation (jframe.exit_on_close); 
    Container panel = Frame.getcontentpane (); 
    Panel.setlayout (new BoxLayout (panel, Boxlayout.y_axis)); 
    for (float align = 0.0f; align <= 1.0f; align = 0.25f) { 
      JButton button = new JButton ("X align =" + align); 
   
    button.setalignmentx (align); 
      Panel.add (button); 
    Frame.setsize (in); 
    Frame.setvisible (True); 
  } 
</span> 

   

4. The result

The above is the entire content of this article, I hope that you learn the Java Swing Layout management Help and inspiration, thank you for your reading.

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.