📄 component.java
字号:
*/ public void hide() { if (isVisible()) { // Avoid NullPointerExceptions by creating a local reference. ComponentPeer currentPeer=peer; if (currentPeer != null) currentPeer.setVisible(false); boolean wasShowing = isShowing(); this.visible = false; // The JDK repaints the component before invalidating the parent. // So do we. if (wasShowing) 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_HIDDEN); getToolkit().getSystemEventQueue().postEvent(ce); } } /** * Returns this component's foreground color. If not set, this is inherited * from the parent. * * @return this component's foreground color, or null * @see #setForeground(Color) */ public Color getForeground() { if (foreground != null) return foreground; return parent == null ? null : parent.getForeground(); } /** * Sets this component's foreground color to the specified color. This is a * bound property. * * @param c the new foreground color * @see #getForeground() */ public void setForeground(Color c) { if (peer != null) peer.setForeground(c); Color previous = foreground; foreground = c; firePropertyChange("foreground", previous, c); } /** * Tests if the foreground was explicitly set, or just inherited from the * parent. * * @return true if the foreground has been set * @since 1.4 */ public boolean isForegroundSet() { return foreground != null; } /** * Returns this component's background color. If not set, this is inherited * from the parent. * * @return the background color of the component, or null * @see #setBackground(Color) */ public Color getBackground() { if (background != null) return background; return parent == null ? null : parent.getBackground(); } /** * Sets this component's background color to the specified color. The parts * of the component affected by the background color may by system dependent. * This is a bound property. * * @param c the new background color * @see #getBackground() */ public void setBackground(Color c) { // return if the background is already set to that color. if ((c != null) && c.equals(background)) return; // If c is null, inherit from closest ancestor whose bg is set. if (c == null && parent != null) c = parent.getBackground(); if (peer != null && c != null) peer.setBackground(c); Color previous = background; background = c; firePropertyChange("background", previous, c); } /** * Tests if the background was explicitly set, or just inherited from the * parent. * * @return true if the background has been set * @since 1.4 */ public boolean isBackgroundSet() { return background != null; } /** * Returns the font in use for this component. If not set, this is inherited * from the parent. * * @return the font for this component * @see #setFont(Font) */ public Font getFont() { Font f = font; if (f != null) return f; Component p = parent; if (p != null) return p.getFont(); if (peer != null) return peer.getGraphics().getFont(); return null; } /** * Sets the font for this component to the specified font. This is a bound * property. * * @param newFont the new font for this component * * @see #getFont() */ public void setFont(Font newFont) { if((newFont != null && (font == null || !font.equals(newFont))) || newFont == null) { Font oldFont = font; font = newFont; if (peer != null) peer.setFont(font); firePropertyChange("font", oldFont, newFont); invalidate(); } } /** * Tests if the font was explicitly set, or just inherited from the parent. * * @return true if the font has been set * @since 1.4 */ public boolean isFontSet() { return font != null; } /** * Returns the locale for this component. If this component does not * have a locale, the locale of the parent component is returned. * * @return the locale for this component * @throws IllegalComponentStateException if it has no locale or parent * @see #setLocale(Locale) * @since 1.1 */ public Locale getLocale() { if (locale != null) return locale; if (parent == null) throw new IllegalComponentStateException ("Component has no parent: can't determine Locale"); return parent.getLocale(); } /** * Sets the locale for this component to the specified locale. This is a * bound property. * * @param newLocale the new locale for this component */ public void setLocale(Locale newLocale) { if (locale == newLocale) return; Locale oldLocale = locale; locale = newLocale; firePropertyChange("locale", oldLocale, newLocale); // New writing/layout direction or more/less room for localized labels. invalidate(); } /** * Returns the color model of the device this componet is displayed on. * * @return this object's color model * @see Toolkit#getColorModel() */ public ColorModel getColorModel() { GraphicsConfiguration config = getGraphicsConfiguration(); return config != null ? config.getColorModel() : getToolkit().getColorModel(); } /** * Returns the location of this component's top left corner relative to * its parent component. This may be outdated, so for synchronous behavior, * you should use a component listner. * * @return the location of this component * @see #setLocation(int, int) * @see #getLocationOnScreen() * @since 1.1 */ public Point getLocation() { return location (); } /** * Returns the location of this component's top left corner in screen * coordinates. * * @return the location of this component in screen coordinates * @throws IllegalComponentStateException if the component is not showing */ public Point getLocationOnScreen() { if (! isShowing()) throw new IllegalComponentStateException("component " + getClass().getName() + " not showing"); // We know peer != null here. return peer.getLocationOnScreen(); } /** * Returns the location of this component's top left corner relative to * its parent component. * * @return the location of this component * @deprecated use {@link #getLocation()} instead */ public Point location() { return new Point (x, y); } /** * Moves this component to the specified location, relative to the parent's * coordinates. The coordinates are the new upper left corner of this * component. * * @param x the new X coordinate of this component * @param y the new Y coordinate of this component * @see #getLocation() * @see #setBounds(int, int, int, int) */ public void setLocation(int x, int y) { move (x, y); } /** * Moves this component to the specified location, relative to the parent's * coordinates. The coordinates are the new upper left corner of this * component. * * @param x the new X coordinate of this component * @param y the new Y coordinate of this component * @deprecated use {@link #setLocation(int, int)} instead */ public void move(int x, int y) { setBounds(x, y, this.width, this.height); } /** * Moves this component to the specified location, relative to the parent's * coordinates. The coordinates are the new upper left corner of this * component. * * @param p new coordinates for this component * @throws NullPointerException if p is null * @see #getLocation() * @see #setBounds(int, int, int, int) * @since 1.1 */ public void setLocation(Point p) { setLocation(p.x, p.y); } /** * Returns the size of this object. * * @return the size of this object * @see #setSize(int, int) * @since 1.1 */ public Dimension getSize() { return size (); } /** * Returns the size of this object. * * @return the size of this object * @deprecated use {@link #getSize()} instead */ public Dimension size() { return new Dimension (width, height); } /** * Sets the size of this component to the specified width and height. * * @param width the new width of this component * @param height the new height of this component * @see #getSize() * @see #setBounds(int, int, int, int) */ public void setSize(int width, int height) { resize (width, height); } /** * Sets the size of this component to the specified value. * * @param width the new width of the component * @param height the new height of the component * @deprecated use {@link #setSize(int, int)} instead */ public void resize(int width, int height) { setBounds(this.x, this.y, width, height); } /** * Sets the size of this component to the specified value. * * @param d the new size of this component * @throws NullPointerException if d is null * @see #setSize(int, int) * @see #setBounds(int, int, int, int) * @since 1.1 */ public void setSize(Dimension d) { resize (d); } /** * Sets the size of this component to the specified value. * * @param d the new size of this component * @throws NullPointerException if d is null * @deprecated use {@link #setSize(Dimension)} instead */ public void resize(Dimension d) { resize (d.width, d.height); } /** * Returns a bounding rectangle for this component. Note that the * returned rectange is relative to this component's parent, not to * the screen. * * @return the bounding rectangle for this component * @see #setBounds(int, int, int, int) * @see #getLocation() * @see #getSize() */ public Rectangle getBounds() { return bounds (); } /** * Returns a bounding rectangle for this component. Note that the * returned rectange is relative to this component's parent, not to * the screen. * * @return the bounding rectangle for this component * @deprecated use {@link #getBounds()} instead */ public Rectangle bounds() { return new Rectangle (x, y, width, height); } /** * Sets the bounding rectangle for this component to the specified values. * Note that these coordinates are relative to the parent, not to the screen. * * @param x the X coordinate of the upper left corner of the rectangle * @param y the Y coordinate of the upper left corner of the rectangle * @param w the width of the rectangle * @param h the height of the rectangle * @see #getBounds() * @see #setLocation(int, int) * @see #setLocation(Point) * @see #setSize(int, int) * @see #setSize(Dimension) * @since 1.1 */ public void setBounds(int x, int y, int w, int h) { reshape (x, y, w, h); } /** * Sets the bounding rectangle for this component to the specified values. * Note that these coordinates are relative to the parent, not to the screen. * * @param x the X coordinate of the upper left corner of the rectangle * @param y the Y coordinate of the upper left corner of the rectangle * @param width the width of the rectangle * @param height the height of the rectangle * @deprecated use {@link #setBounds(int, int, int, int)} instead */ public void reshape(int x, int y, int width, int height) { int oldx = this.x; int oldy = this.y; int oldwidth = this.width; int oldheight = this.height; if (this.x == x && this.y == y && this.width == width && this.height == height) return; invalidate (); this.x = x; this.y = y; this.width = width; this.height = height; if (peer != null) peer.setBounds (x, y, width, height); // Erase old bounds and repaint new bounds for lightweights. if (isLightweight() && isShowing()) { if (parent != null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -