📄 component.java
字号:
*/ transient AppContext appContext; /** * Constructs a new component. Class <code>Component</code> can be * extended directly to create a lightweight component that does not * utilize an opaque native window. A lightweight component must be * hosted by a native container somewhere higher up in the component * tree (for example, by a <code>Frame</code> object). */ protected Component() { appContext = AppContext.getAppContext(); SunToolkit.insertTargetMapping(this, appContext); } /** * Construct a name for this component. Called by getName() when the * name is null. */ String constructComponentName() { return null; // For strict compliance with prior JDKs, a Component // that doesn't set its name should return null from // getName(); } /** * Gets the name of the component. * @return This component's name. * @see #setName * @since JDK1.1 */ public String getName() { if (name == null && !nameExplicitlySet) { synchronized (this) { if (name == null && !nameExplicitlySet) name = constructComponentName(); } } return name; } /** * Sets the name of the component to the specified string. * @param name The string that is to be this * component's name. * @see #getName * @since JDK1.1 */ public void setName(String name) { synchronized (this) { this.name = name; nameExplicitlySet = true; } } /** * Gets the parent of this component. * @return The parent container of this component. * @since JDK1.0 */ public Container getParent() { return parent; } /** * Get the <code>GraphicsConfiguration</code> associated with this * <code>Component</code>. * If the <code>Component</code> has not been assigned a specific * <code>GraphicsConfiguration</code>, * the <code>GraphicsConfiguration</code> of the * <code>Component</code> object's top-level container is * returned. * If the <code>Component</code> has been created, but not yet added * to a <code>Container</code>, this method returns <code>null</code>. * @return the <code>GraphicsConfiguration</code> used by this * <code>Component</code> or <code>null</code> * @since 1.3 */ public GraphicsConfiguration getGraphicsConfiguration() { Frame frame = getFrame(); return (frame != null) ? frame.getGraphicsConfiguration() : null; } /** * Gets the locking object for AWT component-tree and layout * Gets this component's locking object (the object that owns the thread * sychronization monitor) for AWT component-tree and layout * operations. * @return This component's locking object. */ public final Object getTreeLock() { return LOCK; } /** * Gets the toolkit of this component. Note that * the frame that contains a component controls which * toolkit is used by that component. Therefore if the component * is moved from one frame to another, the toolkit it uses may change. * @return The toolkit of this component. * @since JDK1.0 */ public Toolkit getToolkit() { return Toolkit.getDefaultToolkit(); } /** * Determines whether this component is valid. A component is valid * when it is correctly sized and positioned within its parent * container and all its children are also valid. Components are * invalidated when they are first shown on the screen. * @return <code>true</code> if the component is valid; <code>false</code> * otherwise. * @see #validate * @see #invalidate * @since JDK1.0 */ public boolean isValid() { return valid; } /** * Determines whether this component is displayable. A component is * displayable when it is connected to a native screen resource. * <p> * A component is made displayable either when it is added to * a displayable containment hierarchy or when its containment * hierarchy is made displayable. * A containment hierarchy is made displayable when its ancestor * window is either packed or made visible. * <p> * A component is made undisplayable either when it is removed from * a displayable containment hierarchy or when its containment hierarchy * is made undisplayable. A containment hierarchy is made * undisplayable when its ancestor window is disposed. * * @return <code>true</code> if the component is displayable; * <code>false</code> otherwise. * @see java.awt.Container#add(java.awt.Component) * @see java.awt.Window#pack * @see java.awt.Window#show * @see java.awt.Container#remove(java.awt.Component) * @see java.awt.Window#dispose * @since 1.2 */ public boolean isDisplayable() { return displayable; } /** * Determines whether this component should be visible when its * parent is visible. Components are * initially visible, with the exception of top level components such * as <code>Frame</code> objects. * @return <code>true</code> if the component is visible; * <code>false</code> otherwise. * @see #setVisible * @since JDK1.0 */ public boolean isVisible() { return visible; } /** * Determines whether this component is showing on screen. This means * that the component must be visible, and it must be in a container * that is visible and showing. * @return <code>true</code> if the component is showing; * <code>false</code> otherwise. * @see #setVisible * @since JDK1.0 */ public boolean isShowing() { Container parent = this.parent; return (parent != null) ? (visible && parent.isShowing()) : false; } /** * Determines whether this component is enabled. An enabled component * can respond to user input and generate events. Components are * enabled initially by default. A component may be enabled or disabled by * calling its <code>setEnabled</code> method. * @return <code>true</code> if the component is enabled; * <code>false</code> otherwise. * @see #setEnabled * @since JDK1.0 */ public boolean isEnabled() { return enabled; } /** * Enables or disables this component, depending on the value of the * parameter <code>b</code>. An enabled component can respond to user * input and generate events. Components are enabled initially by default. * @param b If <code>true</code>, this component is * enabled; otherwise this component is disabled. * @see #isEnabled * @since JDK1.1 */ public void setEnabled(boolean b) { if (enabled && !b) { KeyboardFocusManager.clearMostRecentFocusOwner(this); synchronized (getTreeLock()) { enabled = false; if (isFocusOwner()) { // Don't clear the global focus owner. If transferFocus // fails, we want the focus to stay on the disabled // Component so that keyboard traversal, et. al. still // makes sense to the user. autoTransferFocus(false); } } } enabled = b; } /** * Returns true if this component is painted to an offscreen image * ("buffer") that's copied to the screen later. Component * subclasses that support double buffering should override this * method to return true if double buffering is enabled. * * @return false by default */ public boolean isDoubleBuffered() { return false; } /** * Enables or disables input method support for this component. If input * method support is enabled and the component also processes key events, * incoming events are offered to * the current input method and will only be processed by the component or * dispatched to its listeners if the input method does not consume them. * By default, input method support is enabled. * * @param enable true to enable, false to disable * @see #processKeyEvent * @since 1.2 */ public void enableInputMethods(boolean enable) { if (enable) { if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) return; // If this component already has focus, then activate the // input method by dispatching a synthesized focus gained // event. if (isFocusOwner()) { InputContext inputContext = getInputContext(); if (inputContext != null) { FocusEvent focusGainedEvent = new FocusEvent(this, FocusEvent.FOCUS_GAINED); inputContext.dispatchEvent(focusGainedEvent); } } eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK; } else { if (areInputMethodsEnabled()) { InputContext inputContext = getInputContext(); if (inputContext != null) { inputContext.endComposition(); inputContext.removeNotify(this); } } eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK; } } /** * Shows or hides this component depending on the value of parameter * <code>b</code>. * @param b If <code>true</code>, shows this component; * otherwise, hides this component. * @see #isVisible * @since JDK1.1 */ public void setVisible(boolean b) { if (b) show(); else hide(); } void show() { if (!visible) { synchronized (getTreeLock()) { visible = true; if (isLightweight()) repaint(); if (componentListener != null || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) { ComponentEvent e = new ComponentEvent(this, ComponentEvent.COMPONENT_SHOWN); Toolkit.getEventQueue().postEvent(e); //SunToolkit.postEvent(appContext, e); } } Container parent = this.parent; if (parent != null) parent.invalidate(); } } void hide() { if (visible) { clearCurrentFocusCycleRootOnHide(); clearMostRecentFocusOwnerOnHide(); synchronized (getTreeLock()) { visible = false; if (containsFocus()) { autoTransferFocus(true); } if (componentListener != null || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) { ComponentEvent e = new ComponentEvent(this, ComponentEvent.COMPONENT_HIDDEN); Toolkit.getEventQueue().postEvent(e); //SunToolkit.postEvent(appContext, e); } } Container parent = this.parent; if (parent != null) { parent.invalidate(); } } } /** * Gets the foreground color of this component. * @return This component's foreground color. If this component does * not have a foreground color, the foreground color of its parent * is returned. * @see #setForeground * @since JDK1.0 */ public Color getForeground() { Color foreground = this.foreground; if (foreground != null) { return foreground; } Container parent = this.parent; return (parent != null) ? parent.getForeground() : null; } /** * Sets the foreground color of this component. * @param c The color to become this component's * foreground color. * If this parameter is null then this component will inherit * the foreground color of its parent. * @see #getForeground * @since JDK1.0 */ public void setForeground(Color c) { Color oldColor = foreground; foreground = c; // This is a bound property, so report the change to // any registered listeners. (Cheap if there are none.) firePropertyChange("foreground", oldColor, c); } /** * Returns whether the foreground color has been explicitly set for this * Component. If this method returns <code>false</code>, this Component is * inheriting its foreground color from an ancestor. * * @return <code>true</code> if the foreground color has been explicitly * set for this Component; <code>false</code> otherwise. * @since 1.4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -