📄 component.java
字号:
*/ public boolean isForegroundSet() { return (foreground != null); } /** * Gets the background color of this component. * @return This component's background color. If this component does * not have a background color, the background color of its parent * is returned. * @see java.awt.Component#setBackground(java.awt.Color) * @since JDK1.0 */ public Color getBackground() { Color background = this.background; if (background != null) { return background; } Container parent = this.parent; return (parent != null) ? parent.getBackground() : null; } /** * Sets the background color of this component. * @param c The color to become this component's color. * If this parameter is null then this component will inherit * the background color of its parent. * background color. * @see #getBackground * @since JDK1.0 */ public void setBackground(Color c) { Color oldColor = background; background = c; // This is a bound property, so report the change to // any registered listeners. (Cheap if there are none.) firePropertyChange("background", oldColor, c); } /** * Returns whether the background color has been explicitly set for this * Component. If this method returns <code>false</code>, this Component is * inheriting its background color from an ancestor. * * @return <code>true</code> if the background color has been explicitly * set for this Component; <code>false</code> otherwise. * @since 1.4 */ public boolean isBackgroundSet() { return (background != null); } /** * Gets the font of this component. * @return This component's font. If a font has not been set * for this component, the font of its parent is returned. * @see #setFont * @since JDK1.0 */ public Font getFont() { Font font = this.font; if (font != null) { return font; } Container parent = this.parent; return (parent != null) ? parent.getFont() : null; } /** * Sets the font of this component. * @param f The font to become this component's font. * If this parameter is null then this component will inherit * the font of its parent. * @see #getFont * @since JDK1.0 */ public void setFont(Font f) { Font oldFont = font; font = f; // This is a bound property, so report the change to // any registered listeners. (Cheap if there are none.) firePropertyChange("font", oldFont, font); } /** * Returns whether the font has been explicitly set for this Component. If * this method returns <code>false</code>, this Component is inheriting its * font from an ancestor. * * @return <code>true</code> if the font has been explicitly set for this * Component; <code>false</code> otherwise. * @since 1.4 */ public boolean isFontSet() { return (font != null); } /** * Gets the locale of this component. * @return This component's locale. If this component does not * have a locale, the locale of its parent is returned. * @see #setLocale * @exception IllegalComponentStateException If the Component * does not have its own locale and has not yet been added to * a containment hierarchy such that the locale can be determined * from the containing parent. * @since JDK1.1 */ public Locale getLocale() { Locale locale = this.locale; if (locale != null) { return locale; } Container parent = this.parent; if (parent == null) { throw new IllegalComponentStateException("This component must have a parent in order to determine its locale"); } else { return parent.getLocale(); } } /** * Sets the locale of this component. * @param l The locale to become this component's locale. * @see #getLocale * @since JDK1.1 */ public void setLocale(Locale l) { locale = l; // This could change the preferred size of the Component. if (valid) { invalidate(); } } /** * Gets the instance of <code>ColorModel</code> used to display * the component on the output device. * @return The color model used by this component. * @see java.awt.image.ColorModel * @see java.awt.peer.ComponentPeer#getColorModel() * @see java.awt.Toolkit#getColorModel() * @since JDK1.0 */ public ColorModel getColorModel() { return getToolkit().getColorModel(); } /** * Gets the location of this component in the form of a * point specifying the component's top-left corner. * The location will be relative to the parent's coordinate space. * <p> * Due to the asynchronous nature of native event handling, this * method can return outdated values (for instance, after several calls * of <code>setLocation()</code> in rapid succession). For this * reason, the recommended method of obtaining a Component's position is * within <code>java.awt.event.ComponentListener.componentMoved()</code>, * which is called after the operating system has finished moving the * Component. * </p> * @return An instance of <code>Point</code> representing * the top-left corner of the component's bounds in the coordinate * space of the component's parent. * @see #setLocation * @see #getLocationOnScreen * @since JDK1.1 */ public Point getLocation() { return new Point(x, y); } /** * Gets the location of this component in the form of a point * specifying the component's top-left corner in the screen's * coordinate space. * @return An instance of <code>Point</code> representing * the top-left corner of the component's bounds in the * coordinate space of the screen. * @see #setLocation * @see #getLocation */ public Point getLocationOnScreen() { synchronized (getTreeLock()) { Component c = this; Point location = new Point(0, 0); while (c != null && c.visible) { location.x += c.x; location.y += c.y; if (c instanceof Window) return location; c = c.parent; } throw new IllegalComponentStateException("component must be showing on the screen to determine its location"); } } /** * Moves this component to a new location. The top-left corner of * the new location is specified by the <code>x</code> and <code>y</code> * parameters in the coordinate space of this component's parent. * @param x The <i>x</i>-coordinate of the new location's * top-left corner in the parent's coordinate space. * @param y The <i>y</i>-coordinate of the new location's * top-left corner in the parent's coordinate space. * @see #getLocation * @see #setBounds * @since JDK1.1 */ public void setLocation(int x, int y) { setBounds(x, y, width, height); } /** * Moves this component to a new location. The top-left corner of * the new location is specified by point <code>p</code>. Point * <code>p</code> is given in the parent's coordinate space. * @param p The point defining the top-left corner * of the new location, given in the coordinate space of this * component's parent. * @see #getLocation * @see #setBounds * @since JDK1.1 */ public void setLocation(Point p) { setLocation(p.x, p.y); } /** * Returns the size of this component in the form of a * <code>Dimension</code> object. The <code>height</code> * field of the <code>Dimension</code> object contains * this component's height, and the <code>width</code> * field of the <code>Dimension</code> object contains * this component's width. * @return A <code>Dimension</code> object that indicates the * size of this component. * @see #setSize * @since JDK1.1 */ public Dimension getSize() { return new Dimension(width, height); } /** * Resizes this component so that it has width <code>width</code> * and <code>height</code>. * @param width The new width of this component in pixels. * @param height The new height of this component in pixels. * @see #getSize * @see #setBounds * @since JDK1.1 */ public void setSize(int width, int height) { setBounds(x, y, width, height); } /** * Resizes this component so that it has width <code>d.width</code> * and height <code>d.height</code>. * @param d The dimension specifying the new size * of this component. * @see #setSize * @see #setBounds * @since JDK1.1 */ public void setSize(Dimension d) { setSize(d.width, d.height); } /** * Gets the bounds of this component in the form of a * <code>Rectangle</code> object. The bounds specify this * component's width, height, and location relative to * its parent. * @return A rectangle indicating this component's bounds. * @see #setBounds * @see #getLocation * @see #getSize */ public Rectangle getBounds() { return new Rectangle (x, y, width, height); } /** * Moves and resizes this component. The new location of the top-left * corner is specified by <code>x</code> and <code>y</code>, and the * new size is specified by <code>width</code> and <code>height</code>. * @param x The new <i>x</i>-coordinate of this component. * @param y The new <i>y</i>-coordinate of this component. * @param width The new <code>width</code> of this component. * @param height The new <code>height</code> of this * component. * @see java.awt.Component#getBounds * @see java.awt.Component#setLocation(int, int) * @see java.awt.Component#setLocation(java.awt.Point) * @see java.awt.Component#setSize(int, int) * @see java.awt.Component#setSize(java.awt.Dimension) * @JDK1.1 */ public void setBounds(int x, int y, int width, int height) { synchronized (getTreeLock()) { boolean resized = (this.width != width) || (this.height != height); boolean moved = (this.x != x) || (this.y != y); if (resized || moved) { // Bug Fix - 4662934 // Remember the area this component occupied in its parent. int oldParentX = this.x; int oldParentY = this.y; int oldWidth = this.width; int oldHeight = this.height; this.x = x; this.y = y; this.width = width; this.height = height; if (displayable) { if (resized) { invalidate(); } if (parent != null && parent.valid) { parent.invalidate(); } } if (resized) { if (componentListener != null || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) { ComponentEvent e = new ComponentEvent(this, ComponentEvent.COMPONENT_RESIZED); Toolkit.getEventQueue().postEvent(e); //SunToolkit.postEvent(appContext, e); // Container.dispatchEventImpl will create // HierarchyEvents } } if (moved) { if (componentListener != null || (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) { ComponentEvent e = new ComponentEvent(this, ComponentEvent.COMPONENT_MOVED); Toolkit.getEventQueue().postEvent(e); //SunToolkit.postEvent(appContext, e); } } if (visible) { // Bug Fix - 4662934 // Repaint the old area ... if (parent != null) { parent.repaint(oldParentX, oldParentY, oldWidth, oldHeight); } // ... then the new (these areas will be collapsed by // coalesceEvents if they intersect). repaint(); } } } } /** * Return the current x coordinate of the components origin. * This method is preferable to writing component.getBounds().x, * or component.getLocation().x because it doesn't cause any * heap allocations. * * @return the current x coordinate of the components origin. * @since 1.2 */ public int getX() { return x; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -