下面列出了java.awt.Container#getY ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(final Dialog dialog,
final double horizontalPercent,
final double verticalPercent) {
final Container parent = dialog.getParent();
if (parent == null)
{
centerFrameOnScreen(dialog);
return;
}
final Dimension d = dialog.getSize();
final Dimension p = parent.getSize();
final int baseX = parent.getX();
final int baseY = parent.getY();
final int x = baseX + (int) (horizontalPercent * p.width);
final int y = baseY + (int) (verticalPercent * p.height);
// make sure the dialog fits completely on the screen...
final Rectangle s = parent.getGraphicsConfiguration().getBounds();
final Rectangle r = new Rectangle(x, y, d.width, d.height);
dialog.setBounds(r.intersection(s));
}
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(Dialog dialog,
double horizontalPercent,
double verticalPercent) {
Dimension d = dialog.getSize();
Container parent = dialog.getParent();
Dimension p = parent.getSize();
int baseX = parent.getX() - d.width;
int baseY = parent.getY() - d.height;
int w = d.width + p.width;
int h = d.height + p.height;
int x = baseX + (int) (horizontalPercent * w);
int y = baseY + (int) (verticalPercent * h);
// make sure the dialog fits completely on the screen...
Rectangle s = getMaximumWindowBounds();
x = Math.min(x, (s.width - d.width));
x = Math.max(x, 0);
y = Math.min(y, (s.height - d.height));
y = Math.max(y, 0);
dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
}
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog the dialog to be positioned.
* @param horizontalPercent the relative location.
* @param verticalPercent the relative location.
*/
public static void positionDialogRelativeToParent(Dialog dialog,
double horizontalPercent,
double verticalPercent) {
Dimension d = dialog.getSize();
Container parent = dialog.getParent();
Dimension p = parent.getSize();
int baseX = parent.getX() - d.width;
int baseY = parent.getY() - d.height;
int w = d.width + p.width;
int h = d.height + p.height;
int x = baseX + (int) (horizontalPercent * w);
int y = baseY + (int) (verticalPercent * h);
// make sure the dialog fits completely on the screen...
Rectangle s = getMaximumWindowBounds();
x = Math.min(x, (s.width - d.width));
x = Math.max(x, 0);
y = Math.min(y, (s.height - d.height));
y = Math.max(y, 0);
dialog.setBounds(x + s.x, y + s.y, d.width, d.height);
}
/**
* Positions the specified dialog at a position relative to its parent.
*
* @param dialog
* the dialog to be positioned.
* @param horizontalPercent
* the relative location.
* @param verticalPercent
* the relative location.
*/
public static void positionDialogRelativeToParent( final Dialog dialog, final double horizontalPercent,
final double verticalPercent ) {
final Container parent = dialog.getParent();
if ( parent == null || ( parent.isVisible() == false ) ) {
positionFrameOnScreen( dialog, horizontalPercent, verticalPercent );
return;
}
final Dimension d = dialog.getSize();
final Dimension p = parent.getSize();
final int baseX = parent.getX();
final int baseY = parent.getY();
final int parentPointX = baseX + (int) ( horizontalPercent * p.width );
final int parentPointY = baseY + (int) ( verticalPercent * p.height );
final int dialogPointX = Math.max( 0, parentPointX - (int) ( horizontalPercent * d.width ) );
final int dialogPointY = Math.max( 0, parentPointY - (int) ( verticalPercent * d.height ) );
// make sure the dialog fits completely on the screen...
final Rectangle s = parent.getGraphicsConfiguration().getBounds();
final Rectangle r = new Rectangle( dialogPointX, dialogPointY, d.width, d.height );
final Rectangle intersectedDialogBounds = r.intersection( s );
if ( intersectedDialogBounds.width < d.width ) {
r.x = s.width - d.width;
r.width = d.width;
}
if ( intersectedDialogBounds.height < d.height ) {
r.y = s.height - d.height;
r.height = d.height;
}
final Rectangle finalIntersection = r.intersection( s );
dialog.setBounds( finalIntersection );
}
protected boolean canDirectlyAccessGraphics() {
// TODO: what about popup windows / tooltips???
// TODO: some of the queries could be cached instead of polling,
// for example isShowing(), isOpaque(), getParent() etc.
////// // Shouldn't access graphics - no buffering would cause flickering
////// if (bufferType == BUFFER_NONE) return false;
// Cannot access graphics - there are some child components
if (getComponentCount() != 0) return false;
// Cannot access graphics - component doesn't fully control its area
if (!isOpaque()) return false;
// Cannot access graphics - not in Swing tree
if (!(getParent() instanceof JComponent)) return false;
// Cannot access graphics - component not showing, doesn't make sense
if (!isShowing()) return false;
// Cannot access graphics - component area is not up-to-date
Rectangle dirtyRegion = RepaintManager.currentManager(this).
getDirtyRegion((JComponent)getParent());
if (dirtyRegion != null && dirtyRegion.width > 0 &&
dirtyRegion.height > 0) return false;
// --- Reused from JViewport -------------------------------------------
Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
Rectangle oldClip = new Rectangle();
Rectangle tmp2 = null;
Container parent;
Component lastParent = null;
int x, y, w, h;
for (parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
x = parent.getX();
y = parent.getY();
w = parent.getWidth();
h = parent.getHeight();
oldClip.setBounds(clip);
SwingUtilities.computeIntersection(0, 0, w, h, clip);
if (!clip.equals(oldClip)) return false;
if (lastParent != null && parent instanceof JComponent &&
!((JComponent)parent).isOptimizedDrawingEnabled()) {
Component comps[] = parent.getComponents();
int index = 0;
for (int i = comps.length - 1 ;i >= 0; i--) {
if (comps[i] == lastParent) {
index = i - 1;
break;
}
}
while (index >= 0) {
tmp2 = comps[index].getBounds(tmp2);
if (tmp2.intersects(clip)) return false;
index--;
}
}
clip.x += x;
clip.y += y;
lastParent = parent;
}
// No Window parent.
if (parent == null) return false;
return true;
}
protected boolean canDirectlyAccessGraphics() {
// TODO: what about popup windows / tooltips???
// TODO: some of the queries could be cached instead of polling,
// for example isShowing(), isOpaque(), getParent() etc.
////// // Shouldn't access graphics - no buffering would cause flickering
////// if (bufferType == BUFFER_NONE) return false;
// Cannot access graphics - there are some child components
if (getComponentCount() != 0) return false;
// Cannot access graphics - component doesn't fully control its area
if (!isOpaque()) return false;
// Cannot access graphics - not in Swing tree
if (!(getParent() instanceof JComponent)) return false;
// Cannot access graphics - component not showing, doesn't make sense
if (!isShowing()) return false;
// Cannot access graphics - component area is not up-to-date
Rectangle dirtyRegion = RepaintManager.currentManager(this).
getDirtyRegion((JComponent)getParent());
if (dirtyRegion != null && dirtyRegion.width > 0 &&
dirtyRegion.height > 0) return false;
// --- Reused from JViewport -------------------------------------------
Rectangle clip = new Rectangle(0, 0, getWidth(), getHeight());
Rectangle oldClip = new Rectangle();
Rectangle tmp2 = null;
Container parent;
Component lastParent = null;
int x, y, w, h;
for (parent = this; parent != null && isLightweightComponent(parent); parent = parent.getParent()) {
x = parent.getX();
y = parent.getY();
w = parent.getWidth();
h = parent.getHeight();
oldClip.setBounds(clip);
SwingUtilities.computeIntersection(0, 0, w, h, clip);
if (!clip.equals(oldClip)) return false;
if (lastParent != null && parent instanceof JComponent &&
!((JComponent)parent).isOptimizedDrawingEnabled()) {
Component comps[] = parent.getComponents();
int index = 0;
for (int i = comps.length - 1 ;i >= 0; i--) {
if (comps[i] == lastParent) {
index = i - 1;
break;
}
}
while (index >= 0) {
tmp2 = comps[index].getBounds(tmp2);
if (tmp2.intersects(clip)) return false;
index--;
}
}
clip.x += x;
clip.y += y;
lastParent = parent;
}
// No Window parent.
if (parent == null) return false;
return true;
}