JavaTM 2 Platform
Standard Ed. 6

javax.swing.plaf.basic
类 BasicComboBoxUI

java.lang.Object
  继承者 javax.swing.plaf.ComponentUI
      继承者 javax.swing.plaf.ComboBoxUI
          继承者 javax.swing.plaf.basic.BasicComboBoxUI
直接已知子类:
MetalComboBoxUI

public class BasicComboBoxUI
extends ComboBoxUI

JComboBox 的基本 UI 实现。

组合框是一个组合组件,这意味着它是很多简单组件的聚合。此类创建和管理组合框上的侦听器和组合框模型。这些侦听器更新用户界面以响应组合框的属性和状态的更改。

所有事件处理都是由利用 createxxxListener() 方法和内部类创建的侦听器类处理的。通过重写 createxxxListener() 方法,并提供自己的事件侦听器或为此类提供的事件侦听器创建子类,可以更改此类的行为。

为了添加特殊动作,需要重写 installKeyboardActions 来添加响应 KeyStroke 绑定的动作。请参见 The Swing Connection 中的文章 Keyboard Bindings in Swing


嵌套类摘要
 class BasicComboBoxUI.ComboBoxLayoutManager
          此布局管理器处理组合框的“标准”布局。
 class BasicComboBoxUI.FocusHandler
          失去焦点时,此侦听器将隐藏弹出部分。
 class BasicComboBoxUI.ItemHandler
          此侦听器观察组合框中选择的更改。
 class BasicComboBoxUI.KeyHandler
          此侦听器检查键事件是否不是导航键。
 class BasicComboBoxUI.ListDataHandler
          此侦听器观察 ComboBoxModel 中的更改。
 class BasicComboBoxUI.PropertyChangeHandler
          此侦听器观察在组合框中发生更改的绑定 (bound) 属性。
 
字段摘要
protected  JButton arrowButton
           
protected  Dimension cachedMinimumSize
           
protected  JComboBox comboBox
           
protected  CellRendererPane currentValuePane
           
protected  Component editor
           
protected  FocusListener focusListener
          此受保护字段是特定于实现的。
protected  boolean hasFocus
          此受保护字段是特定于实现的。
protected  boolean isMinimumSizeDirty
           
protected  ItemListener itemListener
          此受保护字段是特定于实现的。
protected  KeyListener keyListener
          此受保护字段是特定于实现的。
protected  JList listBox
           
protected  ListDataListener listDataListener
          此受保护字段是特定于实现的。
protected  ComboPopup popup
           
protected  KeyListener popupKeyListener
           
protected  MouseListener popupMouseListener
           
protected  MouseMotionListener popupMouseMotionListener
           
protected  PropertyChangeListener propertyChangeListener
          此受保护字段是特定于实现的。
 
构造方法摘要
BasicComboBoxUI()
           
 
方法摘要
 void addEditor()
          此公共方法是特定于实现的,应该为私有,不要调用或重写。
 void configureArrowButton()
          此公共方法是特定于实现的,应该为私有。
protected  void configureEditor()
          此受保护方法是特定于实现的,应该为私有,不要调用或重写。
protected  JButton createArrowButton()
          创建一个按钮,将使用它来控制显示或隐藏组合框的弹出部分。
protected  ComboBoxEditor createEditor()
          创建将在可编辑的组合框中使用的默认编辑器。
protected  FocusListener createFocusListener()
          创建将添加到组合框的 FocusListener
protected  ItemListener createItemListener()
          创建将添加到组合框的 ItemListener
protected  KeyListener createKeyListener()
          创建将添加到组合框的 KeyListener
protected  LayoutManager createLayoutManager()
          创建用于管理构成组合框的组件的布局管理器。
protected  ListDataListener createListDataListener()
          创建将添加到 ComboBoxModel 的列表数据侦听器。
protected  ComboPopup createPopup()
          创建组合框的弹出部分。
protected  PropertyChangeListener createPropertyChangeListener()
          创建将添加到组合框的 PropertyChangeListener
protected  ListCellRenderer createRenderer()
          创建将在不可编辑的组合框中使用的默认渲染器。
static ComponentUI createUI(JComponent c)
           
 Accessible getAccessibleChild(JComponent c, int i)
          返回对象的第 iAccessible 子对象。
 int getAccessibleChildrenCount(JComponent c)
          返回对象中的可访问子对象数。
 int getBaseline(JComponent c, int width, int height)
          返回基线。
 Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c)
          返回一个枚举,它指示该组件的基线如何随大小的改变而发生更改。
protected  Dimension getDefaultSize()
          返回使用当前渲染器和字体的组合框的空显示区域的默认大小。
protected  Dimension getDisplaySize()
          返回显示区域的计算大小。
protected  Insets getInsets()
          获取 JComboBox 的 insets。
 Dimension getMaximumSize(JComponent c)
          返回指定组件的适合外观的最大大小。
 Dimension getMinimumSize(JComponent c)
          最小大小为显示区域加上 insets 再加上按钮的大小。
 Dimension getPreferredSize(JComponent c)
          返回指定组件的适合外观的首选大小。
protected  void installComponents()
          创建并初始化构成聚合组合框的组件。
protected  void installDefaults()
          将默认颜色、默认字体、默认渲染器和默认编辑器安装到 JComboBox 中。
protected  void installKeyboardActions()
          将键盘动作添加到 JComboBox。
protected  void installListeners()
          为组合框及其模型创建并安装侦听器。
 void installUI(JComponent c)
          配置指定组件,使其适合外观。
 boolean isFocusTraversable(JComboBox c)
          确定 JComboBox 是否是可焦点遍历的。
protected  boolean isNavigationKey(int keyCode)
          返回提供的 keyCode 是否映射到用于导航的键中。
 boolean isPopupVisible(JComboBox c)
          告知弹出控件是否可见。
 void paint(Graphics g, JComponent c)
          绘制指定组件,使其适合外观。
 void paintCurrentValue(Graphics g, Rectangle bounds, boolean hasFocus)
          绘制当前所选项。
 void paintCurrentValueBackground(Graphics g, Rectangle bounds, boolean hasFocus)
          绘制当前所选项的背景。
protected  Rectangle rectangleForCurrentValue()
          返回为绘制当前所选项而保留的区域。
 void removeEditor()
          此公共方法是特定于实现的,应该为私有,不要调用或重写。
protected  void selectNextPossibleValue()
          选择列表中的下一项。
protected  void selectPreviousPossibleValue()
          选择列表中的上一项。
 void setPopupVisible(JComboBox c, boolean v)
          隐藏弹出部分。
protected  void toggleOpenClose()
          如果正在显示弹出部分,则隐藏它;如果它是隐藏的,则显示它。
 void unconfigureArrowButton()
          此公共方法是特定于实现的,应该为私有。
protected  void unconfigureEditor()
          此受保护方法是特定于实现的,应该为私有。
protected  void uninstallComponents()
          注销并卸载组成组合框的聚合组件。
protected  void uninstallDefaults()
          从 JComboBox 中卸载默认颜色、默认字体、默认渲染器和默认编辑器。
protected  void uninstallKeyboardActions()
          移除焦点 InputMap 和 ActionMap。
protected  void uninstallListeners()
          从组合框及其模型中移除已安装的侦听器。
 void uninstallUI(JComponent c)
          在 installUI 期间,在指定组件上反向执行的配置操作。
 
从类 javax.swing.plaf.ComponentUI 继承的方法
contains, update
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

comboBox

protected JComboBox comboBox

hasFocus

protected boolean hasFocus
此受保护字段是特定于实现的。不要直接访问或重写。


listBox

protected JList listBox

currentValuePane

protected CellRendererPane currentValuePane

popup

protected ComboPopup popup

editor

protected Component editor

arrowButton

protected JButton arrowButton

keyListener

protected KeyListener keyListener
此受保护字段是特定于实现的。不要直接访问或重写。可以改为重写侦听器构造方法。

另请参见:
createKeyListener()

focusListener

protected FocusListener focusListener
此受保护字段是特定于实现的。不要直接访问或重写。可以改为重写侦听器构造方法。

另请参见:
createFocusListener()

propertyChangeListener

protected PropertyChangeListener propertyChangeListener
此受保护字段是特定于实现的。不要直接访问或重写。可以改为重写侦听器构造方法。

另请参见:
createPropertyChangeListener()

itemListener

protected ItemListener itemListener
此受保护字段是特定于实现的。不要直接访问或重写。可以改为重写侦听器构造方法。

另请参见:
createItemListener()

popupMouseListener

protected MouseListener popupMouseListener

popupMouseMotionListener

protected MouseMotionListener popupMouseMotionListener

popupKeyListener

protected KeyListener popupKeyListener

listDataListener

protected ListDataListener listDataListener
此受保护字段是特定于实现的。不要直接访问或重写。可以改为重写侦听器构造方法。

另请参见:
createListDataListener()

isMinimumSizeDirty

protected boolean isMinimumSizeDirty

cachedMinimumSize

protected Dimension cachedMinimumSize
构造方法详细信息

BasicComboBoxUI

public BasicComboBoxUI()
方法详细信息

createUI

public static ComponentUI createUI(JComponent c)

installUI

public void installUI(JComponent c)
从类 ComponentUI 复制的描述
配置指定组件,使其适合外观。当 ComponentUI 实例将作为 UI 委托安装在指定组件上时,可调用此方法。此方法应该为外观完整地配置组件,包括以下方面:
  1. 在组件上安装用于颜色、字体、边框、图标、不透明性等方面的所有默认属性值。只要有可能,就 应该重写由客户端程序初始化的属性值。
  2. 如有必要,可在组件上安装一个 LayoutManager
  3. 将所需的所有子组件创建/添加到组件中。
  4. 在组件上创建/安装事件侦听器。
  5. 为了检测和适当响应组件属性更改,可在组件上创建/安装一个 PropertyChangeListener
  6. 在组件上安装键盘 UI(助记符、遍历等等)。
  7. 初始化任何适当的实例数据。

覆盖:
ComponentUI 中的 installUI
参数:
c - 将安装此 UI 委托的组件
另请参见:
ComponentUI.uninstallUI(javax.swing.JComponent), JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

uninstallUI

public void uninstallUI(JComponent c)
从类 ComponentUI 复制的描述
installUI 期间,在指定组件上反向执行的配置操作。当此 UIComponent 实例将作为 UI 委托从指定组件上移除时,可调用此方法。此方法应该取消在 installUI 中执行的配置操作,非常小心地使 JComponent 实例处于某种洁净状态(没有额外的侦听器、没有特定于外观的属性对象等等)。配置内容应该包括以下方面:
  1. 从组件中移除所有 UI 设置的边框。
  2. 从组件上移除所有 UI 设置的布局管理器。
  3. 从组件中移除所有 UI 添加的子组件。
  4. 从组件中移除所有 UI 添加的事件/属性侦听器。
  5. 从组件中移除所有 UI 安装的键盘 UI。
  6. 使所有已分配的实例数据对象无效,从而允许进行 GC 操作。

覆盖:
ComponentUI 中的 uninstallUI
参数:
c - 从中移除此 UI 委托的组件;此参数常被忽略,但如果 UI 对象是无状态的并由多个组件共享,则可以使用该参数
另请参见:
ComponentUI.installUI(javax.swing.JComponent), JComponent.updateUI()

installDefaults

protected void installDefaults()
将默认颜色、默认字体、默认渲染器和默认编辑器安装到 JComboBox 中。


installListeners

protected void installListeners()
为组合框及其模型创建并安装侦听器。安装 UI 时调用此方法。


uninstallDefaults

protected void uninstallDefaults()
从 JComboBox 中卸载默认颜色、默认字体、默认渲染器和默认编辑器。


uninstallListeners

protected void uninstallListeners()
从组合框及其模型中移除已安装的侦听器。从此方法中移除的侦听器的数目和类型应该与在 installListeners 中添加的相同。


createPopup

protected ComboPopup createPopup()
创建组合框的弹出部分。

返回:
ComboPopup 的实例
另请参见:
ComboPopup

createKeyListener

protected KeyListener createKeyListener()
创建将添加到组合框的 KeyListener。如果此方法返回 null,则不会将其添加到组合框。

返回:
KeyListener 的实例或 null

createFocusListener

protected FocusListener createFocusListener()
创建将添加到组合框的 FocusListener。如果此方法返回 null,则不会将其添加到组合框。

返回:
FocusListener 的实例或 null

createListDataListener

protected ListDataListener createListDataListener()
创建将添加到 ComboBoxModel 的列表数据侦听器。如果此方法返回 null,则不会将其添加到组合框模型。

返回:
ListDataListener 的实例或 null

createItemListener

protected ItemListener createItemListener()
创建将添加到组合框的 ItemListener。如果此方法返回 null,则不会将其添加到组合框。

子类可以重写此方法以返回其 ItemEvent 处理程序的实例。

返回:
ItemListener 的实例或 null

createPropertyChangeListener

protected PropertyChangeListener createPropertyChangeListener()
创建将添加到组合框的 PropertyChangeListener。如果此方法返回 null,则不会将其添加到组合框。

返回:
PropertyChangeListener 的实例或 null

createLayoutManager

protected LayoutManager createLayoutManager()
创建用于管理构成组合框的组件的布局管理器。

返回:
布局管理器的实例

createRenderer

protected ListCellRenderer createRenderer()
创建将在不可编辑的组合框中使用的默认渲染器。仅当没有利用 setRenderer 显式设置渲染器时才使用默认渲染器。

返回:
用于组合框的 ListCellRender
另请参见:
JComboBox.setRenderer(javax.swing.ListCellRenderer)

createEditor

protected ComboBoxEditor createEditor()
创建将在可编辑的组合框中使用的默认编辑器。仅当没有利用 setEditor 显式设置编辑器时才使用默认编辑器。

返回:
用于组合框的 ComboBoxEditor
另请参见:
JComboBox.setEditor(javax.swing.ComboBoxEditor)

installComponents

protected void installComponents()
创建并初始化构成聚合组合框的组件。此方法作为 UI 安装进程的一部分调用。


uninstallComponents

protected void uninstallComponents()
注销并卸载组成组合框的聚合组件。此方法作为 UI 卸载进程的一部分调用。


addEditor

public void addEditor()
此公共方法是特定于实现的,应该为私有,不要调用或重写。要实现特定编辑器,请创建自定义 ComboBoxEditor

另请参见:
createEditor(), JComboBox.setEditor(javax.swing.ComboBoxEditor), ComboBoxEditor

removeEditor

public void removeEditor()
此公共方法是特定于实现的,应该为私有,不要调用或重写。

另请参见:
addEditor()

configureEditor

protected void configureEditor()
此受保护方法是特定于实现的,应该为私有,不要调用或重写。

另请参见:
addEditor()

unconfigureEditor

protected void unconfigureEditor()
此受保护方法是特定于实现的,应该为私有。不要调用或重写。

另请参见:
addEditor()

configureArrowButton

public void configureArrowButton()
此公共方法是特定于实现的,应该为私有。不要调用或重写。

另请参见:
createArrowButton()

unconfigureArrowButton

public void unconfigureArrowButton()
此公共方法是特定于实现的,应该为私有。不要调用或重写。

另请参见:
createArrowButton()

createArrowButton

protected JButton createArrowButton()
创建一个按钮,将使用它来控制显示或隐藏组合框的弹出部分。

返回:
表示弹出控件的按钮

isPopupVisible

public boolean isPopupVisible(JComboBox c)
告知弹出控件是否可见。

指定者:
ComboBoxUI 中的 isPopupVisible

setPopupVisible

public void setPopupVisible(JComboBox c,
                            boolean v)
隐藏弹出部分。

指定者:
ComboBoxUI 中的 setPopupVisible

isFocusTraversable

public boolean isFocusTraversable(JComboBox c)
确定 JComboBox 是否是可焦点遍历的。如果 JComboBox 可编辑,则其返回 false;否则,返回 true。

指定者:
ComboBoxUI 中的 isFocusTraversable

paint

public void paint(Graphics g,
                  JComponent c)
从类 ComponentUI 复制的描述
绘制指定组件,使其适合外观。在将要绘制指定组件时,从 ComponentUI.update 方法中调用此方法。子类应该重写此方法并使用指定 Graphics 对象来呈现组件的内容。

覆盖:
ComponentUI 中的 paint
参数:
g - 将在其中进行绘制的 Graphics 上下文
c - 将绘制的组件;此参数常被忽略,但如果 UI 对象是无状态的并由多个组件共享,则可以使用该参数
另请参见:
ComponentUI.update(java.awt.Graphics, javax.swing.JComponent)

getPreferredSize

public Dimension getPreferredSize(JComponent c)
从类 ComponentUI 复制的描述
返回指定组件的适合外观的首选大小。如果返回 null,则首选大小将由组件的布局管理器计算(对于安装了特定布局管理器的组件而言,这是首选方法)。此方法的默认实现返回 null

覆盖:
ComponentUI 中的 getPreferredSize
参数:
c - 将查询其首选大小的组件;此参数常被忽略,但如果 UI 对象是无状态的并由多个组件共享,则可以使用该参数
另请参见:
JComponent.getPreferredSize(), LayoutManager.preferredLayoutSize(java.awt.Container)

getMinimumSize

public Dimension getMinimumSize(JComponent c)
最小大小为显示区域加上 insets 再加上按钮的大小。

覆盖:
ComponentUI 中的 getMinimumSize
参数:
c - 将查询其最小大小的组件;此参数常被忽略,但如果 UI 对象是无状态的并由多个组件共享,则可以使用该参数
返回:
一个 Dimension 对象或 null
另请参见:
JComponent.getMinimumSize(), LayoutManager.minimumLayoutSize(java.awt.Container), ComponentUI.getPreferredSize(javax.swing.JComponent)

getMaximumSize

public Dimension getMaximumSize(JComponent c)
从类 ComponentUI 复制的描述
返回指定组件的适合外观的最大大小。如果返回 null,则最大大小将由组件的布局管理器计算(对于安装了特定布局管理器的组件而言,这是首选方法)。此方法的默认实现调用 getPreferredSize 并返回该值。

覆盖:
ComponentUI 中的 getMaximumSize
参数:
c - 将查询其最大大小的组件;此参数常被忽略,但如果 UI 对象是无状态的并由多个组件共享,则可以使用该参数
返回:
一个 Dimension 对象或 null
另请参见:
JComponent.getMaximumSize(), LayoutManager2.maximumLayoutSize(java.awt.Container)

getBaseline

public int getBaseline(JComponent c,
                       int width,
                       int height)
返回基线。

覆盖:
ComponentUI 中的 getBaseline
参数:
c - 为其请求基线的 JComponent
width - 为其获取基线的宽度
height - 为其获取基线的高度
返回:
基线;如果没有合理的基线,则返回 < 0 的值
抛出:
NullPointerException - 如果 cnull
IllegalArgumentException - 如果宽度或高度 < 0
从以下版本开始:
1.6
另请参见:
JComponent.getBaseline(int, int)

getBaselineResizeBehavior

public Component.BaselineResizeBehavior getBaselineResizeBehavior(JComponent c)
返回一个枚举,它指示该组件的基线如何随大小的改变而发生更改。

覆盖:
ComponentUI 中的 getBaselineResizeBehavior
参数:
c - 为其返回调整大小行为的 JComponent
返回:
一个枚举,指示基线如何随组件大小的改变而发生更改
抛出:
NullPointerException - 如果 cnull
从以下版本开始:
1.6
另请参见:
JComponent.getBaseline(int, int)

getAccessibleChildrenCount

public int getAccessibleChildrenCount(JComponent c)
从类 ComponentUI 复制的描述
返回对象中的可访问子对象数。如果此对象的所有子对象都实现 Accessible,那么此方法应返回此对象的子对象数。如果 UI 在可看作组件的屏幕上呈现一些区域,那么它们可能希望重写此方法,但实际组件并不用于呈现那些区域。 注:从 v1.3 开始,建议开发人员调用 Component.AccessibleAWTComponent.getAccessibleChildrenCount() 替代此方法。

覆盖:
ComponentUI 中的 getAccessibleChildrenCount
返回:
该对象中可访问的子对象数
另请参见:
ComponentUI.getAccessibleChild(javax.swing.JComponent, int)

getAccessibleChild

public Accessible getAccessibleChild(JComponent c,
                                     int i)
从类 ComponentUI 复制的描述
返回对象的第 iAccessible 子对象。如果 UI 在可看作组件的屏幕上呈现一些区域,那么它们可能需要重写此方法,但实际组件并不用于呈现那些区域。

注:从 v1.3 开始,建议开发人员调用 Component.AccessibleAWTComponent.getAccessibleChild() 替代此方法。

覆盖:
ComponentUI 中的 getAccessibleChild
i - 子对象的从零开始的索引
返回:
对象的第 iAccessible 子对象
另请参见:
ComponentUI.getAccessibleChildrenCount(javax.swing.JComponent)

isNavigationKey

protected boolean isNavigationKey(int keyCode)
返回提供的 keyCode 是否映射到用于导航的键中。它用于仅通过将非导航键传递到提前键入机制来优化键输入。如果子类更改导航键,则其应该重写此方法。


selectNextPossibleValue

protected void selectNextPossibleValue()
选择列表中的下一项。如果当前所选项已经是最后一项,则其不更改选择。


selectPreviousPossibleValue

protected void selectPreviousPossibleValue()
选择列表中的上一项。如果当前选择的项已经是第一项,则其不更改选择。


toggleOpenClose

protected void toggleOpenClose()
如果正在显示弹出部分,则隐藏它;如果它是隐藏的,则显示它。


rectangleForCurrentValue

protected Rectangle rectangleForCurrentValue()
返回为绘制当前所选项而保留的区域。


getInsets

protected Insets getInsets()
获取 JComboBox 的 insets。


paintCurrentValue

public void paintCurrentValue(Graphics g,
                              Rectangle bounds,
                              boolean hasFocus)
绘制当前所选项。


paintCurrentValueBackground

public void paintCurrentValueBackground(Graphics g,
                                        Rectangle bounds,
                                        boolean hasFocus)
绘制当前所选项的背景。


getDefaultSize

protected Dimension getDefaultSize()
返回使用当前渲染器和字体的组合框的空显示区域的默认大小。

返回:
空显示区域的大小
另请参见:
getDisplaySize()

getDisplaySize

protected Dimension getDisplaySize()
返回显示区域的计算大小。显示区域是组合框的一部分,用于显示所选项。如果已经将显示区域设置为原型显示值,则此方法将使用原型显示值。

对于项数不多的组合框,建议使用原型显示值,使用原型显示值可以大大加速显示大小的计算。

返回:
根据组合框项计算出的显示区域的大小
另请参见:
JComboBox.setPrototypeDisplayValue(java.lang.Object)

installKeyboardActions

protected void installKeyboardActions()
将键盘动作添加到 JComboBox。Enter 键和 Esc 键上的动作已提供。根据需要添加更多动作。


uninstallKeyboardActions

protected void uninstallKeyboardActions()
移除焦点 InputMap 和 ActionMap。


JavaTM 2 Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java 2 SDK SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2004 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策