📄 component.java
字号:
* <code>p</code> is given in the parent's coordinate space.
* @param <code>p</code> 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 size();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>getSize()</code>.
*/
public Dimension size() {
return new Dimension(width, height);
}
/**
* Resizes this component so that it has width <code>width</code>
* and <code>height</code>.
* @param <code>width</code> The new width of this component in pixels.
* @param <code>height</code> The new height of this component in pixels.
* @see #getSize
* @see #setBounds
* @since JDK1.1
*/
public void setSize(int width, int height) {
resize(width, height);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setSize(int, int)</code>.
*/
public void resize(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 <code>d</code> The dimension specifying the new size
* of this component.
* @see #setSize
* @see #setBounds
* @since JDK1.1
*/
public void setSize(Dimension d) {
resize(d);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setSize(Dimension)</code>.
*/
public void resize(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 bounds();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>getBounds()</code>.
*/
public Rectangle bounds() {
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 <code>x</code> The new <i>x</i>-coordinate of this component.
* @param <code>y</code> The new <i>y</i>-coordinate of this component.
* @param <code>width</code> The new <code>width</code> of this component.
* @param <code>height</code> 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) {
reshape(x, y, width, height);
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>setBounds(int, int, int, int)</code>.
*/
public void reshape(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) {
boolean isLightweight =
(peer instanceof java.awt.peer.LightweightPeer);
// Remember the area this component occupied in its parent.
int oldParentX = this.x;
int oldParentY = this.y;
if (parent != null) {
oldParentX += parent.x;
oldParentY += parent.y;
}
int oldWidth = this.width;
int oldHeight = this.height;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
if (peer != null) {
if (isLightweight) {
peer.setBounds(x, y, width, height);
} else {
// native peer might be offset by more than direct
// parent since parent might be lightweight.
int nativeX = x;
int nativeY = y;
for(Component c = parent; (c != null) &&
(c.peer instanceof java.awt.peer.LightweightPeer);
c = c.parent) {
nativeX += c.x;
nativeY += c.y;
}
peer.setBounds(nativeX, nativeY, width, height);
}
if (resized) {
invalidate();
if (componentListener != null ||
(eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
ComponentEvent e = new ComponentEvent(this,
ComponentEvent.COMPONENT_RESIZED);
Toolkit.getEventQueue().postEvent(e);
}
}
if (moved &&
(componentListener != null ||
(eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0)) {
ComponentEvent e = new ComponentEvent(this,
ComponentEvent.COMPONENT_MOVED);
Toolkit.getEventQueue().postEvent(e);
}
if (parent != null && parent.valid) {
parent.invalidate();
}
}
if (isLightweight && isShowing()) {
// Repaint the old area ...
parent.repaint(oldParentX, oldParentY, oldWidth, oldHeight);
// ... then the new (this areas will be collapsed by
// the ScreenUpdater if they intersect).
repaint();
}
}
}
}
/**
* 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 <code>r<code> 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);
}
/**
* 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() {
return preferredSize();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>getPreferredSize()</code>.
*/
public Dimension preferredSize() {
/* Avoid grabbing the lock if a reasonable cached size value
* is available.
*/
Dimension dim = prefSize;
if (dim != null && isValid()) {
return dim;
}
synchronized (getTreeLock()) {
prefSize = (peer != null) ?
peer.preferredSize() :
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() {
return minimumSize();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>getMinimumSize()</code>.
*/
public Dimension minimumSize() {
/* Avoid grabbing the lock if a reasonable cached size value
* is available.
*/
Dimension dim = minSize;
if (dim != null && isValid()) {
return dim;
}
synchronized (getTreeLock()) {
minSize = (peer != null) ?
peer.minimumSize() :
size();
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() {
layout();
}
/**
* @deprecated As of JDK version 1.1,
* replaced by <code>doLayout()</code>.
*/
public void layout() {
}
/**
* 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 (!valid) {
synchronized (getTreeLock()) {
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 on
* the screen.
* @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 (peer instanceof java.awt.peer.LightweightPeer) {
// This is for a lightweight component, need to
// translate coordinate spaces and clip relative
// to the parent.
Graphics g = parent.getGraphics();
g.translate(x,y);
//g.setClip(0, 0, width, height); /* IBM 9309 */
g.clipRect (0, 0, width, height); /* IBM 9309 */
g.setFont(getFont());
return g;
} else {
ComponentPeer peer = this.peer;
return (peer != null) ? peer.getGraphics() : null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -