Swing
Swing教學首頁
Swing介紹
Swing開發環境安裝
Swing控件
Swing Component類
Swing Container類
Swing JComponent類
Swing JLabel
Swing JButton
Swing JColorChooser
Swing JCheckBox
Swing JRadioButton
Swing JList
Swing JComboBox
Swing JTextField
Swing JTextArea
Swing ImageIcon
Swing JScrollBar
Swing JOptionPane
Swing JFileChooser
Swing JProgressBar
Swing JSlider
Swing JSpinner
Swing事件處理
SWING Event事件類
SWING AWTEvent事件類
SWING ActionEvent事件類
SWING InputEvent事件類
SWING KeyEvent事件類
SWING MouseEvent事件類
SWING WindowEvent事件類
SWING AdjustmentEvent事件處理
SWING ComponentEvent處理類
SWING ContainerEvent事件處理類
SWING MouseMotionEvent事件處理
SWING PaintEvent事件類
Swing事件監聽器
SWING ActionListener接口
SWING ComponentListener接口
Swing ItemListener接口
Swing KeyListener接口
Swing MouseListener Interface
Swing WindowListener接口
Swing AdjustmentListener接口
Swing ContainerListener接口
Swing MouseMotionListener接口
Swing FocusListener接口
Swing事件適配器
Swing FocusAdapter類
Swing KeyAdapter類
Swing MouseAdapter類
Swing WindowAdapter類
Swing MouseMotionAdapter類
Swing Layout佈局
Swing LayoutManager接口
Swing LayoutManager2接口
Swing BorderLayout佈局
Swing CardLayout佈局
Swing FlowLayout佈局類
Swing GridLayout佈局類
Swing GridBagLayout佈局類
Swing GroupLayout佈局類
Swing SpringLayout佈局類
Swing Menu菜單類
Swing JMenuBar類
Swing JMenuItem類
Swing JMenu類
Swing JCheckboxMenuItem類及例子
Swing JRadioButtonMenuItem類及例子
Swing JPopupMenu類及實例
Swing容器
Swing JPanel類及實例
Swing JFrame類和實例
Swing JWindow類及實例

Swing JComponent類

JComponent類是除頂層容器外所有Swing組件的基類。要使用繼承自JComponent 一個組成部分,必須把的容納層次結構,其根是一個頂層 Swing 容器組件。

類聲明

以下是聲明的 javax.swing.JComponent類:

public abstract class JComponent extends Container implements Serializable

字段域

以下是java.awt.Component 類的字段:

  • protected AccessibleContext accessibleContext -- AccessibleContext 可以帶此JComponent。

  • protected EventListenerList listenerList -- 此組件的事件偵聽器的列表。

  • static String TOOL_TIP_TEXT_KEY -- 註釋顯示當光標在組件上,也被稱爲 "value tip", "flyover help", 或 "flyover label".

  • protected ComponentUI ui -- 此組件代表的外觀和風格。

  • static int UNDEFINED_CONDITION -- Constant used by some of the APIs to mean that no condition is defined.

  • static int WHEN_ANCESTOR_OF_FOCUSED_COMPONENT -- 常數用於registerKeyboardAction,意味着當接收組件是一個聚焦組件的基類,或者本身就是焦點的組件,該命令要調用。

  • static int WHEN_FOCUSED -- 常數用於registerKeyboardAction,意味着命令組件具有焦點時要調用。

  • static int WHEN_IN_FOCUSED_WINDOW -- 常數用於registerKeyboardAction,意味着當接收組件具有焦點的窗口,或者是本身的重點組成部分,該命令調用。

類構造函數

S.N.

構造函數 & 描述

1

JComponent()
Default JComponent constructor.

類方法

S.N.

方法 & 描述

1

void addAncestorListener(AncestorListener listener)
Registers listener so that it will receive AncestorEvents when it or any of its ancestors move or are made visible or invisible.

2

void addNotify()
Notifies this component that it now has a parent component.

3

void addVetoableChangeListener(VetoableChangeListener listener)
Adds a VetoableChangeListener to the listener list.

4

void computeVisibleRect(Rectangle visibleRect)
Returns the Component's "visible rect rectangle" - the intersection of the visible rectangles for this component and all of its ancestors.

5

boolean contains(int x, int y)
Gives the UI delegate an opportunity to define the precise shape of this component for the sake of mouse processing.

6

JToolTip createToolTip()
Returns the instance of JToolTip that should be used to display the tooltip.

7

void disable()
Deprecated.As of JDK version 1.1, replaced by java.awt.Component.setEnabled(boolean).

8

void enable()
Deprecated. As of JDK version 1.1, replaced by java.awt.Component.setEnabled(boolean).

9

void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)
Support for reporting bound property changes for boolean properties.

10

void firePropertyChange(String propertyName, char oldValue, char newValue)
Reports a bound property change.

11

void firePropertyChange(String propertyName, int oldValue, int newValue)
Support for reporting bound property changes for integer properties.

12

protected void fireVetoableChange(String propertyName, Object oldValue, Object newValue)
Supports reporting constrained property changes.

13

AccessibleContext getAccessibleContext()
Returns the AccessibleContext associated with this JComponent.

14

ActionListener getActionForKeyStroke(KeyStroke aKeyStroke)
Returns the object that will perform the action registered for a given keystroke.

15

ActionMap getActionMap()
Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.

16

float getAlignmentX()
Overrides Container.getAlignmentX to return the vertical alignment.

17

float getAlignmentY()
Overrides Container.getAlignmentY to return the horizontal alignment.

18

AncestorListener[] getAncestorListeners()
Returns an array of all the ancestor listeners registered on this component.

19

boolean getAutoscrolls()
Gets the autoscrolls property.

20

int getBaseline(int width, int height)
Returns the baseline.

21

Component.BaselineResizeBehavior getBaselineResizeBehavior()
Returns an enum indicating how the baseline of the component changes as the size changes.

22

Border getBorder()
Returns the border of this component or null if no border is currently set.

23

Rectangle getBounds(Rectangle rv)
Stores the bounds of this component into "return value" rv and returns rv.

24

Object getClientProperty(Object key)
Returns the value of the property with the specified key.

25

protected Graphics getComponentGraphics(Graphics g)
Returns the graphics object used to paint this component.

26

JPopupMenu getComponentPopupMenu()
Returns JPopupMenu that assigned for this component.

27

int getConditionForKeyStroke(KeyStroke aKeyStroke)
Returns the condition that determines whether a registered action occurs in response to the specified keystroke.

28

int getDebugGraphicsOptions()
Returns the state of graphics debugging.

29

static Locale getDefaultLocale()
Returns the default locale used to initialize each JComponent's locale property upon creation.

30

FontMetrics getFontMetrics(Font font)
Gets the FontMetrics for the specified Font.

31

Graphics getGraphics()
Returns this component's graphics context, which lets you draw on a component.

32

int getHeight()
Returns the current height of this component.

33

boolean getInheritsPopupMenu()
Returns true if the JPopupMenu should be inherited from the parent.

34

InputMap getInputMap()
Returns the InputMap that is used when the component has focus.

35

InputMap getInputMap(int condition)
Returns the InputMap that is used during condition.

36

InputVerifier getInputVerifier()
Returns the input verifier for this component.

37

Insets getInsets()
If a border has been set on this component, returns the border's insets; otherwise calls super.getInsets.

38

Insets getInsets(Insets insets)
Returns an Insets object containing this component's inset values.

39

T[] getListeners(Class listenerType)
Returns an array of all the objects currently registered as FooListeners upon this JComponent.

40

Point getLocation(Point rv)
Stores the x,y origin of this component into "return value" rv and returns rv.

41

Dimension getMaximumSize()
If the maximum size has been set to a non-null value just returns it.

42

Dimension getMinimumSize()
If the minimum size has been set to a non-null value just returns it.

43

Component getNextFocusableComponent()
Deprecated. As of 1.4, replaced by FocusTraversalPolicy.

44

Point getPopupLocation(MouseEvent event)
Returns the preferred location to display the popup menu in this component's coordinate system.

45

Dimension getPreferredSize()
If the preferredSize has been set to a non-null value just returns it.

46

KeyStroke[] getRegisteredKeyStrokes()
Returns the KeyStrokes that will initiate registered actions.

47

JRootPane getRootPane()
Returns the JRootPane ancestor for this component.

48

Dimension getSize(Dimension rv)
Stores the width/height of this component into "return value" rv and returns rv.

49

Point getToolTipLocation(MouseEvent event)
Returns the tooltip location in this component's coordinate system.

50

String getToolTipText()
Returns the tooltip string that has been set with setToolTipText.

51

String getToolTipText(MouseEvent event)
Returns the string to be used as the tooltip for event.

52

Container getTopLevelAncestor()
Returns the top-level ancestor of this component (either the containing Window or Applet), or null if this component has not been added to any container.

53

TransferHandler getTransferHandler()
Gets the transferHandler property.

54

String getUIClassID()
Returns the UIDefaults key used to look up the name of the swing.plaf.ComponentUI class that defines the look and feel for this component.

55

boolean getVerifyInputWhenFocusTarget()
Returns the value that indicates whether the input verifier for the current focus owner will be called before this component requests focus.

56

VetoableChangeListener[] getVetoableChangeListeners()
Returns an array of all the vetoable change listeners registered on this component.

57

Rectangle getVisibleRect()
Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()) , and all of its ancestors' visible rectangles.

58

int getWidth()
Returns the current width of this component.

59

int getX()
Returns the current x coordinate of the component's origin.

60

int getY()
Returns the current y coordinate of the component's origin.

61

void grabFocus()
Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window.

62

boolean isDoubleBuffered()
Returns whether this component should use a buffer to paint.

63

static boolean isLightweightComponent(Component c)
Returns true if this component is lightweight, that is, if it doesn't have a native window system peer.

64

boolean isManagingFocus()
Deprecated.As of 1.4, replaced by Component.setFocusTraversalKeys(int, Set) and Container.setFocusCycleRoot(boolean).

65

boolean isOpaque()
Returns true if this component is completely opaque.

66

boolean isOptimizedDrawingEnabled()
Returns true if this component tiles its children -- that is, if it can guarantee that the children will not overlap.

67

boolean isPaintingForPrint()
Returns true if the current painting operation on this component is part of a print operation.

68

boolean isPaintingTile()
Returns true if the component is currently painting a tile.

69

boolean isRequestFocusEnabled()
Returns true if this JComponent should get focus; otherwise returns false.

70

boolean isValidateRoot()
If this method returns true, revalidate calls by descendants of this component will cause the entire tree beginning with this root to be validated.

71

void paint(Graphics g)
Invoked by Swing to draw components.

72

protected void paintBorder(Graphics g)
Paints the component's border.

73

protected void paintChildren(Graphics g)
Paints this component's children.

74

protected void paintComponent(Graphics g)
Calls the UI delegate's paint method, if the UI delegate is non-null.

75

void paintImmediately(int x, int y, int w, int h)
Paints the specified region in this component and all of its descendants that overlap the region, immediately.

76

void paintImmediately(Rectangle r)
Paints the specified region now.

77

protected String paramString()
Returns a string representation of this JComponent.

78

void print(Graphics g)
Invoke this method to print the component to the specified Graphics.

79

void printAll(Graphics g)
Invoke this method to print the component.

80

protected void printBorder(Graphics g)
Prints the component's border.

81

protected void printChildren(Graphics g)
Prints this component's children.

82

protected void printComponent(Graphics g)
This is invoked during a printing operation.

82

protected void processComponentKeyEvent(KeyEvent e)
Processes any key events that the component itself recognizes.

84

protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed)
Invoked to process the key bindings for ks as the result of the KeyEvent e.

85

protected void processKeyEvent(KeyEvent e)
Overrides processKeyEvent to process events.

86

protected void processMouseEvent(MouseEvent e)
Processes mouse events occurring on this component by dispatching them to any registered MouseListener objects, refer to Component.processMouseEvent(MouseEvent) for a complete description of this method.

87

protected void processMouseMotionEvent(MouseEvent e)
Processes mouse motion events, such as MouseEvent.MOUSE_DRAGGED.

88

void putClientProperty(Object key, Object value)
Adds an arbitrary key/value "client property" to this component.

89

void registerKeyboardAction(ActionListener anAction, KeyStroke aKeyStroke, int aCondition)
This method is now obsolete, please use a combination of getActionMap() 
and getInputMap() 
for similiar behavior.

90

void registerKeyboardAction(ActionListener anAction, String aCommand, KeyStroke aKeyStroke, int aCondition)
This method is now obsolete, please use a combination of getActionMap() 
and getInputMap() 
for similiar behavior.

91

void removeAncestorListener(AncestorListener listener)
Unregisters listener so that it will no longer receive AncestorEvents.

92

void removeNotify()
Notifies this component that it no longer has a parent component.

93

void removeVetoableChangeListener(VetoableChangeListener listener)
Removes a VetoableChangeListener from the listener list.

94

void repaint(long tm, int x, int y, int width, int height)
Adds the specified region to the dirty region list if the component is showing.

95

void repaint(Rectangle r)
Adds the specified region to the dirty region list if the component is showing.

96

boolean requestDefaultFocus()
Deprecated.As of 1.4, replaced by FocusTraversalPolicy.getDefaultComponent(Container).requestFocus()

97

void requestFocus()
Requests that this Component gets the input focus.

98

boolean requestFocus(boolean temporary)
Requests that this Component gets the input focus.

99

boolean requestFocusInWindow()
Requests that this Component gets the input focus.

100

protected boolean requestFocusInWindow(boolean temporary)
Requests that this Component gets the input focus.

101

void resetKeyboardActions()
Unregisters all the bindings in the first tier InputMaps and ActionMap.

102

void reshape(int x, int y, int w, int h)
Deprecated.As of JDK 5, replaced by Component.setBounds(int, int, int, int).Moves and resizes this component.

103

void revalidate()
Supports deferred automatic layout.

104

void scrollRectToVisible(Rectangle aRect)
Forwards the scrollRectToVisible() 
message to the JComponent's parent.

105

void setActionMap(ActionMap am)
Sets the ActionMap to am.

106

void setAlignmentX(float alignmentX)
Sets the the vertical alignment.

107

void setAlignmentY(float alignmentY)
Sets the the horizontal alignment.

108

void setAutoscrolls(boolean autoscrolls)
Sets the autoscrolls property.

109

void setBackground(Color bg)
Sets the background color of this component.

110

void setBorder(Border border)
Sets the border of this component.

111

void setComponentPopupMenu(JPopupMenu popup)
Sets the JPopupMenu for this JComponent.

112

void setDebugGraphicsOptions(int debugOptions)
Enables or disables diagnostic information about every graphics operation performed within the component or one of its children.

113

static void setDefaultLocale(Locale l)
Sets the default locale used to initialize each JComponent's locale property upon creation.

114

void setDoubleBuffered(boolean aFlag)
Sets whether this component should use a buffer to paint.

115

void setEnabled(boolean enabled)
Sets whether or not this component is enabled.

116

void setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes)
Sets the focus traversal keys for a given traversal operation for this Component.

117

void setFont(Font font)
Sets the font for this component.

118

void setForeground(Color fg)
Sets the foreground color of this component.

119

void setInheritsPopupMenu(boolean value)
Sets whether or not getComponentPopupMenu should delegate to the parent if this component does not have a JPopupMenu assigned to it.

120

void setInputMap(int condition, InputMap map)
Sets the InputMap to use under the condition condition to map.

121

void setInputVerifier(InputVerifier inputVerifier)
Sets the input verifier for this component.

122

void setMaximumSize(Dimension maximumSize)
Sets the maximum size of this component to a constant value.

123

void setMinimumSize(Dimension minimumSize)
Sets the minimum size of this component to a constant value.

124

void setNextFocusableComponent(Component aComponent)
Deprecated. As of 1.4, replaced by FocusTraversalPolicy

125

void setOpaque(boolean isOpaque)
If true the component paints every pixel within its bounds.

126

void setPreferredSize(Dimension preferredSize)
Sets the preferred size of this component.

127

void setRequestFocusEnabled(boolean requestFocusEnabled)
Provides a hint as to whether or not this JComponent should get focus.

128

void setToolTipText(String text)
Registers the text to display in a tool tip.

129

void setTransferHandler(TransferHandler newHandler)
Sets the transferHandler property, which is null if the component does not support data transfer operations.

130

protected void setUI(ComponentUI newUI)
Sets the look and feel delegate for this component.

131

void setVerifyInputWhenFocusTarget(boolean verifyInputWhenFocusTarget)
Sets the value to indicate whether input verifier for the current focus owner will be called before this component requests focus.

132

void setVisible(boolean aFlag)
Makes the component visible or invisible.

133

void unregisterKeyboardAction(KeyStroke aKeyStroke)
This method is now obsolete.

134

void update(Graphics g)
Calls paint.

135

void updateUI()
Resets the UI property to a value from the current look and feel.

方法繼承

這個類從以下類繼承的方法:

  • java.awt.Container

  • java.awt.Component

  • java.lang.Object