- 所有已实现的接口:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
Box
类可以创建几种影响布局的不可见组件:glue、struts 和 rigid areas。如果您的 Box
包含的所有组件都具有固定大小,您可能希望使用一个 glue 组件(通过 createGlue
返回)来控制组件的位置。如果需要在两个组件之间固定的空间量,请尝试使用 strut(createHorizontalStrut
或 createVerticalStrut
)。如果需要一个始终占据相同空间量的不可见组件,请通过调用 createRigidArea
获取。
如果您正在实现一个 BoxLayout
,您可以在如何使用 BoxLayout中找到更多信息和示例,这是《Java 教程》中的一个部分。
警告: 该类的序列化对象将不兼容未来的 Swing 版本。当前的序列化支持适用于短期存储或在运行相同 Swing 版本的应用程序之间的 RMI。从 1.4 版本开始,已将对所有 JavaBeans 的长期存储支持添加到 java.beans
包中。请参阅 XMLEncoder
。
- 自版本:
- 1.2
- 参见:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
该类为Box
类实现了辅助功能支持。static class
一个轻量级组件的实现,用于参与布局但没有视图。Nested classes/interfaces declared in class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces declared in class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields declared in class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Component
创建一个不可见的“glue”组件,在具有最大宽度(对于水平框)或高度(对于垂直框)的可见组件的 Box 中可能很有用。static Box
创建一个从左到右显示其组件的Box
。static Component
创建一个水平 glue 组件。static Component
createHorizontalStrut
(int width) 创建一个不可见的、固定宽度的组件。static Component
创建一个始终具有指定大小的不可见组件。static Box
创建一个从上到下显示其组件的Box
。static Component
创建一个垂直 glue 组件。static Component
createVerticalStrut
(int height) 创建一个不可见的、固定高度的组件。获取与此 Box 关联的 AccessibleContext。protected void
绘制此Box
。void
抛出 AWTError,因为 Box 只能使用 BoxLayout。Methods declared in class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setFont, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
Methods declared in class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, transferFocusDownCycle, validate, validateTree
Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
Box
public Box(int axis) 创建一个沿指定轴显示其组件的Box
。- 参数:
-
axis
- 可以是BoxLayout.X_AXIS
、BoxLayout.Y_AXIS
、BoxLayout.LINE_AXIS
或BoxLayout.PAGE_AXIS
。 - 抛出:
-
AWTError
- 如果axis
无效 - 参见:
-
-
Method Details
-
createHorizontalBox
创建一个从左到右显示其组件的Box
。如果您希望一个尊重组件方向的Box
,您应该使用构造函数创建Box
并传入BoxLayout.LINE_AXIS
,例如:Box lineBox = new Box(BoxLayout.LINE_AXIS);
- 返回:
- 该框
-
createVerticalBox
创建一个从上到下显示其组件的Box
。如果您希望一个尊重组件方向的Box
,您应该使用构造函数创建Box
并传入BoxLayout.PAGE_AXIS
,例如:Box lineBox = new Box(BoxLayout.PAGE_AXIS);
- 返回:
- 该框
-
createRigidArea
创建一个始终具有指定大小的不可见组件。- 参数:
-
d
- 不可见组件的尺寸 - 返回:
- 该组件
- 参见:
-
createHorizontalStrut
创建一个不可见的、固定宽度的组件。在水平框中,您通常使用此方法来强制两个组件之间有一定的空间量。在垂直框中,您可能使用此方法来强制框至少具有指定的宽度。除非有多余的空间,否则不可见组件没有高度,此时它将占用可用空间的份额,就像任何没有最大高度的其他组件一样。- 参数:
-
width
- 不可见组件的宽度,像素数 >= 0 - 返回:
- 该组件
- 参见:
-
createVerticalStrut
创建一个不可见的、固定高度的组件。在垂直框中,您通常使用此方法来强制两个组件之间有一定的空间量。在水平框中,您可能使用此方法来强制框至少具有指定的高度。除非有多余的空间,否则不可见组件没有宽度,此时它将占用可用空间的份额,就像任何没有最大宽度的其他组件一样。- 参数:
-
height
- 不可见组件的高度,像素数 >= 0 - 返回:
- 该组件
- 参见:
-
createGlue
创建一个不可见的“glue”组件,在具有最大宽度(对于水平框)或高度(对于垂直框)的可见组件的 Box 中可能很有用。您可以将 glue 组件视为一种粘性物质,它会根据需要扩展以填充其相邻组件之间的空间。例如,假设您有一个水平框,其中包含两个固定大小的组件。如果框获得额外空间,固定大小的组件不会变大,那么额外空间去哪里了?没有 glue,额外空间会出现在第二个组件的右侧。如果在固定大小的组件之间放置 glue,则额外空间会出现在那里。如果在第一个固定大小的组件之前需要 glue,额外空间会出现在那里,并且固定大小的组件会被推到框的右边缘。如果在第一个固定大小的组件之前和第二个固定大小的组件之后放置 glue,则固定大小的组件将居中在框中。
要使用 glue,请调用
Box.createGlue
并将返回的组件添加到容器中。glue 组件没有最小或首选大小,因此除非有多余的空间,否则它不占用空间。如果有多余的空间,则 glue 组件将占用可用的水平或垂直空间份额,就像任何没有最大宽度或高度的其他组件一样。- 返回:
- 该组件
-
createHorizontalGlue
创建一个水平 glue 组件。- 返回:
- 该组件
-
createVerticalGlue
创建一个垂直 glue 组件。- 返回:
- 该组件
-
setLayout
抛出 AWTError,因为 Box 只能使用 BoxLayout。 -
paintComponent
绘制此Box
。如果此Box
具有 UI,则此方法调用超类的实现,否则如果此Box
是不透明的,则使用背景填充Graphics
。- 覆盖:
-
paintComponent
在类JComponent
中 - 参数:
-
g
- 要绘制的Graphics
- 抛出:
-
NullPointerException
- 如果g
为 null - 自版本:
- 1.6
- 参见:
-
getAccessibleContext
获取与此 Box 相关联的 AccessibleContext。对于 Box,AccessibleContext 采用 AccessibleBox 的形式。如果需要,将创建一个新的 AccessibleAWTBox 实例。- 指定者:
-
getAccessibleContext
在接口Accessible
- 覆盖:
-
getAccessibleContext
在类Component
- 返回:
- 作为此 Box 的 AccessibleContext 的 AccessibleBox
-