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.