- java.lang.Object
-
- javax.swing.BoxLayout
-
- All Implemented Interfaces:
- LayoutManager, LayoutManager2, Serializable
- Direct Known Subclasses:
- DefaultMenuLayout
public class BoxLayout extends Object implements LayoutManager2, Serializable
A layout manager that allows multiple components to be laid out either vertically or horizontally. The components will not wrap so, for example, a vertical arrangement of components will stay vertically arranged when the frame is resized.Nesting multiple panels with different combinations of horizontal and vertical gives an effect similar to GridBagLayout, without the complexity. The diagram shows two panels arranged horizontally, each of which contains 3 components arranged vertically.
The BoxLayout manager is constructed with an axis parameter that specifies the type of layout that will be done. There are four choices:
X_AXIS - Components are laid out horizontally from left to right.
Y_AXIS - Components are laid out vertically from top to bottom.
LINE_AXIS - Components are laid out the way words are laid out in a line, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out horizontally, otherwise they are laid out vertically. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left. For vertical orientations components are always laid out from top to bottom.
PAGE_AXIS - Components are laid out the way text lines are laid out on a page, based on the container's ComponentOrientation property. If the container's ComponentOrientation is horizontal then components are laid out vertically, otherwise they are laid out horizontally. For horizontal orientations, if the container's ComponentOrientation is left to right then components are laid out left to right, otherwise they are laid out right to left. For vertical orientations components are always laid out from top to bottom.
For all directions, components are arranged in the same order as they were added to the container.
BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout). For a horizontal layout, if not all the components are the same height, BoxLayout attempts to make all the components as high as the highest component. If that's not possible for a particular component, then BoxLayout aligns that component vertically, according to the component's Y alignment. By default, a component has a Y alignment of 0.5, which means that the vertical center of the component should have the same Y coordinate as the vertical centers of other components with 0.5 Y alignment.
Similarly, for a vertical layout, BoxLayout attempts to make all components in the column as wide as the widest component. If that fails, it aligns them horizontally according to their X alignments. For
PAGE_AXIS
layout, horizontal alignment is done based on the leading edge of the component. In other words, an X alignment value of 0.0 means the left edge of a component if the container'sComponentOrientation
is left to right and it means the right edge of the component otherwise.Instead of using BoxLayout directly, many programs use the Box class. The Box class is a lightweight container that uses a BoxLayout. It also provides handy methods to help you use BoxLayout well. Adding components to multiple nested boxes is a powerful way to get the arrangement you want.
For further information and examples see How to Use BoxLayout, a section in The Java Tutorial.
Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the
java.beans
package. Please seeXMLEncoder
.
-
-
Field Summary
Fields Modifier and Type Field and Description static int
LINE_AXIS
Specifies that components should be laid out in the direction of a line of text as determined by the target container'sComponentOrientation
property.static int
PAGE_AXIS
Specifies that components should be laid out in the direction that lines flow across a page as determined by the target container'sComponentOrientation
property.static int
X_AXIS
Specifies that components should be laid out left to right.static int
Y_AXIS
Specifies that components should be laid out top to bottom.
-
Constructor Summary
Constructors Constructor and Description BoxLayout(Container target, int axis)
Creates a layout manager that will lay out components along the given axis.
-
Method Summary
Methods Modifier and Type Method and Description void
addLayoutComponent(Component comp, Object constraints)
Not used by this class.void
addLayoutComponent(String name, Component comp)
Not used by this class.int
getAxis()
Returns the axis that was used to lay out components.float
getLayoutAlignmentX(Container target)
Returns the alignment along the X axis for the container.float
getLayoutAlignmentY(Container target)
Returns the alignment along the Y axis for the container.Container
getTarget()
Returns the container that uses this layout manager.void
invalidateLayout(Container target)
Indicates that a child has changed its layout related information, and thus any cached calculations should be flushed.void
layoutContainer(Container target)
Called by the AWT when the specified container needs to be laid out.Dimension
maximumLayoutSize(Container target)
Returns the maximum dimensions the target container can use to lay out the components it contains.Dimension
minimumLayoutSize(Container target)
Returns the minimum dimensions needed to lay out the components contained in the specified target container.Dimension
preferredLayoutSize(Container target)
Returns the preferred dimensions for this layout, given the components in the specified target container.void
removeLayoutComponent(Component comp)
Not used by this class.
-
-
-
Field Detail
-
X_AXIS
public static final int X_AXIS
Specifies that components should be laid out left to right.- See Also:
- Constant Field Values
-
Y_AXIS
public static final int Y_AXIS
Specifies that components should be laid out top to bottom.- See Also:
- Constant Field Values
-
LINE_AXIS
public static final int LINE_AXIS
Specifies that components should be laid out in the direction of a line of text as determined by the target container'sComponentOrientation
property.- See Also:
- Constant Field Values
-
PAGE_AXIS
public static final int PAGE_AXIS
Specifies that components should be laid out in the direction that lines flow across a page as determined by the target container'sComponentOrientation
property.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BoxLayout
@ConstructorProperties(value={"target","axis"}) public BoxLayout(Container target, int axis)
Creates a layout manager that will lay out components along the given axis.- Parameters:
target
- the container that needs to be laid outaxis
- the axis to lay out components along. Can be one of:BoxLayout.X_AXIS
,BoxLayout.Y_AXIS
,BoxLayout.LINE_AXIS
orBoxLayout.PAGE_AXIS
- Throws:
AWTError
- if the value ofaxis
is invalid
-
-
Method Detail
-
getTarget
public final Container getTarget()
Returns the container that uses this layout manager.- Returns:
- the container that uses this layout manager
- Since:
- 1.6
-
getAxis
public final int getAxis()
Returns the axis that was used to lay out components. Returns one of:BoxLayout.X_AXIS
,BoxLayout.Y_AXIS
,BoxLayout.LINE_AXIS
orBoxLayout.PAGE_AXIS
- Returns:
- the axis that was used to lay out components
- Since:
- 1.6
-
invalidateLayout
public void invalidateLayout(Container target)
Indicates that a child has changed its layout related information, and thus any cached calculations should be flushed.This method is called by AWT when the invalidate method is called on the Container. Since the invalidate method may be called asynchronously to the event thread, this method may be called asynchronously.
- Specified by:
invalidateLayout
in interfaceLayoutManager2
- Parameters:
target
- the affected container- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor
-
addLayoutComponent
public void addLayoutComponent(String name, Component comp)
Not used by this class.- Specified by:
addLayoutComponent
in interfaceLayoutManager
- Parameters:
name
- the name of the componentcomp
- the component
-
removeLayoutComponent
public void removeLayoutComponent(Component comp)
Not used by this class.- Specified by:
removeLayoutComponent
in interfaceLayoutManager
- Parameters:
comp
- the component
-
addLayoutComponent
public void addLayoutComponent(Component comp, Object constraints)
Not used by this class.- Specified by:
addLayoutComponent
in interfaceLayoutManager2
- Parameters:
comp
- the componentconstraints
- constraints
-
preferredLayoutSize
public Dimension preferredLayoutSize(Container target)
Returns the preferred dimensions for this layout, given the components in the specified target container.- Specified by:
preferredLayoutSize
in interfaceLayoutManager
- Parameters:
target
- the container that needs to be laid out- Returns:
- the dimensions >= 0 && <= Integer.MAX_VALUE
- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor- See Also:
Container
,minimumLayoutSize(java.awt.Container)
,maximumLayoutSize(java.awt.Container)
-
minimumLayoutSize
public Dimension minimumLayoutSize(Container target)
Returns the minimum dimensions needed to lay out the components contained in the specified target container.- Specified by:
minimumLayoutSize
in interfaceLayoutManager
- Parameters:
target
- the container that needs to be laid out- Returns:
- the dimensions >= 0 && <= Integer.MAX_VALUE
- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor- See Also:
preferredLayoutSize(java.awt.Container)
,maximumLayoutSize(java.awt.Container)
-
maximumLayoutSize
public Dimension maximumLayoutSize(Container target)
Returns the maximum dimensions the target container can use to lay out the components it contains.- Specified by:
maximumLayoutSize
in interfaceLayoutManager2
- Parameters:
target
- the container that needs to be laid out- Returns:
- the dimenions >= 0 && <= Integer.MAX_VALUE
- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor- See Also:
preferredLayoutSize(java.awt.Container)
,minimumLayoutSize(java.awt.Container)
-
getLayoutAlignmentX
public float getLayoutAlignmentX(Container target)
Returns the alignment along the X axis for the container. If the box is horizontal, the default alignment will be returned. Otherwise, the alignment needed to place the children along the X axis will be returned.- Specified by:
getLayoutAlignmentX
in interfaceLayoutManager2
- Parameters:
target
- the container- Returns:
- the alignment >= 0.0f && <= 1.0f
- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor
-
getLayoutAlignmentY
public float getLayoutAlignmentY(Container target)
Returns the alignment along the Y axis for the container. If the box is vertical, the default alignment will be returned. Otherwise, the alignment needed to place the children along the Y axis will be returned.- Specified by:
getLayoutAlignmentY
in interfaceLayoutManager2
- Parameters:
target
- the container- Returns:
- the alignment >= 0.0f && <= 1.0f
- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor
-
layoutContainer
public void layoutContainer(Container target)
Called by the AWT when the specified container needs to be laid out.- Specified by:
layoutContainer
in interfaceLayoutManager
- Parameters:
target
- the container to lay out- Throws:
AWTError
- if the target isn't the container specified to the BoxLayout constructor
-
-
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 11/06/2005, zuletzt geändert 04/03/2020
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/java-api-rf-javax/swing/BoxLayout.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.