📄 component.java
字号:
/** * Return the current y coordinate of the components origin. * This method is preferable to writing component.getBounds().y, * or component.getLocation().y because it doesn't cause any * heap allocations. * * @return the current y coordinate of the components origin. * @since 1.2 */ public int getY() { return y; } /** * Return the current width of this component. * This method is preferable to writing component.getBounds().width, * or component.getSize().width because it doesn't cause any * heap allocations. * * @return the current width of this component. * @since 1.2 */ public int getWidth() { return width; } /** * Return the current height of this component. * This method is preferable to writing component.getBounds().height, * or component.getSize().height because it doesn't cause any * heap allocations. * * @return the current height of this component. * @since 1.2 */ public int getHeight() { return height; } /** * Store the bounds of this component into "return value" <b>rv</b> and * return <b>rv</b>. If rv is null a new Rectangle is allocated. * This version of getBounds() is useful if the caller * wants to avoid allocating a new Rectangle object on the heap. * * @param rv the return value, modified to the components bounds * @return rv */ public Rectangle getBounds(Rectangle rv) { if (rv == null) return getBounds(); rv.x = x; rv.y = y; rv.width = width; rv.height = height; return rv; } /** * Store the width/height of this component into "return value" <b>rv</b> * and return <b>rv</b>. If rv is null a new Dimension object is * allocated. This version of getSize() is useful if the * caller wants to avoid allocating a new Dimension object on the heap. * * @param rv the return value, modified to the components size * @return rv */ public Dimension getSize(Dimension rv) { if (rv == null) return getSize(); rv.width = width; rv.height = height; return rv; } /** * Store the x,y origin of this component into "return value" <b>rv</b> * and return <b>rv</b>. If rv is null a new Point is allocated. * This version of getLocation() is useful if the * caller wants to avoid allocating a new Point object on the heap. * * @param rv the return value, modified to the components location * @return rv */ public Point getLocation(Point rv) { if (rv == null) return getLocation(); rv.x = x; rv.y = y; return rv; } /** * Moves and resizes this component to conform to the new * bounding rectangle <code>r</code>. This component's new * position is specified by <code>r.x</code> and <code>r.y</code>, * and its new size is specified by <code>r.width</code> and * <code>r.height</code> * @param r The new bounding rectangle for 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) * @since JDK1.1 */ public void setBounds(Rectangle r) { setBounds(r.x, r.y, r.width, r.height); } /** * Returns true if this component is completely opaque, returns * false by default. * <p> * An opaque component paints every pixel within its * rectangular region. A non-opaque component paints only some of * its pixels, allowing the pixels underneath it to "show through". * A component that does not fully paint its pixels therefore * provides a degree of transparency. Only lightweight * components can be transparent. * <p> * Subclasses that guarantee to always completely paint their * contents should override this method and return true. All * of the "heavyweight" AWT components are opaque. * * @return true if this component is completely opaque. * @see #isLightweight * @since 1.2 */ public boolean isOpaque() { return displayable ? !isLightweight() : false; } /** * A lightweight component doesn't have a native toolkit peer. * Subclasses of Component and Container, other than the ones * defined in this package like Button or Scrollbar, are lightweight. * All of the Swing components are lightweights. * * This method will always return <code>false</code> if this Component * is not displayable because it is impossible to determine the * weight of an undisplayable Component. * * @return true if this component has a lightweight peer; false if * it has a native peer or no peer. * @see #isDisplayable * @since 1.2 */ public boolean isLightweight() { return displayable ? (!(this instanceof Window)) : false; } /** * Gets the preferred size of this component. * @return A dimension object indicating this component's preferred size. * @see #getMinimumSize * @see java.awt.LayoutManager */ public Dimension getPreferredSize() { /* Avoid grabbing the lock if a reasonable cached size value * is available. */ Dimension dim = prefSize; if (dim != null && isValid()) { return dim; } synchronized (getTreeLock()) { prefSize = isDisplayable() ? new Dimension(1, 1) : getMinimumSize(); return prefSize; } } /** * Gets the mininimum size of this component. * @return A dimension object indicating this component's minimum size. * @see #getPreferredSize * @see java.awtLayoutManager */ public Dimension getMinimumSize() { /* Avoid grabbing the lock if a reasonable cached size value * is available. */ Dimension dim = minSize; if (dim != null && isValid()) { return dim; } synchronized (getTreeLock()) { minSize = isDisplayable() ? new Dimension(1, 1) : getSize(); return minSize; } } /** * Gets the maximum size of this component. * @return A dimension object indicating this component's maximum size. * @see #getMinimumSize * @see #getPreferredSize * @see LayoutManager */ public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); } /** * Returns the alignment along the x axis. This specifies how * the component would like to be aligned relative to other * components. The value should be a number between 0 and 1 * where 0 represents alignment along the origin, 1 is aligned * the furthest away from the origin, 0.5 is centered, etc. */ public float getAlignmentX() { return CENTER_ALIGNMENT; } /** * Returns the alignment along the y axis. This specifies how * the component would like to be aligned relative to other * components. The value should be a number between 0 and 1 * where 0 represents alignment along the origin, 1 is aligned * the furthest away from the origin, 0.5 is centered, etc. */ public float getAlignmentY() { return CENTER_ALIGNMENT; } /** * Prompts the layout manager to lay out this component. This is * usually called when the component (more specifically, container) * is validated. * @see #validate * @see LayoutManager */ public void doLayout() {} /** * Ensures that this component has a valid layout. This method is * primarily intended to operate on instances of <code>Container</code>. * @see java.awt.Component#invalidate * @see java.awt.Component#doLayout() * @see java.awt.LayoutManager * @see java.awt.Container#validate * @since JDK1.0 */ public void validate() { if (this.cursor != null) if (this.cursor.getType() != Cursor.DEFAULT_CURSOR) setCursor(this.cursor); valid = true; } /** * Invalidates this component. This component and all parents * above it are marked as needing to be laid out. This method can * be called often, so it needs to execute quickly. * @see java.awt.Component#validate * @see java.awt.Component#doLayout * @see java.awt.LayoutManager * @since JDK1.0 */ public void invalidate() { synchronized (getTreeLock()) { /* Nullify cached layout and size information. * For efficiency, propagate invalidate() upwards only if * some other component hasn't already done so first. */ valid = false; prefSize = null; minSize = null; if (parent != null && parent.valid) { parent.invalidate(); } } } /** * Creates a graphics context for this component. This method will * return <code>null</code> if this component is currently not * displayable. * @return A graphics context for this component, or <code>null</code> * if it has none. * @see java.awt.Component#paint * @since JDK1.0 */ public Graphics getGraphics() { if (displayable) { boolean createNullGraphics = false; Component c = this; int x = 0, y = 0; while (c != null) { if (!c.visible) createNullGraphics = true; if (c instanceof Window) { Graphics g = null; if (createNullGraphics) g = new NullGraphics(c); else g = getToolkit().getGraphics((Window) c); if (g instanceof ConstrainableGraphics) { ((ConstrainableGraphics) g).constrain(x, y, width, height); } else { g.translate(x, y); g.setClip(0, 0, width, height); } return g; } x += c.x; y += c.y; c = c.parent; } } return null; } /** * Gets the font metrics for the specified font. * @param font The font for which font metrics is to be * obtained. * @return The font metrics for <code>font</code>. * @param font the font. * @return the font metrics for the specified font. * @see java.awt.Component#getFont * @see java.awt.Component#getPeer() * @see java.awt.peer.ComponentPeer#getFontMetrics(java.awt.Font) * @see java.awt.Toolkit#getFontMetrics(java.awt.Font) * @since JDK1.0 */ public FontMetrics getFontMetrics(Font font) { return getToolkit().getFontMetrics(font); } /** * Sets the cursor image to the specified cursor. This cursor * image is displayed when the <code>contains</code> method for * this component returns true for the current cursor location, and * this Component is visible, displayable, and enabled. Setting the * cursor of a <code>Container</code> causes that cursor to be displayed * within all of the container's subcomponents, except for those * that have a non-null cursor. * <p> * <em>Note: This operation is subject to * <a href="#restrictions">restriction</a> * in Personal Basis Profile.</em> * * @param cursor One of the constants defined * by the <code>Cursor</code> class. * If this parameter is null then this component will inherit * the cursor of its parent. * @see #isEnabled * @see #isShowing * @see java.awt.Component#getCursor * @see java.awt.Component#contains * @see java.awt.Toolkit#createCustomCursor * @see java.awt.Cursor * @since JDK1.1 */ public void setCursor(Cursor cursor) { this.cursor = cursor; Frame frame = getFrame(); if (frame != null && visible) frame.updateCursor(this); } /** * Gets the cursor set in the component. If the component does * not have a cursor set, the cursor of its parent is returned. * If no Cursor is set in the entire hierarchy, Cursor.DEFAULT_CURSOR is * returned. * @see #setCursor * @since JDK1.1 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -