java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JColorChooser
- 所有已实现的接口:
-
ImageObserver
,MenuContainer
,Serializable
,Accessible
@JavaBean(defaultProperty="UI", description="A component that supports selecting a Color.") public class JColorChooser extends JComponent implements Accessible
JColorChooser
提供了一个控件面板,用于允许用户操纵和选择颜色。有关使用颜色选择器的信息,请参阅如何使用颜色选择器,这是《Java教程》中的一个部分。
这个类提供了三个级别的API:
- 一个静态便利方法,显示一个模态颜色选择对话框,并返回用户选择的颜色。
- 一个静态便利方法,用于创建一个颜色选择对话框,可以指定
ActionListeners
在用户按下对话框按钮时被调用。 - 直接创建
JColorChooser
面板的实例(在任何容器内)。可以添加PropertyChange
监听器以检测当前“颜色”属性何时更改。
警告: Swing 不是线程安全的。更多信息请参阅Swing的线程策略。
警告: 该类的序列化对象将不兼容未来的Swing版本。当前的序列化支持适用于短期存储或在运行相同Swing版本的应用程序之间的RMI。从1.4开始,已将所有JavaBeans的长期存储支持添加到java.beans
包中。请参阅XMLEncoder
。
- 自版本:
- 1.2
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
该类为JColorChooser
类实现了辅助功能支持。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
Modifier and TypeFieldDescriptionprotected AccessibleContext
辅助功能上下文。static final String
选择器面板数组属性名称。static final String
预览面板属性名称。static final String
选择模型属性名称。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
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
ConstructorDescription创建一个初始颜色为白色的颜色选择器面板。JColorChooser
(Color initialColor) 创建一个具有指定初始颜色的颜色选择器面板。JColorChooser
(ColorSelectionModel model) 创建一个具有指定ColorSelectionModel
的颜色选择器面板。 -
Method Summary
Modifier and TypeMethodDescriptionvoid
向颜色选择器添加一个颜色选择器面板。static JDialog
createDialog
(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) 创建并返回一个包含指定ColorChooser
面板以及“确定”、“取消”和“重置”按钮的新对话框。获取与此JColorChooser关联的AccessibleContext。返回指定的颜色面板。getColor()
从颜色选择器获取当前颜色值。boolean
获取dragEnabled
属性的值。返回显示所选颜色的预览面板。返回处理颜色选择的数据模型。getUI()
返回渲染此组件的L&F对象。返回渲染此组件的L&F类的名称。protected String
返回此JColorChooser
的字符串表示形式。移除指定的颜色面板。void
setChooserPanels
(AbstractColorChooserPanel[] panels) 指定用于选择颜色值的颜色面板。void
setColor
(int c) 将颜色选择器的当前颜色设置为指定的颜色。void
setColor
(int r, int g, int b) 将颜色选择器的当前颜色设置为指定的RGB颜色。void
将颜色选择器的当前颜色设置为指定的颜色。void
setDragEnabled
(boolean b) 设置dragEnabled
属性,必须为true
才能在此组件上启用自动拖放处理(拖放的第一部分)。void
setPreviewPanel
(JComponent preview) 设置当前预览面板。void
setSelectionModel
(ColorSelectionModel newModel) 设置包含所选颜色的模型。void
setUI
(ColorChooserUI ui) 设置渲染此组件的L&F对象。static Color
showDialog
(Component component, String title, Color initialColor) 显示一个模态颜色选择对话框,并阻塞直到对话框被隐藏。static Color
showDialog
(Component component, String title, Color initialColor, boolean colorTransparencySelectionEnabled) 显示一个模态颜色选择对话框,并阻塞直到对话框被隐藏。void
updateUI()
来自UIManager
的通知,L&F已更改。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, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, 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
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, setLayout, 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
-
Field Details
-
SELECTION_MODEL_PROPERTY
选择模型属性名称。- 参见:
-
PREVIEW_PANEL_PROPERTY
预览面板属性名称。- 参见:
-
CHOOSER_PANELS_PROPERTY
选择器面板数组属性名称。- 参见:
-
accessibleContext
辅助功能上下文。
-
-
Constructor Details
-
JColorChooser
public JColorChooser()创建一个初始颜色为白色的颜色选择器面板。 -
JColorChooser
创建一个具有指定初始颜色的颜色选择器面板。- 参数:
-
initialColor
- 在选择器中设置的初始颜色
-
JColorChooser
创建一个具有指定ColorSelectionModel
的颜色选择器面板。- 参数:
-
model
- 要使用的ColorSelectionModel
-
-
Method Details
-
showDialog
public static Color showDialog(Component component, String title, Color initialColor) throws HeadlessException 显示一个模态颜色选择对话框,并阻塞直到对话框被隐藏。如果用户按下“确定”按钮,则此方法隐藏/销毁对话框并返回所选颜色。如果用户按下“取消”按钮或关闭对话框而不按“确定”,则此方法隐藏/销毁对话框并返回null
。- 参数:
-
component
- 对话框的父Component
-
title
- 包含对话框标题的字符串 -
initialColor
- 在显示颜色选择器时设置的初始颜色 - 返回:
-
所选颜色或如果用户选择退出则为
null
- 抛出:
-
HeadlessException
- 如果GraphicsEnvironment.isHeadless()
返回true。 - 参见:
-
showDialog
public static Color showDialog(Component component, String title, Color initialColor, boolean colorTransparencySelectionEnabled) throws HeadlessException 显示一个模态颜色选择对话框,并阻塞直到对话框被隐藏。如果用户按下“确定”按钮,则此方法隐藏/销毁对话框并返回所选颜色。如果用户按下“取消”按钮或关闭对话框而不按“确定”,则此方法隐藏/销毁对话框并返回null
。- 参数:
-
component
- 对话框的父Component
-
title
- 包含对话框标题的字符串 -
initialColor
- 在显示颜色选择器时设置的初始颜色 -
colorTransparencySelectionEnabled
- 如果可以选择颜色的透明度,则为true - 返回:
-
所选颜色或如果用户选择退出则为
null
- 抛出:
-
HeadlessException
- 如果GraphicsEnvironment.isHeadless()
返回true。 - 参见:
-
createDialog
public static JDialog createDialog(Component c, String title, boolean modal, JColorChooser chooserPane, ActionListener okListener, ActionListener cancelListener) throws HeadlessException 创建并返回一个新对话框,其中包含指定的ColorChooser
面板以及“确定”、“取消”和“重置”按钮。如果按下“确定”或“取消”按钮,则对话框将自动隐藏(但不会被销毁)。如果按下“重置”按钮,则颜色选择器的颜色将重置为上次在对话框上调用show
时设置的颜色,并且对话框将保持显示状态。- 参数:
-
c
- 对话框的父组件 -
title
- 对话框的标题 -
modal
- 一个布尔值。当为true时,程序的其余部分在对话框关闭之前无法操作。 -
chooserPane
- 放置在对话框内部的颜色选择器 -
okListener
- 在按下“确定”时调用的ActionListener -
cancelListener
- 在按下“取消”时调用的ActionListener - 返回:
- 包含颜色选择器面板的新对话框
- 抛出:
-
HeadlessException
- 如果GraphicsEnvironment.isHeadless()
返回true。 - 参见:
-
getUI
返回渲染此组件的L&F对象。- 覆盖:
-
getUI
在类JComponent
中 - 返回:
-
渲染此组件的
ColorChooserUI
对象
-
setUI
@BeanProperty(hidden=true, description="The UI object that implements the color chooser\'s LookAndFeel.") public void setUI(ColorChooserUI ui) 设置渲染此组件的L&F对象。- 参数:
-
ui
-ColorChooserUI
L&F对象 - 参见:
-
updateUI
public void updateUI()来自UIManager
的通知,L&F已更改。用最新版本从UIManager
替换当前UI对象。- 覆盖:
-
updateUI
在类JComponent
中 - 参见:
-
getUIClassID
返回渲染此组件的L&F类的名称。- 覆盖:
-
getUIClassID
在类JComponent
中 - 返回:
- 字符串"ColorChooserUI"
- 参见:
-
getColor
从颜色选择器获取当前颜色值。默认情况下,这会委托给模型。- 返回:
- 颜色选择器的当前颜色值
-
setColor
@BeanProperty(bound=false, description="The current color the chooser is to display.") public void setColor(Color color) 将颜色选择器的当前颜色设置为指定的颜色。ColorSelectionModel
将触发一个ChangeEvent
- 参数:
-
color
- 要在颜色选择器中设置的颜色 - 参见:
-
setColor
public void setColor(int r, int g, int b) 将颜色选择器的当前颜色设置为指定的RGB颜色。请注意,红色、绿色和蓝色的值应在0和255之间,包括0和255。- 参数:
-
r
- 指定红色量的整数 -
g
- 指定绿色量的整数 -
b
- 指定蓝色量的整数 - 抛出:
-
IllegalArgumentException
- 如果r、g、b值超出范围 - 参见:
-
setColor
public void setColor(int c) 将颜色选择器的当前颜色设置为指定的颜色。- 参数:
-
c
- 一个整数值,用于在选择器中设置当前颜色,其中低8位指定蓝色值,接下来的8位指定绿色值,上面的8位指定红色值。
-
setDragEnabled
@BeanProperty(bound=false, description="Determines whether automatic drag handling is enabled.") public void setDragEnabled(boolean b) 设置dragEnabled
属性,必须将其设置为true
才能在此组件上启用自动拖放处理(拖放的第一部分)。拖放要执行任何操作,必须将transferHandler
属性设置为非null
值。dragEnabled
属性的默认值为false
。启用自动拖放处理后,大多数外观和感觉在用户在预览面板上按下鼠标按钮时开始拖放操作。某些外观和感觉可能不支持自动拖放;它们将忽略此属性。您可以通过修改组件直接调用
TransferHandler
的exportAsDrag
方法来解决此类外观和感觉问题。- 参数:
-
b
- 要设置dragEnabled
属性的值 - 抛出:
-
HeadlessException
- 如果b
为true
且GraphicsEnvironment.isHeadless()
返回true
- 自:
- 1.4
- 参见:
-
getDragEnabled
public boolean getDragEnabled()获取dragEnabled
属性的值。- 返回:
-
dragEnabled
属性的值 - 自:
- 1.4
- 参见:
-
setPreviewPanel
@BeanProperty(hidden=true, description="The UI component which displays the current color.") public void setPreviewPanel(JComponent preview) 设置当前预览面板。这将为名为"previewPanel"的属性触发一个PropertyChangeEvent
。- 参数:
-
preview
- 显示当前颜色的JComponent
- 参见:
-
getPreviewPanel
返回显示所选颜色的预览面板。- 返回:
-
一个
JComponent
对象 -- 预览面板
-
addChooserPanel
向颜色选择器添加颜色选择器面板。- 参数:
-
panel
- 要添加的AbstractColorChooserPanel
-
removeChooserPanel
删除指定的颜色面板。- 参数:
-
panel
- 指定要移除的面板的字符串 - 返回:
- 颜色面板
- 抛出:
-
IllegalArgumentException
- 如果面板不在已知选择器面板列表中
-
setChooserPanels
@BeanProperty(hidden=true, description="An array of different chooser types.") public void setChooserPanels(AbstractColorChooserPanel[] panels) 指定用于选择颜色值的颜色面板。- 参数:
-
panels
- 一个AbstractColorChooserPanel
对象数组
-
getChooserPanels
返回指定的颜色面板。- 返回:
-
一个
AbstractColorChooserPanel
对象数组
-
getSelectionModel
返回处理颜色选择的数据模型。- 返回:
-
一个
ColorSelectionModel
对象
-
setSelectionModel
@BeanProperty(hidden=true, description="The model which contains the currently selected color.") public void setSelectionModel(ColorSelectionModel newModel) 设置包含所选颜色的模型。- 参数:
-
newModel
- 新的ColorSelectionModel
对象
-
paramString
返回此JColorChooser
的字符串表示形式。此方法仅用于调试目的,返回的字符串的内容和格式可能因实现而异。返回的字符串可能为空,但不能为null
。- 覆盖:
-
paramString
在类JComponent
中 - 返回:
-
此
JColorChooser
的字符串表示形式
-
getAccessibleContext
获取与此JColorChooser关联的AccessibleContext。对于颜色选择器,AccessibleContext采用AccessibleJColorChooser的形式。如果需要,将创建一个新的AccessibleJColorChooser实例。- 指定:
-
getAccessibleContext
在接口Accessible
中 - 覆盖:
-
getAccessibleContext
在类Component
中 - 返回:
- 作为此JColorChooser的AccessibleContext的AccessibleJColorChooser
-