javax.swing

接口
异常
java.lang.Object
  继承者 java.awt.Component
      继承者 java.awt.Container
          继承者 javax.swing.JComponent
              继承者 javax.swing.JSpinner
所有已实现的接口:
ImageObserver, MenuContainer, Serializable, Accessible

public class JSpinner
     
extends JComponent
implements Accessible

让用户从一个有序序列中选择一个数字或者一个对象值的单行输入字段。Spinner 通常提供一对带小箭头的按钮以便逐步遍历序列元素。键盘的向上/向下方向键也可循环遍历元素。也允许用户在 spinner 中直接输入合法值。尽管组合框提供了相似的功能,但因为 spinner 不要求可隐藏重要数据的下拉列表,所以有时它也成为首要选择。

JSpinner 序列的值由其 SpinnerModel 定义。此 model 可指定为构造方法的参数,并且可通过 model 属性进行更改。提供了针对某些常见类型的 SpinnerModel 类:SpinnerListModelSpinnerNumberModelSpinnerDateModel

JSpinner 具有一个负责显示和潜在地更改当前元素或者模型 value 的惟一子组件,被称为 editor。此编辑器由 JSpinner 的构造方法创建,并且可通过 editor 属性进行更改。JSpinner 的编辑器通过侦听 ChangeEvent 而与模型保持同步。如果用户更改了 editor 所显示的值,则有可能 model 的值与 editor 的值不一样。为保证 model 与编辑器具有相同的值,请使用 commitEdit 方法,例如:

   try {
       spinner.commitEdit();
   }
   catch (ParseException pe) {{
       // Edited value is invalid, spinner.getValue() will return
       // the last valid value, you could revert the spinner to show that:
       JComponent editor = spinner.getEditor()
       if (editor instanceof DefaultEditor) {
           ((DefaultEditor)editor).getTextField().setValue(spinner.getValue();
       }
       // reset the value to some known value:
       spinner.setValue(fallbackValue);
       // or treat the last valid value as the current, in which
       // case you don't need to do anything.
   }
   return spinner.getValue();
 

有关使用微调器的信息和示例,请参阅 The Java Tutorial 中的 How to Use Spinners 一节。

警告:Swing 不是线程安全的。有关更多信息,请参阅 Swing's Threading Policy

警告:此类的序列化对象与以后的 Swing 版本不兼容。当前序列化支持适用于短期存储,或适用于在运行相同 Swing 版本的应用程序之间进行 RMI(Remote Method Invocation,远程方法调用)。从 1.4 版本开始,已在 java.beans 包中添加了支持所有 JavaBeansTM 长期存储的功能。请参见 XMLEncoder

从以下版本开始:
1.4
另请参见:
SpinnerModel, AbstractSpinnerModel, SpinnerListModel, SpinnerNumberModel, SpinnerDateModel, JFormattedTextField

嵌套类摘要
protected  class JSpinner.AccessibleJSpinner
          AccessibleJSpinner 实现 JSpinner 类的可访问性支持。
static class JSpinner.DateEditor
          其模型为 SpinnerDateModelJSpinner 编辑器。
static class JSpinner.DefaultEditor
          一个针对非常特殊编辑器的简单基类,该类在 JFormattedTextField 中显示模型的当前值的只读视图。
static class JSpinner.ListEditor
          其模型为 SpinnerListModelJSpinner 编辑器。
static class JSpinner.NumberEditor
          其模型为 SpinnerNumberModelJSpinner 编辑器。
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
 
从类 javax.swing.JComponent 继承的字段
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
从类 java.awt.Component 继承的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JSpinner()
          构造一个 spinner,使其具有初始值为 0 并且无任何最小值或者最大值限制的 Integer SpinnerNumberModel
JSpinner(SpinnerModel model)
          构造具有一对 next/previous 按钮和 SpinnerModel 编辑器的完整 spinner。
 
方法摘要
 void addChangeListener(ChangeListener listener)
          为每次发生模型更改时要通知的列表添加侦听器。
 void commitEdit()
          将当前编辑的值提交给 SpinnerModel
protected  JComponent createEditor(SpinnerModel model)
          此方法由构造方法调用,以创建显示序列当前值的 JComponent
protected  void fireStateChanged()
          将一个其源为此 JSpinnerChangeEvent 发送到每个 ChangeListener
 AccessibleContext getAccessibleContext()
          获取此 JSpinnerAccessibleContext
 ChangeListener[] getChangeListeners()
          返回一个通过 addChangeListener() 方法添加到此 JSpinner 的所有 ChangeListener 的数组。
 JComponent getEditor()
          返回显示和潜在更改模型值的组件。
 SpinnerModel getModel()
          返回定义此 spinner 值序列的 SpinnerModel
 Object getNextValue()
          返回序列中由 getValue() 所返回的对象之后的对象。
 Object getPreviousValue()
          返回序列中由 getValue() 所返回对象之前的对象。
 SpinnerUI getUI()
          返回呈现此组件的外观 (L&F) 对象。
 String getUIClassID()
          返回用于构造呈现此组件外观 (L&F) 类的名称的后缀。
 Object getValue()
          返回模型的当前值,通常此值是 editor 所显示的值。
 void removeChangeListener(ChangeListener listener)
          从此 spinne 中移除 ChangeListener
 void setEditor(JComponent editor)
          更改显示 SpinnerModel 当前值的 JComponent
 void setModel(SpinnerModel model)
          更改表示此 spinner 值的模型。
 void setUI(SpinnerUI ui)
          设置呈现此组件的外观 (L&F) 对象。
 void setValue(Object value)
          更改模型的当前值,通常此值是 editor 所显示的值。
 void updateUI()
          用取自当前外观的值重设 UI 属性。
 
从类 javax.swing.JComponent 继承的方法
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, 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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, 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
 
从类 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, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
从类 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, 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, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

构造方法详细信息

JSpinner

public JSpinner(SpinnerModel model)
构造具有一对 next/previous 按钮和 SpinnerModel 编辑器的完整 spinner。


JSpinner

public JSpinner()
构造一个 spinner,使其具有初始值为 0 并且无任何最小值或者最大值限制的 Integer SpinnerNumberModel

方法详细信息

getUI

public SpinnerUI getUI()
返回呈现此组件的外观 (L&F) 对象。

返回:
呈现此组件的 SpinnerUI 对象

setUI

public void setUI(SpinnerUI ui)
设置呈现此组件的外观 (L&F) 对象。

参数:
ui - SpinnerUI L&F 对象
另请参见:
UIDefaults.getUI(javax.swing.JComponent)

getUIClassID

public String getUIClassID()
返回用于构造呈现此组件外观 (L&F) 类的名称的后缀。

覆盖:
JComponent 中的 getUIClassID
返回:
字符串 "SpinnerUI"
另请参见:
JComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)

updateUI

public void updateUI()
用取自当前外观的值重设 UI 属性。

覆盖:
JComponent 中的 updateUI
另请参见:
UIManager.getUI(javax.swing.JComponent)

createEditor

protected JComponent createEditor(SpinnerModel model)
此方法由构造方法调用,以创建显示序列当前值的 JComponent。编辑器也可能允许用户直接输入序列中的元素。编辑器必须侦听 model 上的 ChangeEvents,并且让显示的值与模型的值保持同步。

子类可能重写此方法以便添加对新 SpinnerModel 类的支持。作为一种选择,用户可只替换此处使用 setEditor 方法创建的编辑器。从模型类型到编辑器的默认映射是:

  • SpinnerNumberModel => JSpinner.NumberEditor
  • SpinnerDateModel => JSpinner.DateEditor
  • SpinnerListModel => JSpinner.ListEditor
  • all others => JSpinner.DefaultEditor

参数:
model - getModel 的值
返回:
显示序列当前值的组件
<