javax.swing

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

public class JProgressBar
     
extends JComponent
implements SwingConstants, Accessible

以可视化形式显示某些任务进度的组件。在任务的完成进度中,进度条显示该任务完成的百分比。此百分比通常由一个矩形以可视化形式表示,该矩形开始是空的,随着任务的完成逐渐被填充。此外,进度条可显示此百分比的文本表示形式。

JProgressBar 使用 BoundedRangeModel 作为其数据模型,以 value 属性表示该任务的“当前”状态,minimummaximum 属性分别表示开始点和结束点。

要指示正在执行一个未知长度的任务,可以将进度条设置为不确定模式。不确定模式的进度条持续地显示动画来表示正进行的操作。一旦可以确定任务长度和进度量,则应该更新进度条的值,将其切换回确定模式。

下面是一个创建进度条的示例,其中 task 为返回关于该任务进度信息的对象(表示某部分操作):

progressBar = new JProgressBar(0, task.getLengthOfTask());
progressBar.setValue(0);
progressBar.setStringPainted(true);
下面是查询该任务的当前状态以及使用返回值更新进度条值的示例:
progressBar.setValue(task.getCurrent());
下面的示例将进度条设置为不确定模式,然后在知道任务长度后切换回确定模式:
progressBar = new JProgressBar();
...//when the task of (initially) unknown length begins:
progressBar.setIndeterminate(true);
...//do some work; get length of task...
progressBar.setMaximum(newLength);
progressBar.setValue(newValue);
progressBar.setIndeterminate(false);

有关进一步的文档和完整的示例,请参阅 The Java Tutorial 中的 How to Monitor Progress 一节。

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

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

另请参见:
BasicProgressBarUI, BoundedRangeModel, SwingWorker

嵌套类摘要
protected  class JProgressBar.AccessibleJProgressBar
          此类实现 JProgressBar 类的可访问性支持。
 
从类 javax.swing.JComponent 继承的嵌套类/接口
JComponent.AccessibleJComponent
 
从类 java.awt.Container 继承的嵌套类/接口
Container.AccessibleAWTContainer
 
从类 java.awt.Component 继承的嵌套类/接口
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
字段摘要
protected  ChangeEvent changeEvent
          每个实例只需要一个 ChangeEvent,因为事件的唯一感兴趣属性是不可变源,即进度条。
protected  ChangeListener changeListener
          侦听由进度条的模型发送的更改事件,并将其再次指派到在此进度条上注册的更改事件侦听器。
protected  BoundedRangeModel model
          保存进度条数据的对象。
protected  int orientation
          进度条是水平的还是垂直的。
protected  boolean paintBorder
          是否显示围绕进度条的边框。
protected  boolean paintString
          是否在进度条上显示文本的字符串。
protected  String progressString
          可以在进度条上显示的可选字符串。
 
从类 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
 
从接口 javax.swing.SwingConstants 继承的字段
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
从接口 java.awt.image.ImageObserver 继承的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
构造方法摘要
JProgressBar()
          创建一个显示边框但不带进度字符串的水平进度条。
JProgressBar(BoundedRangeModel newModel)
          创建使用指定的保存进度条数据模型的水平进度条。
JProgressBar(int orient)
          创建具有指定方向(SwingConstants.VERTICALSwingConstants.HORIZONTAL)的进度条。
JProgressBar(int min, int max)
          创建具有指定最小值和最大值的水平进度条。
JProgressBar(int orient, int min, int max)
          创建使用指定方向、最小值和最大值的进度条。
 
方法摘要
 void addChangeListener(ChangeListener l)
          将指定的 ChangeListener 添加到进度条。
protected  ChangeListener createChangeListener()
          要以不同方式处理来自模型的更改事件的子类可以重写此方法,以返回自定义 ChangeListener 实现的实例。
protected  void fireStateChanged()
          将一个 ChangeEvent(其源是此 JProgressBar)发送给针对 ChangeEvent 的所有已注册 ChangeListener
 AccessibleContext getAccessibleContext()
          获得与此 JProgressBar 关联的 AccessibleContext
 ChangeListener[] getChangeListeners()
          返回通过 addChangeListener 添加到此进度条的所有 ChangeListener 组成的数组。
 int getMaximum()
          从 BoundedRangeModel 返回进度条的 maximum 值。
 int getMinimum()
          从 BoundedRangeModel 返回进度条的 minimum 值。
 BoundedRangeModel getModel()
          返回此进度条使用的数据模型。
 int getOrientation()
          根据进度条的方向返回 SwingConstants.VERTICALSwingConstants.HORIZONTAL
 double getPercentComplete()
          返回进度条的完成百分比。
 String getString()
          返回当前进度的 String 表示形式。
 ProgressBarUI getUI()
          返回呈现此组件的外观对象。
 String getUIClassID()
          返回呈现此组件的外观类的名称。
 int getValue()
          从 BoundedRangeModel 返回进度条的当前 value
 boolean isBorderPainted()
          返回 borderPainted 属性。
 boolean isIndeterminate()
          返回 indeterminate 属性的值。
 boolean isStringPainted()
          返回 stringPainted 属性的值。
protected  void paintBorder(Graphics g)
          如果 borderPainted 属性为 true,则绘制进度条的边框。
protected  String paramString()
          返回此 JProgressBar 的字符串表示形式。
 void removeChangeListener(ChangeListener l)
          从进度条移除 ChangeListener
 void setBorderPainted(boolean b)
          设置 borderPainted 属性,如果进度条应该绘制其边框,则此属性为 true
 void setIndeterminate(boolean newValue)
          设置进度条的 indeterminate 属性,该属性确定进度条处于确定模式中还是处于不确定模式中。
 void setMaximum(int n)
          将进度条的最大值(存储在进度条的数据模型中)设置为 n
 void setMinimum(int n)
          将进度条的最小值(存储在进度条的数据模型中)设置为 n
 void setModel(BoundedRangeModel newModel)
          设置 JProgressBar 使用的数据模型。
 void setOrientation(int newOrientation)
          将进度条的方向设置为 newOrientation(必须为 SwingConstants.VERTICALSwingConstants.HORIZONTAL)。
 void setString(String s)
          设置进度字符串的值。
 void setStringPainted(boolean b)
          设置 stringPainted 属性的值,该属性确定进度条是否应该呈现进度字符串。
 void setUI(ProgressBarUI ui)
          设置呈现此组件的外观对象。
 void setValue(int n)
          将进度条的当前值设置为 n
 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, paintChildren, paintComponent, paintImmediately, paintImmediately, 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,

JDK 1.6 中文手册