📄 component.java
字号:
{ Rectangle oldBounds = new Rectangle(oldx, oldy, oldwidth, oldheight); Rectangle newBounds = new Rectangle(x, y, width, height); Rectangle destroyed = oldBounds.union(newBounds); if (!destroyed.isEmpty()) parent.repaint(0, destroyed.x, destroyed.y, destroyed.width, destroyed.height); } } // Only post event if this component is visible and has changed size. if (isShowing () && (oldx != x || oldy != y)) { ComponentEvent ce = new ComponentEvent(this, ComponentEvent.COMPONENT_MOVED); getToolkit().getSystemEventQueue().postEvent(ce); } if (isShowing () && (oldwidth != width || oldheight != height)) { ComponentEvent ce = new ComponentEvent(this, ComponentEvent.COMPONENT_RESIZED); getToolkit().getSystemEventQueue().postEvent(ce); } } /** * 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; return p; } /** * Tests if this component is opaque. All "heavyweight" (natively-drawn) * components are opaque. A component is opaque if it draws all pixels in * the bounds; a lightweight component is partially transparent if it lets * pixels underneath show through. Subclasses that guarantee that all pixels * will be drawn should override this. * * @return true if this is opaque * @see #isLightweight() * @since 1.2 */ public boolean isOpaque() { return ! isLightweight(); } /** * Return whether the component is lightweight. That means the component has * no native peer, but is displayable. This applies to subclasses of * Component not in this package, such as javax.swing. * * @return true if the component has a lightweight peer * @see #isDisplayable() * @since 1.2 */ public boolean isLightweight() { return peer instanceof LightweightPeer; } /** * Returns the component's preferred size. * * @return the component's preferred size * @see #getMinimumSize() * @see LayoutManager */ public Dimension getPreferredSize() { return preferredSize(); } /** * Returns the component's preferred size. * * @return the component's preferred size * @deprecated use {@link #getPreferredSize()} instead */ public Dimension preferredSize() { if (prefSize == null) if (peer == null) return new Dimension(width, height); else prefSize = peer.getPreferredSize(); return prefSize; } /** * Returns the component's minimum size. * * @return the component's minimum size * @see #getPreferredSize() * @see LayoutManager */ public Dimension getMinimumSize() { return minimumSize(); } /** * Returns the component's minimum size. * * @return the component's minimum size * @deprecated use {@link #getMinimumSize()} instead */ public Dimension minimumSize() { if (minSize == null) minSize = (peer != null ? peer.getMinimumSize() : new Dimension(width, height)); return minSize; } /** * Returns the component's maximum size. * * @return the component's maximum size * @see #getMinimumSize() * @see #getPreferredSize() * @see LayoutManager */ public Dimension getMaximumSize() { return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE); } /** * Returns the preferred horizontal alignment of this component. The value * returned will be between {@link #LEFT_ALIGNMENT} and * {@link #RIGHT_ALIGNMENT}, inclusive. * * @return the preferred horizontal alignment of this component */ public float getAlignmentX() { return CENTER_ALIGNMENT; } /** * Returns the preferred vertical alignment of this component. The value * returned will be between {@link #TOP_ALIGNMENT} and * {@link #BOTTOM_ALIGNMENT}, inclusive. * * @return the preferred vertical alignment of this component */ public float getAlignmentY() { return CENTER_ALIGNMENT; } /** * Calls the layout manager to re-layout the component. This is called * during validation of a container in most cases. * * @see #validate() * @see LayoutManager */ public void doLayout() { layout (); } /** * Calls the layout manager to re-layout the component. This is called * during validation of a container in most cases. * * @deprecated use {@link #doLayout()} instead */ public void layout() { // Nothing to do unless we're a container. } /** * Called to ensure that the layout for this component is valid. This is * usually called on containers. * * @see #invalidate() * @see #doLayout() * @see LayoutManager * @see Container#validate() */ public void validate() { valid = true; } /** * Invalidates this component and all of its parent components. This will * cause them to have their layout redone. This is called frequently, so * make it fast. */ public void invalidate() { valid = false; prefSize = null; minSize = null; if (parent != null && parent.isValid()) parent.invalidate(); } /** * Returns a graphics object for this component. Returns <code>null</code> * if this component is not currently displayed on the screen. * * @return a graphics object for this component * @see #paint(Graphics) */ public Graphics getGraphics() { if (peer != null) { Graphics gfx = peer.getGraphics(); // Create peer for lightweights. if (gfx == null && parent != null) { gfx = parent.getGraphics(); Rectangle bounds = getBounds(); gfx.setClip(bounds); gfx.translate(bounds.x, bounds.y); return gfx; } gfx.setFont(font); return gfx; } return null; } /** * Returns the font metrics for the specified font in this component. * * @param font the font to retrieve metrics for * @return the font metrics for the specified font * @throws NullPointerException if font is null * @see #getFont() * @see Toolkit#getFontMetrics(Font) */ public FontMetrics getFontMetrics(Font font) { return peer == null ? getToolkit().getFontMetrics(font) : peer.getFontMetrics(font); } /** * Sets the cursor for this component to the specified cursor. The cursor * is displayed when the point is contained by the component, and the * component is visible, displayable, and enabled. This is inherited by * subcomponents unless they set their own cursor. * * @param cursor the new cursor for this component * @see #isEnabled() * @see #isShowing() * @see #getCursor() * @see #contains(int, int) * @see Toolkit#createCustomCursor(Image, Point, String) */ public void setCursor(Cursor cursor) { this.cursor = cursor; if (peer != null) peer.setCursor(cursor); } /** * Returns the cursor for this component. If not set, this is inherited * from the parent, or from Cursor.getDefaultCursor(). * * @return the cursor for this component */ public Cursor getCursor() { if (cursor != null) return cursor; return parent != null ? parent.getCursor() : Cursor.getDefaultCursor(); } /** * Tests if the cursor was explicitly set, or just inherited from the parent. * * @return true if the cursor has been set * @since 1.4 */ public boolean isCursorSet() { return cursor != null; } /** * Paints this component on the screen. The clipping region in the graphics * context will indicate the region that requires painting. This is called * whenever the component first shows, or needs to be repaired because * something was temporarily drawn on top. It is not necessary for * subclasses to call <code>super.paint(g)</code>. Components with no area * are not painted. * * @param g the graphics context for this paint job * @see #update(Graphics) */ public void paint(Graphics g) { // This is a callback method and is meant to be overridden by subclasses // that want to perform custom painting. } /** * Updates this component. This is called in response to * <code>repaint</code>. This method fills the component with the * background color, then sets the foreground color of the specified * graphics context to the foreground color of this component and calls * the <code>paint()</code> method. The coordinates of the graphics are * relative to this component. Subclasses should call either * <code>super.update(g)</code> or <code>paint(g)</code>. * * @param g the graphics context for this update * * @see #paint(Graphics) * @see #repaint() * * @specnote In contrast to what the spec says, tests show that the exact * behaviour is to clear the background on lightweight and * top-level components only. Heavyweight components are not * affected by this method and only call paint(). */ public void update(Graphics g) { // Tests show that the clearing of the background is only done in // two cases: // - If the component is lightweight (yes this is in contrast to the spec). // or // - If the component is a toplevel container. if (isLightweight() || getParent() == null) { Rectangle clip = g.getClipBounds(); if (clip == null) g.clearRect(0, 0, width, height); else g.clearRect(clip.x, clip.y, clip.width, clip.height); } paint(g); } /** * Paints this entire component, including any sub-components. * * @param g the graphics context for this paint job * * @see #paint(Graphics) */ public void paintAll(Graphics g) { if (! visible) return; paint(g); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -