📄 component.java
字号:
/** * The serialization version for this class. Currently at version 4. * * XXX How do we handle prior versions? * * @serial the serialization version */ int componentSerializedDataVersion = 4; /** * The accessible context associated with this component. This is only set * by subclasses. * * @see #getAccessibleContext() * @serial the accessibility context * @since 1.2 */ AccessibleContext accessibleContext; // Guess what - listeners are special cased in serialization. See // readObject and writeObject. /** Component listener chain. */ transient ComponentListener componentListener; /** Focus listener chain. */ transient FocusListener focusListener; /** Key listener chain. */ transient KeyListener keyListener; /** Mouse listener chain. */ transient MouseListener mouseListener; /** Mouse motion listener chain. */ transient MouseMotionListener mouseMotionListener; /** * Mouse wheel listener chain. * * @since 1.4 */ transient MouseWheelListener mouseWheelListener; /** * Input method listener chain. * * @since 1.2 */ transient InputMethodListener inputMethodListener; /** * Hierarcy listener chain. * * @since 1.3 */ transient HierarchyListener hierarchyListener; /** * Hierarcy bounds listener chain. * * @since 1.3 */ transient HierarchyBoundsListener hierarchyBoundsListener; // Anything else is non-serializable, and should be declared "transient". /** The parent. */ transient Container parent; /** The associated native peer. */ transient ComponentPeer peer; /** The preferred component orientation. */ transient ComponentOrientation orientation = ComponentOrientation.UNKNOWN; /** * The associated graphics configuration. * * @since 1.4 */ transient GraphicsConfiguration graphicsConfig; /** * The buffer strategy for repainting. * * @since 1.4 */ transient BufferStrategy bufferStrategy; /** * true if requestFocus was called on this component when its * top-level ancestor was not focusable. */ private transient FocusEvent pendingFocusRequest = null; /** * The system properties that affect image updating. */ private static transient boolean incrementalDraw; private static transient Long redrawRate; static { incrementalDraw = Boolean.getBoolean ("awt.image.incrementalDraw"); redrawRate = Long.getLong ("awt.image.redrawrate"); } // Public and protected API. /** * Default constructor for subclasses. When Component is extended directly, * it forms a lightweight component that must be hosted in an opaque native * container higher in the tree. */ protected Component() { // Nothing to do here. } /** * Returns the name of this component. * * @return the name of this component * @see #setName(String) * @since 1.1 */ public String getName() { if (name == null && ! nameExplicitlySet) name = generateName(); return name; } /** * Sets the name of this component to the specified name. * * @param name the new name of this component * @see #getName() * @since 1.1 */ public void setName(String name) { nameExplicitlySet = true; this.name = name; } /** * Returns the parent of this component. * * @return the parent of this component */ public Container getParent() { return parent; } /** * Returns the native windowing system peer for this component. Only the * platform specific implementation code should call this method. * * @return the peer for this component * @deprecated user programs should not directly manipulate peers; use * {@link #isDisplayable()} instead */ // Classpath's Gtk peers rely on this. public ComponentPeer getPeer() { return peer; } /** * Set the associated drag-and-drop target, which receives events when this * is enabled. * * @param dt the new drop target * @see #isEnabled() */ public void setDropTarget(DropTarget dt) { this.dropTarget = dt; } /** * Gets the associated drag-and-drop target, if there is one. * * @return the drop target */ public DropTarget getDropTarget() { return dropTarget; } /** * Returns the graphics configuration of this component, if there is one. * If it has not been set, it is inherited from the parent. * * @return the graphics configuration, or null * @since 1.3 */ public GraphicsConfiguration getGraphicsConfiguration() { return getGraphicsConfigurationImpl(); } /** * Returns the object used for synchronization locks on this component * when performing tree and layout functions. * * @return the synchronization lock for this component */ public final Object getTreeLock() { return treeLock; } /** * Returns the toolkit in use for this component. The toolkit is associated * with the frame this component belongs to. * * @return the toolkit for this component */ public Toolkit getToolkit() { if (peer != null) { Toolkit tk = peer.getToolkit(); if (tk != null) return tk; } // Get toolkit for lightweight component. if (parent != null) return parent.getToolkit(); return Toolkit.getDefaultToolkit(); } /** * Tests whether or not this component is valid. A invalid component needs * to have its layout redone. * * @return true if this component is valid * @see #validate() * @see #invalidate() */ public boolean isValid() { return valid; } /** * Tests if the component is displayable. It must be connected to a native * screen resource. This reduces to checking that peer is not null. A * containment hierarchy is made displayable when a window is packed or * made visible. * * @return true if the component is displayable * @see Container#add(Component) * @see Container#remove(Component) * @see Window#pack() * @see Window#show() * @see Window#dispose() * @since 1.2 */ public boolean isDisplayable() { return peer != null; } /** * Tests whether or not this component is visible. Except for top-level * frames, components are initially visible. * * @return true if the component is visible * @see #setVisible(boolean) */ public boolean isVisible() { return visible; } /** * Tests whether or not this component is actually being shown on * the screen. This will be true if and only if it this component is * visible and its parent components are all visible. * * @return true if the component is showing on the screen * @see #setVisible(boolean) */ public boolean isShowing() { if (! visible || peer == null) return false; return parent == null ? false : parent.isShowing(); } /** * Tests whether or not this component is enabled. Components are enabled * by default, and must be enabled to receive user input or generate events. * * @return true if the component is enabled * @see #setEnabled(boolean) */ public boolean isEnabled() { return enabled; } /** * Enables or disables this component. The component must be enabled to * receive events (except that lightweight components always receive mouse * events). * * @param enabled true to enable this component * * @see #isEnabled() * @see #isLightweight() * * @since 1.1 */ public void setEnabled(boolean enabled) { enable(enabled); } /** * Enables this component. * * @deprecated use {@link #setEnabled(boolean)} instead */ public void enable() { this.enabled = true; if (peer != null) peer.setEnabled (true); } /** * Enables or disables this component. * * @param enabled true to enable this component * * @deprecated use {@link #setEnabled(boolean)} instead */ public void enable(boolean enabled) { if (enabled) enable(); else disable(); } /** * Disables this component. * * @deprecated use {@link #setEnabled(boolean)} instead */ public void disable() { this.enabled = false; if (peer != null) peer.setEnabled (false); } /** * Checks if this image is painted to an offscreen image buffer that is * later copied to screen (double buffering reduces flicker). This version * returns false, so subclasses must override it if they provide double * buffering. * * @return true if this is double buffered; defaults to false */ public boolean isDoubleBuffered() { return false; } /** * Enables or disables input method support for this component. By default, * components have this enabled. Input methods are given the opportunity * to process key events before this component and its listeners. * * @param enable true to enable input method processing * @see #processKeyEvent(KeyEvent) * @since 1.2 */ public void enableInputMethods(boolean enable) { if (enable) eventMask |= AWTEvent.INPUT_ENABLED_EVENT_MASK; else eventMask &= ~AWTEvent.INPUT_ENABLED_EVENT_MASK; } /** * Makes this component visible or invisible. Note that it wtill might * not show the component, if a parent is invisible. * * @param visible true to make this component visible * * @see #isVisible() * * @since 1.1 */ public void setVisible(boolean visible) { // Inspection by subclassing shows that Sun's implementation calls // show(boolean) which then calls show() or hide(). It is the show() // method that is overriden in subclasses like Window. show(visible); } /** * Makes this component visible on the screen. * * @deprecated use {@link #setVisible(boolean)} instead */ public void show() { // We must set visible before showing the peer. Otherwise the // peer could post paint events before visible is true, in which // case lightweight components are not initially painted -- // Container.paint first calls isShowing () before painting itself // and its children. if(!isVisible()) { this.visible = true; // Avoid NullPointerExceptions by creating a local reference. ComponentPeer currentPeer=peer; if (currentPeer != null) currentPeer.setVisible(true); // The JDK repaints the component before invalidating the parent. // So do we. if (isShowing()) repaint(); // Invalidate the parent if we have one. The component itself must // not be invalidated. We also avoid NullPointerException with // a local reference here. Container currentParent = parent; if (currentParent != null) currentParent.invalidate(); ComponentEvent ce = new ComponentEvent(this,ComponentEvent.COMPONENT_SHOWN); getToolkit().getSystemEventQueue().postEvent(ce); } } /** * Makes this component visible or invisible. * * @param visible true to make this component visible * * @deprecated use {@link #setVisible(boolean)} instead */ public void show(boolean visible) { if (visible) show(); else hide(); } /** * Hides this component so that it is no longer shown on the screen. * * @deprecated use {@link #setVisible(boolean)} instead
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -