Swing - 元件類



介紹

Component是AWT非選單使用者介面控制元件的抽象基類。Component表示具有圖形表示的物件。

類宣告

以下是java.awt.Component類的宣告:

public abstract class Component
   extends Object
      implements ImageObserver, MenuContainer, Serializable

欄位

以下是java.awt.Component類的欄位:

  • static float BOTTOM_ALIGNMENT - 獲取getAlignmentY的易用性常量。

  • static float CENTER_ALIGNMENT - 獲取getAlignmentY和getAlignmentX的易用性常量。

  • static float LEFT_ALIGNMENT - 獲取getAlignmentX的易用性常量。

  • static float RIGHT_ALIGNMENT - 獲取getAlignmentX的易用性常量。

  • static float TOP_ALIGNMENT - 獲取getAlignmentY()的易用性常量。

類建構函式

序號 建構函式和說明
1

protected Component()

這將建立一個新的Component。

類方法

序號 方法和說明
1

boolean action(Event evt, Object what)

已棄用。從JDK 1.1版開始,應將此元件註冊為觸發操作事件的元件上的ActionListener。

2

void add(PopupMenu popup)

將指定的彈出選單新增到元件。

3

void addComponentListener(ComponentListener l)

新增指定的元件監聽器以接收來自此元件的元件事件。

4

void addFocusListener(FocusListener l)

新增指定的焦點監聽器以接收來自此元件的焦點事件,當此元件獲得輸入焦點時。

5

void addHierarchyBoundsListener(HierarchyBoundsListener l)

新增指定的層次邊界監聽器以接收來自此元件的層次邊界事件,當此容器所屬的層次結構發生更改時。

6

void addHierarchyListener(HierarchyListener l)

新增指定的層次監聽器以接收來自此元件的層次結構更改事件,當此容器所屬的層次結構發生更改時。

7

void addInputMethodListener(InputMethodListener l)

新增指定的輸入方法監聽器以接收來自此元件的輸入方法事件。

8

void addKeyListener(KeyListener l)

新增指定的鍵監聽器以接收來自此元件的鍵事件。

9

void addMouseListener(MouseListener l)

新增指定的滑鼠監聽器以接收來自此元件的滑鼠事件。

10

void addMouseMotionListener(MouseMotionListener l)

新增指定的滑鼠移動監聽器以接收來自此元件的滑鼠移動事件。

11

void addMouseWheelListener(MouseWheelListener l)

新增指定的滑鼠滾輪監聽器以接收來自此元件的滑鼠滾輪事件。

12

void addNotify()

透過將其連線到本機螢幕資源來使此元件可顯示。

13

void addPropertyChangeListener(PropertyChangeListener listener)

向監聽器列表新增PropertyChangeListener。

14

void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)

為特定屬性向監聽器列表新增PropertyChangeListener。

15

void applyComponentOrientation(ComponentOrientation orientation)

設定此元件及其包含的所有元件的ComponentOrientation屬性。

16

boolean areFocusTraversalKeysSet(int id)

返回是否為此元件顯式定義了給定焦點遍歷操作的焦點遍歷鍵集。

17

int checkImage(Image image, ImageObserver observer)

返回指定影像的螢幕表示的構造狀態。

18

int checkImage(Image image,int width,int height, ImageObserver observer)

返回指定影像的螢幕表示的構造狀態。

19

boolean contains(int x,int y)

檢查此元件是否“包含”指定的點,其中xy被定義為相對於此元件的座標系。

20

boolean contains(Point p)

檢查此元件是否“包含”指定的點,其中點的xy座標被定義為相對於此元件的座標系。

21

Image createImage(ImageProducer producer)

從指定的影像生產者建立影像。

22

Image createImage(int width,int height)

建立一個離屏可繪製圖像,用於雙緩衝。

23

VolatileImage createVolatileImage(int width,int height)

建立一個易失性離屏可繪製圖像,用於雙緩衝。

24

VolatileImage createVolatileImage(int width,int height, ImageCapabilities caps)

建立一個具有給定功能的易失性離屏可繪製圖像。

25

void deliverEvent(Event e)

已棄用。從JDK 1.1版開始,由dispatchEvent(AWTEvent e)替換。

26

void disable()

已棄用。從JDK 1.1版開始,由setEnabled(boolean)替換。

27

protected void disableEvents(long eventsToDisable)

停用由指定的事件掩碼引數定義的事件傳遞到此元件。

28

void dispatchEvent(AWTEvent e)

將事件分派到此元件或其子元件之一。

29

void doLayout()

提示佈局管理器佈局此元件。

30

void enable()

已棄用。從JDK 1.1版開始,由setEnabled(boolean)替換。

31

void enable(boolean b)

已棄用。從JDK 1.1版開始,由setEnabled(boolean)替換。

32

protected void enableEvents(long eventsToEnable)

啟用由指定的事件掩碼引數定義的事件傳遞到此元件。

33

void enableInputMethods(boolean enable)

啟用或停用此元件的輸入方法支援。

34

protected void firePropertyChange(String propertyName, boolean oldValue, boolean newValue)

支援為布林屬性報告繫結屬性更改。

35

void firePropertyChange(String propertyName, byte oldValue, byte newValue)

報告繫結屬性更改。

36

void firePropertyChange(String propertyName, char oldValue, char newValue)

報告繫結屬性更改。

37

void firePropertyChange(String propertyName, double oldValue, double newValue)

報告繫結屬性更改。

38

void firePropertyChange(String propertyName, float oldValue, float newValue)

報告繫結屬性更改。

39

void firePropertyChange(String propertyName, long oldValue, long newValue)

報告繫結屬性更改。

40

protected void firePropertyChange(String propertyName, Object oldValue, Object newValue)

支援為Object屬性報告繫結屬性更改。

41

void firePropertyChange(String propertyName, short oldValue, short newValue)

報告繫結屬性更改。

42

AccessibleContext getAccessibleContext()

獲取與此Component關聯的AccessibleContext。

43

float getAlignmentX()

返回沿x軸的對齊方式。

44

float getAlignmentY()

返回沿y軸的對齊方式。

45

Color getBackground()

獲取此元件的背景顏色。

46

int getBaseline(int width,int height)

返回基線。

47

Component.BaselineResizeBehavior getBaselineResizeBehavior()

返回一個列舉,指示元件的基線如何隨著大小的變化而變化。

48

Rectangle getBounds()

以Rectangle物件的形式獲取此元件的邊界。

49

Rectangle getBounds(Rectangle rv)

將此元件的邊界儲存到“返回值”rv中並返回rv。

50

ColorModel getColorModel()

獲取用於在輸出裝置上顯示元件的ColorModel例項。

51

Component getComponentAt(int x,int y)

確定此元件或其直接子元件之一是否包含(x, y)位置,如果包含,則返回包含元件。

52

Component getComponentAt(Point p)

返回包含指定點的元件或子元件。

53

ComponentListener[] getComponentListeners()

返回在此元件上註冊的所有元件監聽器的陣列。

54

ComponentOrientation getComponentOrientation()

檢索用於對齊此元件中元素或文字的語言敏感方向。

55

Cursor getCursor()

獲取元件中設定的游標。

56

DropTarget getDropTarget()

獲取與此Component關聯的DropTarget。

57

Container getFocusCycleRootAncestor()

返回此Component的焦點遍歷迴圈的焦點迴圈根容器。

58

FocusListener[] getFocusListeners()

返回在此元件上註冊的所有焦點監聽器的陣列。

59

Set<AWTKeyStroke> getFocusTraversalKeys(int id)

返回此元件給定遍歷操作的焦點遍歷鍵集。

60

boolean getFocusTraversalKeysEnabled()

返回此元件是否啟用了焦點遍歷鍵。

61

Font getFont()

獲取此元件的字型。

62

FontMetrics getFontMetrics(Font font)

獲取指定字型的字型度量。

63

Color getForeground()

獲取此元件的前景色。

64

Graphics getGraphics()

為此元件建立圖形上下文。

65

GraphicsConfiguration getGraphicsConfiguration()

獲取與此Component關聯的GraphicsConfiguration。

66

int getHeight()

返回此元件的當前高度。

67

HierarchyBoundsListener[] getHierarchyBoundsListeners()

返回在此元件上註冊的所有層次邊界監聽器的陣列。

68

HierarchyListener[] getHierarchyListeners()

返回在此元件上註冊的所有層次監聽器的陣列。

69

boolean getIgnoreRepaint()

70

InputContext getInputContext()

獲取此元件用於處理與輸入方法通訊的輸入上下文,當在此元件中輸入文字時。

71

InputMethodListener[] getInputMethodListeners()

返回在此元件上註冊的所有輸入方法監聽器的陣列。

72

InputMethodRequests getInputMethodRequests()

獲取支援此元件輸入方法請求的輸入方法請求處理程式。

73

KeyListener[] getKeyListeners()

返回在此元件上註冊的所有鍵監聽器的陣列。

74

<T extends EventListener> T[] getListeners(Class<T> listenerType)

返回當前在此元件上註冊為FooListeners的所有物件的陣列。

75

Locale getLocale()

獲取此元件的區域設定。

76

Point getLocation()

以指定元件左上角的點的形式獲取此元件的位置。

77

Point getLocation(Point rv)

將此元件的x,y原點儲存到“返回值”rv中並返回rv。

78

Point getLocationOnScreen()

以指定元件在螢幕座標空間中左上角的點的形式獲取此元件的位置。

79

Dimension getMaximumSize()

獲取此元件的最大大小。

80

Dimension getMinimumSize()

獲取此元件的最小大小。

81

MouseListener[] getMouseListeners()

返回在此元件上註冊的所有滑鼠監聽器的陣列。

82

MouseMotionListener[] getMouseMotionListeners()

返回在此元件上註冊的所有滑鼠移動監聽器的陣列。

83

Point getMousePosition()

如果元件直接位於滑鼠指標下方,則返回該元件座標空間中滑鼠指標的位置;否則返回 null。

84

MouseWheelListener[] getMouseWheelListeners()

返回在此元件上註冊的所有滑鼠滾輪監聽器的陣列。

85

String getName()

獲取元件的名稱。

86

Container getParent()

獲取此元件的父容器。

87

java.awt.peer.ComponentPeer getPeer()

已過時。從 JDK 1.1 版開始,程式不應直接操作對等體;已由 boolean isDisplayable() 替代。

88

Dimension getPreferredSize()

獲取此元件的首選大小。

89

PropertyChangeListener[] getPropertyChangeListeners()

返回在此元件上註冊的所有屬性更改監聽器的陣列。

90

PropertyChangeListener[] getPropertyChangeListeners(String propertyName)

返回已與命名屬性關聯的所有監聽器的陣列。

91

Dimension getSize()

以 Dimension 物件的形式返回此元件的大小。

92

Dimension getSize(Dimension rv)

將此元件的寬度/高度儲存到“返回值:rv”中並返回 rv。

93

Toolkit getToolkit()

獲取此元件的工具包。

94

Object getTreeLock()

獲取此元件的鎖定物件(擁有 AWT 元件樹和佈局操作的執行緒同步監視器的物件)。

95

int getWidth()

返回此元件的當前寬度。

96

int getX()

返回元件原點的當前 x 座標。

97

int getY()

返回元件原點的當前 y 座標。

98

boolean gotFocus(Event evt, Object what)

已過時。從 JDK 1.1 版開始,已由 processFocusEvent(FocusEvent) 替代。

99

boolean handleEvent(Event evt)

已過時。從 JDK 1.1 版開始,已由 processEvent(AWTEvent) 替代。

100

boolean hasFocus()

如果此元件是焦點所有者,則返回 true。

101

void hide()

已過時。從 JDK 1.1 版開始,已由 setVisible(boolean) 替代。

102

boolean imageUpdate(Image img,int infoflags,int x,int y,int w,int h)

影像更改時重新繪製元件。

103

boolean inside(int x,int y)

已過時。從 JDK 1.1 版開始,已由 contains(int, int) 替代。

104

void invalidate()

使此元件無效。

105

boolean isBackgroundSet()

返回是否為此元件顯式設定了背景顏色。

106

boolean isCursorSet()

返回是否為此元件顯式設定了游標。

107

boolean isDisplayable()

確定此元件是否可顯示。

108

boolean isDoubleBuffered()

如果此元件繪製到稍後複製到螢幕的離屏影像(緩衝區),則返回 true。

109

boolean isEnabled()

確定此元件是否已啟用。

110

boolean isFocusable()

返回此元件是否可獲得焦點。

111

boolean isFocusCycleRoot(Container container)

返回指定的容器是否為此元件的焦點遍歷迴圈的焦點迴圈根。

112

boolean isFocusOwner()

如果此元件是焦點所有者,則返回 true。

113

boolean isFocusTraversable()

已過時。從 1.4 版開始,已由 isFocusable() 替代。

114

boolean isFontSet()

返回是否為此元件顯式設定了字型。

115

boolean isForegroundSet()

返回是否為此元件顯式設定了前景色。

116

boolean isLightweight()

輕量級元件沒有本機工具包對等體。

117

boolean isMaximumSizeSet()

如果最大大小已設定為非 null 值,則返回 true;否則返回 false。

118

boolean isMinimumSizeSet()

返回是否已使用非 null 值呼叫 setMinimumSize。

119

boolean isOpaque()

如果此元件完全不透明,則返回 true;預設情況下返回 false。

120

boolean isPreferredSizeSet()

如果首選大小已設定為非 null 值,則返回 true;否則返回 false。

121

boolean isShowing()

確定此元件是否正在螢幕上顯示。

122

boolean isValid()

確定此元件是否有效。

123

boolean isVisible()

確定此元件的父元件可見時,此元件是否應可見。

124

boolean keyDown(Event evt,int key)

已過時。從 JDK 1.1 版開始,已由 processKeyEvent(KeyEvent) 替代。

125

boolean keyUp(Event evt,int key)

已過時。從 JDK 1.1 版開始,已由 processKeyEvent(KeyEvent) 替代。

126

void layout()

已過時。從 JDK 1.1 版開始,已由 doLayout() 替代。

127

void list()

將此元件的列表列印到標準系統輸出流 System.out。

128

void list(PrintStream out)

將此元件的列表列印到指定的輸出流。

129

void list(PrintStream out,int indent)

從指定的縮排開始,將列表列印到指定的列印流。

130

void list(PrintWriter out)

將列表列印到指定的列印編寫器。

131

void list(PrintWriter out,int indent)

從指定的縮排開始,將列表列印到指定的列印編寫器。

132

Component locate(int x,int y)

已過時。從 JDK 1.1 版開始,已由 getComponentAt(int, int) 替代。

133

Point location()

已過時。從 JDK 1.1 版開始,已由 getLocation() 替代。

134

boolean lostFocus(Event evt, Object what)

已過時。從 JDK 1.1 版開始,已由 processFocusEvent(FocusEvent) 替代。

135

boolean mouseDown(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseEvent(MouseEvent) 替代。

136

boolean mouseDrag(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseMotionEvent(MouseEvent) 替代。

137

boolean mouseEnter(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseEvent(MouseEvent) 替代。

138

boolean mouseExit(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseEvent(MouseEvent) 替代。

139

boolean mouseMove(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseMotionEvent(MouseEvent) 替代。

140

boolean mouseUp(Event evt,int x,int y)

已過時。從 JDK 1.1 版開始,已由 processMouseEvent(MouseEvent) 替代。

141

void move(int x,int y)

已過時。從 JDK 1.1 版開始,已由 setLocation(int, int) 替代。

142

void nextFocus()

已過時。從 JDK 1.1 版開始,已由 transferFocus() 替代。

143

void paint(Graphics g)

繪製此元件。

144

void paintAll(Graphics g)

繪製此元件及其所有子元件。

145

boolean postEvent(Event e)

已過時。從 JDK 1.1 版開始,已由 dispatchEvent(AWTEvent) 替代。

146

boolean prepareImage(Image image,int width,int height, ImageObserver observer)

準備在此元件上以指定的寬度和高度渲染影像。

147

void print(Graphics g)

列印此元件。

148

void printAll(Graphics g)

列印此元件及其所有子元件。

149

protected void processComponentEvent(ComponentEvent e)

透過將元件事件分派給任何已註冊的 ComponentListener 物件來處理在此元件上發生的元件事件。

150

protected void processEvent(AWTEvent e)

處理在此元件上發生的事件。

151

protected void processFocusEvent(FocusEvent e)

透過將焦點事件分派給任何已註冊的 FocusListener 物件來處理在此元件上發生的焦點事件。

152

protected void processHierarchyBoundsEvent(HierarchyEvent e)

透過將層次結構邊界事件分派給任何已註冊的 HierarchyBoundsListener 物件來處理在此元件上發生的層次結構邊界事件。

153

protected void processHierarchyEvent(HierarchyEvent e)

透過將層次結構事件分派給任何已註冊的 HierarchyListener 物件來處理在此元件上發生的層次結構事件。

154

protected void processInputMethodEvent(InputMethodEvent e)

透過將輸入方法事件分派給任何已註冊的 InputMethodListener 物件來處理在此元件上發生的輸入方法事件。

155

protected void processKeyEvent(KeyEvent e)

透過將鍵盤事件分派給任何已註冊的 KeyListener 物件來處理在此元件上發生的鍵盤事件。

156

protected void processMouseEvent(MouseEvent e)

透過將滑鼠事件分派給任何已註冊的 MouseListener 物件來處理在此元件上發生的滑鼠事件。

157

protected void processMouseMotionEvent(MouseEvent e)

透過將滑鼠移動事件分派給任何已註冊的 MouseMotionListener 物件來處理在此元件上發生的滑鼠移動事件。

158

protected void processMouseWheelEvent(MouseWheelEvent e)

透過將滑鼠滾輪事件分派給任何已註冊的 MouseWheelListener 物件來處理在此元件上發生的滑鼠滾輪事件。

159

void remove(MenuComponent popup)

從此元件中刪除指定的彈出選單。

160

void removeComponentListener(ComponentListener l)

移除指定的元件監聽器,以便它不再接收來自此元件的元件事件。

161

void removeFocusListener(FocusListener l)

移除指定的焦點監聽器,以便它不再接收來自此元件的焦點事件。

162

void removeHierarchyBoundsListener(HierarchyBoundsListener l)

移除指定的層次結構邊界監聽器,以便它不再接收來自此元件的層次結構邊界事件。

163

void removeHierarchyListener(HierarchyListener l)

移除指定的層次結構監聽器,以便它不再接收來自此元件的層次結構更改事件。

164

void removeInputMethodListener(InputMethodListener l)

移除指定的輸入方法監聽器,以便它不再接收來自此元件的輸入方法事件。

165

void removeKeyListener(KeyListener l)

移除指定的鍵盤監聽器,以便它不再接收來自此元件的鍵盤事件。

166

void removeMouseListener(MouseListener l)

移除指定的滑鼠監聽器,以便它不再接收來自此元件的滑鼠事件。

167

void removeMouseMotionListener(MouseMotionListener l)

移除指定的滑鼠移動監聽器,以便它不再接收來自此元件的滑鼠移動事件。

168

void removeMouseWheelListener(MouseWheelListener l)

移除指定的滑鼠滾輪監聽器,以便它不再接收來自此元件的滑鼠滾輪事件。

169

void removeNotify()

透過銷燬其本機螢幕資源來使此元件不可顯示。

170

void removePropertyChangeListener(PropertyChangeListener listener)

從此監聽器列表中移除 PropertyChangeListener。

171

void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)

從特定屬性的監聽器列表中移除 PropertyChangeListener。

172

void repaint()

重新繪製此元件。

173

void repaint(int x,int y,int width,int height)

重新繪製此元件的指定矩形。

174

void repaint(long tm)

重新繪製元件。

175

void repaint(long tm,int x,int y,int width,int height)

tm 毫秒內重新繪製此元件的指定矩形。

176

void requestFocus()

請求此元件獲取輸入焦點,並使此元件的頂級祖先成為聚焦視窗。

177

protected boolean requestFocus(boolean temporary)

請求此元件獲取輸入焦點,並使此元件的頂級祖先成為聚焦視窗。

178

boolean requestFocusInWindow()

如果此元件的頂級祖先已經是聚焦視窗,則請求此元件獲取輸入焦點。

179

protected boolean requestFocusInWindow(boolean temporary)

如果此元件的頂級祖先已經是聚焦視窗,則請求此元件獲取輸入焦點。

180

void reshape(int x,int y,int width,int height)

已過時。從 JDK 1.1 版開始,已由 setBounds(int, int, int, int) 替代。

181

void resize(Dimension d)

已過時。從 JDK 1.1 版開始,已由 setSize(Dimension) 替代。

182

void resize(int width,int height)

已過時。從 JDK 1.1 版開始,已由 setSize(int, int) 替代。

183

void setBackground(Color c)

設定此元件的背景顏色。

184

void setBounds(int x,int y,int width,int height)

移動並調整此元件的大小。

185

void setBounds(Rectangle r)

移動並調整此元件的大小,使其符合新的邊界矩形r

186

void setComponentOrientation(ComponentOrientation o)

設定用於排序此元件內元素或文字的語言敏感方向。

187

void setCursor(Cursor cursor)

將游標影像設定為指定的游標。

188

void setDropTarget(DropTarget dt)

將DropTarget與該元件關聯。

189

void setEnabled(boolean b)

根據引數b的值啟用或停用此元件。

190

void setFocusable(boolean focusable)

將此元件的可聚焦狀態設定為指定的值。

191

void setFocusTraversalKeys(int id, Set<? extends AWTKeyStroke> keystrokes)

為該元件的給定遍歷操作設定焦點遍歷鍵。

192

void setFocusTraversalKeysEnabled(boolean focusTraversalKeysEnabled)

設定是否為此元件啟用焦點遍歷鍵。

193

void setFont(Font f)

設定此元件的字型。

194

void setForeground(Color c)

設定此元件的前景色。

195

void setIgnoreRepaint(boolean ignoreRepaint)

設定是否應忽略從作業系統接收到的繪製訊息。

196

void setLocale(Locale l)

設定此元件的區域設定。

197

void setLocation(int x,int y)

將此元件移動到新位置。

198

void setLocation(Point p)

將此元件移動到新位置。

199

void setMaximumSize(Dimension maximumSize)

將此元件的最大大小設定為一個常數值。

200

void setMinimumSize(Dimension minimumSize)

將此元件的最小大小設定為一個常數值。

201

void setName(String name)

將元件的名稱設定為指定的字串。

202

void setPreferredSize(Dimension preferredSize)

將此元件的首選大小設定為一個常數值。

203

void setSize(Dimension d)

調整此元件的大小,使其寬度為d.width,高度為d.height

204

void setSize(int width,int height)

調整此元件的大小,使其寬度為width,高度為height

205

void setVisible(boolean b)

根據引數b的值顯示或隱藏此元件。

206

void show()

已過時。從 JDK 1.1 版開始,已由 setVisible(boolean) 替代。

207

void show(boolean b)

已過時。從 JDK 1.1 版開始,已由 setVisible(boolean) 替代。

208

Dimension size()

已過時。從JDK版本1.1開始,已由getSize()替換。

209

String toString()

返回此元件及其值的字串表示形式。

210

void transferFocus()

將焦點轉移到下一個元件,就好像此元件是焦點所有者一樣。

211

void transferFocusBackward()

將焦點轉移到上一個元件,就好像此元件是焦點所有者一樣。

212

void transferFocusUpCycle()

將焦點向上轉移一個焦點遍歷週期。

213

void update(Graphics g)

更新此元件。

214

void validate()

確保此元件具有有效的佈局。

215

Rectangle bounds()

已過時。從JDK版本1.1開始,已由getBounds()替換。

216

protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent)

可能將要釋出的事件與現有事件合併。

217

protected String paramString()

返回表示此元件狀態的字串。

218

protected void firePropertyChange(String propertyName,int oldValue,int newValue)

支援為整型屬性報告繫結屬性更改。

219

Dimension preferredSize()

已過時。從JDK版本1.1開始,已由getPreferredSize()替換。

220

boolean prepareImage(Image image, ImageObserver observer)

準備在此元件上呈現影像。

221

Dimension minimumSize()

已過時。從JDK版本1.1開始,已由getMinimumSize()替換。

繼承的方法

此類繼承自以下類:

  • java.lang.Object
swing_controls.htm
廣告