Java Swing教程 - Java Swing組件
滾動條值更改...
Java Swing組件繼承自javax.swing.JComponent類和它充當(dāng)所有Swing組件的基類。JComponent是一個抽象類。
JComponent
Java Swing組件繼承自javax.swing.JComponent類和它充當(dāng)所有Swing組件的基類。JComponent是一個抽象類。...
JComponent類作為所有Swing組件的基類,為所有Swing組件提供以下基本功能。
- tool tips
- pluggable look and feel
- border
- accessibility
- double buffering
- key binding
- laying out the component
JComponent方法
下表列出了JComponent的一些常用方法類可用于所有Swing組件。
-
Border getBorder()
Returns the border of the component or null if the component has no border.
-
void setBorder(Border border)
Sets the border for the component.
-
Object getClientProperty(Object key)
Returns the value associated with the specified key. The value must have been set using the putClientProperty (Object key, Object value) method.
-
void putClientProperty(Object key, Object value)
Adds an arbitrary key-value pair to the component.
-
Graphics getGraphics()
Returns the graphics context object for the component, which can be used to draw on the component.
- Dimension getMaximumSize()
Dimension getMinimumSize()
Dimension getPreferredSize()
Dimension getSize(Dimension d)
void setMaximumSize(Dimension d)
void setMinimumSize(Dimension d)
void setPreferredSize(Dimension d)
void setSize(Dimension d)
void setSize(int width, int height)
Gets/sets the maximum, minimum, preferred, and actual size of the component.
-
String getToolTipText()
Returns the tool tip text for this component.
-
void setToolTipText(String text)
Sets the tool tip text.
-
boolean isDoubleBuffered()
Returns true if the component uses double buffering. Otherwise, it returns false.
-
void setDoubleBuffered(boolean db)
Sets if the component should use double buffering to paint or not.
-
boolean isFocusable()
Returns true if the component can gain focus. Otherwise, it returns false.
-
void setFocusable(boolean focusable)
Sets if the component can gain focus or not.
-
boolean isVisible()
Returns true if the component is visible. Otherwise, it returns false.
-
void setVisible(boolean v)
Sets the component visible or invisible.
-
boolean isEnabled()
Returns true if the component is enabled. Otherwise, it returns false.
-
void setEnabled(boolean e)
Enables or disables the component.
- boolean requestFocus(boolean temporary)
boolean requestFocusInWindow()
boolean requestFocusInWindow(boolean temporary)
Both requestFocus() and requestFocusInWindow() methods request that the component should get the input focus.
We should use the requestFocusInWindow() method instead of the requestFocus() method because its behavior is consistent across all platforms.
-
boolean isOpaque()
Returns true if the JComponent is opaque. Otherwise, it returns
false.
-
void setOpaque(boolean opaque)
Sets the opacity of the JComponent.
JComponent事件
下表列出了適用于所有Swing組件的一些常用事件。
- ComponentEvent/ComponentListener
The event occurs when a component"s visibility, size, or location is changed.
更多建議: