📄 component.java
字号:
* 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) { firePropertyChange("background", background, c); if (peer != null) peer.setBackground(c); background = 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() { if (font != null) return font; return parent == null ? null : parent.getFont(); } /** * Sets the font for this component to the specified font. This is a bound * property. * * @param font the new font for this component * @see #getFont() */ public void setFont(Font f) { firePropertyChange("font", font, f); if (peer != null) peer.setFont(f); font = f; } /** * 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 locale the new locale for this component */ public void setLocale(Locale l) { firePropertyChange("locale", locale, l); locale = l; // 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 new Point(x, y); } /** * 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 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 getLocation(); } /** * 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) { if (this.x == x && this.y == y) return; invalidate(); this.x = x; this.y = y; if (peer != null) peer.setBounds(x, y, width, 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 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) { setLocation(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 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 new Dimension(width, height); } /** * Returns the size of this object. * * @return the size of this object * @deprecated use {@link #getSize()} instead */ public Dimension size() { return getSize(); } /** * 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) { if (this.width == width && this.height == height) return; invalidate(); this.width = width; this.height = height; if (peer != null) peer.setBounds(x, y, 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) { setSize(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) { setSize(d.width, d.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 * @deprecated use {@link #setSize(Dimension)} instead */ public void resize(Dimension d) { setSize(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 new Rectangle(x, y, width, 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 * @deprecated use {@link #getBounds()} instead */ public Rectangle bounds() { return getBounds(); } /** * 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) { if (this.x == x && this.y == y && width == w && height == h) return; invalidate(); this.x = x; this.y = y; width = w; height = h; if (peer != null) peer.setBounds(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 w the width of the rectangle * @param h 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) { setBounds(x, y, width, height); } /** * Sets the bounding rectangle for this component to the specified * rectangle. Note that these coordinates are relative to the parent, not * to the screen. * * @param r the new bounding rectangle * @throws NullPointerException if r is null * @see #getBounds() * @see #setLocation(Point) * @see #setSize(Dimension) * @since 1.1 */ public void setBounds(Rectangle r) { setBounds(r.x, r.y, r.width, r.height); } /** * Gets the x coordinate of the upper left corner. This is more efficient * than getBounds().x or getLocation().x. * * @return the current x coordinate * @since 1.2 */ public int getX() { return x; } /** * Gets the y coordinate of the upper left corner. This is more efficient * than getBounds().y or getLocation().y. * * @return the current y coordinate * @since 1.2 */ public int getY() { return y; } /** * Gets the width of the component. This is more efficient than * getBounds().width or getSize().width. * * @return the current width * @since 1.2 */ public int getWidth() { return width; } /** * Gets the height of the component. This is more efficient than * getBounds().height or getSize().height. * * @return the current width * @since 1.2 */ public int getHeight() { return height; } /** * Returns the bounds of this component. This allows reuse of an existing * rectangle, if r is non-null. * * @param r the rectangle to use, or null * @return the bounds */ public Rectangle getBounds(Rectangle r) { if (r == null) r = new Rectangle(); r.x = x; r.y = y; r.width = width; r.height = height; return r; } /** * Returns the size of this component. This allows reuse of an existing * dimension, if d is non-null. * * @param d the dimension to use, or null * @return the size */ public Dimension getSize(Dimension d) { if (d == null) d = new Dimension(); d.width = width; d.height = height; return d; } /** * Returns the location of this component. This allows reuse of an existing * point, if p is non-null. * * @param p the point to use, or null * @return the location */ public Point getLocation(Point p) { if (p == null) p = new Point(); p.x = x; p.y = y;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -