JComponent 类

原文: https://docs.oracle.com/javase/tutorial/uiswing/components/jcomponent.html

除顶级容器外,名称以“J”开头的所有 Swing 组件都来自 JComponent 类。例如,JPanelJScrollPaneJButtonJTable都继承自JComponent。但是,JFrameJDialog不会,因为它们实现了顶级容器。

JComponent类扩展了 Container 类,它本身扩展了 ComponentComponent类包括从提供布局提示到支持绘画和事件的所有内容。 Container类支持将组件添加到容器并进行布局。本节的 API 表总结了ComponentContainer以及JComponent最常用的方法。

JComponent类为其后代提供以下功能:

By specifying a string with the setToolTipText method, you can provide help to users of a component. When the cursor pauses over the component, the specified string is displayed in a small window that appears near the component. See How to Use Tool Tips for more information.

The setBorder method allows you to specify the border that a component displays around its edges. To paint the inside of a component, override the paintComponent method. See How to Use Borders and Performing Custom Painting for details.

Behind the scenes, each JComponent object has a corresponding ComponentUI object that performs all the drawing, event handling, size determination, and so on for that JComponent. Exactly which ComponentUI object is used depends on the current look and feel, which you can set using the UIManager.setLookAndFeel method. See How to Set the Look and Feel for details.

You can associate one or more properties (name/object pairs) with any JComponent. For example, a layout manager might use properties to associate a constraints object with each JComponent it manages. You put and get properties using the putClientProperty and getClientProperty methods. For general information about properties, see Properties.

Although the Component class provides layout hint methods such as getPreferredSize and getAlignmentX, it doesn’t provide any way to set these layout hints, short of creating a subclass and overriding the methods. To give you another way to set layout hints, the JComponent class adds setter methods — setMinimumSize, setMaximumSize, setAlignmentX, and setAlignmentY. See Laying Out Components Within a Container for more information.

The JComponent class provides API and basic functionality to help assistive technologies such as screen readers get information from Swing components, For more information about accessibility, see How to Support Assistive Technologies.

The JComponent class provides API to set a component’s transfer handler, which is the basis for Swing’s drag and drop support. See Introduction to DnD for details.

Double buffering smooths on-screen painting. For details, see Performing Custom Painting.

This feature makes components react when the user presses a key on the keyboard. For example, in many look and feels when a button has the focus, typing the Space key is equivalent to a mouse click on the button. The look and feel automatically sets up the bindings between pressing and releasing the Space key and the resulting effects on the button. For more information about key bindings, see How to Use Key Bindings.

JComponent类提供了许多新方法,并从ComponentContainer继承了许多方法。下表总结了我们最常使用的方法。

方法 目的
void setBorder(Border)

Border getBorder() | 设置或获取组件的边框。有关详细信息,请参见如何使用边框。 | | void setForeground(Color) void setBackground(Color) | 设置组件的前景色或背景色。前景通常是用于在组件中绘制文本的颜色。假设组件是不透明的,背景是(毫不奇怪)组件背景区域的颜色。 | | 颜色 getForeground() 颜色 getBackground() | 获取组件的前景色或背景色。 | | void setOpaque(boolean) boolean isOpaque() | 设置或获取组件是否不透明。不透明组件用其背景颜色填充其背景。 | | void setFont(Font) Font getFont() | 设置或获取组件的字体。如果尚未为组件设置字体,则返回其父级的字体。 | | void setCursor(Cursor) Cursor getCursor() | 设置或获取光标显示在组件及其包含的所有组件上(除了具有自己的光标集的子组件)。示例:aPanel.setCursor( Cursor.getPredefinedCursor( Cursor.WAIT_CURSOR)); |

方法 目的
void setComponentPopupMenu(JPopupMenu) 设置JComponentJPopupMenu。 UI 负责注册绑定并添加必要的监听器,以便在适当的时间显示JPopupMenu。当显示JPopupMenu取决于外观:有些可能在鼠标事件上显示,有些可能启用键绑定。
如果popup为空,getInheritsPopupMenu返回true,则getComponentPopupMenu将被委托给父项。这提供了一种使所有子组件继承父组件的popupmenu的方法。
void setTransferHandler(TransferHandler)

TransferHandler getTransferHandler() | 设置或删除transferHandler属性。 TransferHandler支持通过剪切,复制或粘贴到剪贴板或从剪贴板交换数据以及拖放。有关详细信息,请参见 DnD 简介。 | | void setToolTipText(String) | 设置要在工具提示中显示的文本。有关详细信息,请参阅如何使用工具提示。 | | void setName(String) String getName() | 设置或获取组件的名称。当您需要将文本与不显示文本的组件相关联时,这非常有用。 | | boolean isShowing() | 确定组件是否在屏幕上显示。这意味着组件必须是可见的,并且必须位于可见和显示的容器中。 | | void setEnabled(boolean) boolean isEnabled() | 设置或获取组件是否已启用。启用的组件可以响应用户输入并生成事件。 | | void setVisible(boolean) boolean isVisible() | 设置或获取组件是否可见。组件最初是可见的,顶级组件除外。 |

Writing Event Listeners for details)

方法 目的
void addHierarchyListener(hierarchyListener l)

void removeHierarchyListener(hierarchyListener l) | 添加或删除指定的层次结构监听器,以便在此容器所属的层次结构发生更改时从此组件接收层次结构更改事件。如果 listener l 为 null,则不会抛出异常并且不执行任何操作。 | | void addMouseListener(MouseListener) void removeMouseListener(MouseListener) | 在组件中添加或删除鼠标监听器。当用户使用鼠标与收听的组件进行交互时,将通知鼠标监听器。 | | void addMouseMotionListener(MouseMotionListener) void removeMouseMotionListener(MouseMotionListener) | 在组件中添加或删除鼠标移动监听器。当用户在侦听组件的边界内移动鼠标时,将通知鼠标移动监听器。 | | void addKeyListener(KeyListener) void removeKeyListener(KeyListener) | 在组件中添加或删除键监听器。当用户在键盘上键入并且收听的组件具有键盘焦点时,将通知键监听器。 | | void addComponentListener(ComponentListener) void removeComponentListener(ComponentListener) | 在组件中添加或删除组件监听器。监听组件被隐藏,显示,移动或调整大小时会通知组件监听器。 | | boolean contains(int,int) boolean contains(Point) | 确定指定的点是否在组件内。应根据组件的坐标系指定参数。两个int参数分别指定 xy 坐标。 | | 组件 getComponentAt(int,int) 组件 getComponentAt(Point) | 返回包含指定 x,y 位置的组件。在组件重叠的情况下返回最顶层的子组件。这是通过找到最靠近索引 0 的组件来确定的,该组件声称通过Component.contains()包含给定点。 | | 组件 setComponentZOrder(组件 comp,int 索引) | 将指定的组件移动到容器中指定的 z-order 索引。 | | | 如果组件是某个其他容器的子组件,则在将其添加到此容器之前将其从该容器中删除。此方法与java.awt.Container.add(Component, int)之间的重要区别在于,除非必要且底层本机窗口系统允许,否则此方法在将组件从其先前容器中删除时不会调用组件上的removeNotify。这样,如果组件具有键盘焦点,则在移动到新位置时它会保持焦点。 | | | 注意: z 顺序决定了组件的绘制顺序。具有最高 z 次序涂料的组件首先涂漆,具有最低 z 次序涂料的组件最后涂漆。在组件重叠的情况下,具有较低 z 次序的组件在具有较高 z 次序的组件上绘制。 | | 组件 getComponentZOrder(组件 comp) | 返回容器内组件的 z 顺序索引。组件在 z 顺序层次结构中越高,其索引越低。具有最低 z 顺序索引的组件最后绘制,高于所有其他子组件。 |

Performing Custom Painting for details)

方法 目的
void repaint()

void repaint(int,int,int,int) | 请求重新绘制全部或部分组件。四个int参数指定要绘制的矩形的边界( xy ,宽度,高度)。 | | void repaint(Rectangle) | 请求重新绘制组件中的指定区域。 | | void revalidate() | 请求再次布置组件及其受影响的容器。除非在可见之后显式更改组件的大小/对齐提示,否则通常不需要调用此方法,或者在可见后更改包含层次结构。始终在revalidate后调用repaint。 | | void paintComponent(Graphics) | 绘制组件。重写此方法以实现自定义组件的绘制。 |

Using Top-Level Containers for more information) | 方法 | 目的 | | —- | —- | | 组分添加(组分) 组分添加(组分,整数) 空白添加(组分,对象) | 将指定的组件添加到此容器中。此方法的单参数版本将组件添加到容器的末尾。如果存在,int参数表示新组件在容器中的位置。如果存在,Object参数为当前布局管理器提供布局约束。 | | void remove(int) void 去除(成分) void removeAll() | 从此容器中删除一个或所有组件。如果存在,int参数指示要删除的组件的容器内的位置。 | | JRootPane getRootPane() | 获取包含该组件的根窗格。 | | Container getTopLevelAncestor() | 获取组件的最顶层容器 - WindowApplet,如果组件尚未添加到任何容器,则为 null。 | | Container getParent() | 获取组件的直接容器。 | | int getComponentCount() | 获取此容器中的组件数。 | | 组件 getComponent(int) 组件[] getComponents() | 获取此容器中的一个或所有组件。 int参数指示要获取的组件的位置。 | | 组件 getComponentZOrder(int) 组件[] getComponentZOrder() | 返回容器内组件的 z 顺序索引。组件在 z 顺序层次结构中越高,其索引越低。具有最低 z 顺序索引的组件最后绘制,高于所有其他子组件。 |

Laying Out Components Within a Container for more information)

方法 目的
void setPreferredSize(Dimension)

void setMaximumSize(Dimension) void setMinimumSize(Dimension) | 设置组件的首选,最大或最小尺寸,以像素为单位。首选大小表示组件的最佳尺寸。组件不应大于其最大大小,且不小于其最小大小。请注意,这些仅是提示,某些布局管理器可能会忽略这些提示。 | | Dimension getPreferredSize() Dimension getMaximumSize() Dimension getMinimumSize() | 获取组件的首选大小,最大大小或最小大小(以像素为单位)。许多 JComponent 类都有 setter 和 getter 方法。对于那些没有相应 setter 方法的非JComponent子类,可以通过创建子类并覆盖这些方法来设置组件的首选,最大或最小大小。 | | void setAlignmentX(float) void setAlignmentY(float) | 沿 x-y- 轴设置对齐。这些值表示组件如何相对于其他组件对齐。该值应为 0 到 1 之间的数字,其中 0 表示沿原点对齐,1 表示距离原点最远,0.5 表示居中,依此类推。请注意,这些仅是提示,某些布局管理器可能会忽略这些提示。 | | float getAlignmentX() float getAlignmentY() | 沿着 x-y- 轴获得组件的对齐。对于没有相应 setter 方法的非JComponent子类,可以通过创建子类并覆盖这些方法来设置组件的对齐方式。 | | void setLayout(LayoutManager) LayoutManager getLayout() | 设置或获取组件的布局管理器。布局管理器负责在容器内调整和定位组件。 | | void applyComponentOrientation(ComponentOrientation) void setComponentOrientation(ComponentOrientation) | 设置此容器的ComponentOrientation属性及其中包含的所有组件。有关详细信息,请参阅设置容器的方向。 |

方法 目的
int getWidth()

int getHeight() | 获取以像素为单位测量的组件的当前宽度或高度。 | | 维度 getSize() 维度 getSize(维度) | 以像素为单位获取组件的当前大小。使用此方法的单参数版本时,调用方负责创建返回结果的Dimension实例。 | | int getX() int getY() | 获取组件原点相对于父级左上角的当前 x 或 y 坐标,以像素为单位。 | | Rectangle getBounds() Rectangle getBounds(Rectangle) | 获取以像素为单位测量的组件边界。边界指定组件相对于其父级的宽度,高度和原点。使用此方法的单参数版本时,调用方负责创建返回结果的Rectangle实例。 | | Point getLocation() Point getLocation(Point) | 获取组件相对于父级左上角的当前位置(以像素为单位)。使用getLocation方法的单参数版本时,调用者负责创建返回结果的Point实例。 | | Point getLocationOnScreen() | 返回相对于屏幕左上角的位置。 | | Insets getInsets() | 获取组件边框的大小。 |

Doing Without a Layout Manager (Absolute Positioning) for more information) | 方法 | 目的 | | —- | —- | | void setLocation(int,int) void setLocation(Point) | 设置组件相对于父级左上角的位置(以像素为单位)。两个int参数按顺序指定 xy 。不使用布局管理器时,使用这些方法定位组件。 | | void setSize(int,int) void setSize(Dimension) | 设置以像素为单位测量的组件大小。两个int参数按顺序指定宽度和高度。不使用布局管理器时,使用这些方法调整组件的大小。 | | void setBounds(int,int,int,int) void setBounds(Rectangle) | 设置相对于父组件左上角的大小和位置(以像素为单位)。四个int参数按顺序指定 xy ,宽度和高度。在不使用布局管理器时,使用这些方法来定位和调整组件的大小。 |