下面列出了java.awt.Container#getSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
boolean hitBoundary = (f.getWidth() != newWidth || f.getHeight() != newHeight);
if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
Container parent = f.getParent();
Dimension parentSize = parent.getSize();
// Limit the unit window or tool windows to stay inside and never go outside of
// the desktop
// or always show up fully (never show up less than the full window)
int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - 40);// newHeight);
if (f != null)
f.setBounds(boundedX, boundedY, newWidth, newHeight);
} else {
if (f != null)
f.setBounds(newX, newY, newWidth, newHeight);
}
if (hitBoundary) {
if (f != null)
f.validate();
}
}
@Override
public void setBoundsForFrame(JComponent f, int newX, int newY, int newWidth, int newHeight) {
boolean hitBoundary = (f.getWidth() != newWidth || f.getHeight() != newHeight);
if (!inBounds((JInternalFrame) f, newX, newY, newWidth, newHeight)) {
Container parent = f.getParent();
Dimension parentSize = parent.getSize();
// Limit the unit window or tool windows to stay inside and never go outside of
// the desktop
// or always show up fully (never show up less than the full window)
int boundedX = (int) Math.min(Math.max(0, newX), parentSize.getWidth() - newWidth);
int boundedY = (int) Math.min(Math.max(0, newY), parentSize.getHeight() - 40);// newHeight);
if (f != null)
f.setBounds(boundedX, boundedY, newWidth, newHeight);
} else {
if (f != null)
f.setBounds(newX, newY, newWidth, newHeight);
}
if (hitBoundary) {
if (f != null)
f.validate();
}
}
public void layoutContainer(Container parent) {
int c = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension size = parent.getSize();
size.width = Math.min(size.width, maximumLayoutSize(parent).width);
int x = insets.left;
int y = insets.top;
int w = size.width - x - insets.right - HGAP * (c - 1);
int h = size.height - y - insets.bottom;
int m = w % c;
w /= c;
for (int i = 0; i < c; i++) {
int o = i < m ? 1 : 0;
parent.getComponent(i).setBounds(x, y, w + o, h);
x += w + o + HGAP;
}
}
public void layoutContainer(Container parent) {
int c = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension size = parent.getSize();
size.width = Math.min(size.width, maximumLayoutSize(parent).width);
int x = insets.left;
int y = insets.top;
int w = size.width - x - insets.right - HGAP * (c - 1);
int h = size.height - y - insets.bottom;
int m = w % c;
w /= c;
for (int i = 0; i < c; i++) {
int o = i < m ? 1 : 0;
parent.getComponent(i).setBounds(x, y, w + o, h);
x += w + o + HGAP;
}
}
/**
* 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);
}
public void showGoToDialog() {
if (goToDialog==null) {
goToDialog = new GoToDialog(this);
// center dialog on videoPanel view
Container c = VideoPlayer.this.getParent();
while (c!=null) {
if (c instanceof JSplitPane) {
Dimension dim = c.getSize();
Point p = c.getLocationOnScreen();
int x = (dim.width - goToDialog.getBounds().width) / 2;
int y = (dim.height - goToDialog.getBounds().height) / 2;
goToDialog.setLocation(p.x+x, p.y+y);
break;
}
c = c.getParent();
}
}
else {
goToDialog.setPlayer(this);
}
goToDialog.setVisible(true);
}
/**
* 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));
}
/**
* Lays out the components.
*
* @param parent the parent.
*/
public void layoutContainer(final Container parent) {
synchronized (parent.getTreeLock()) {
if (parent.getComponentCount() > 0) {
final Insets insets = parent.getInsets();
final Dimension parentSize = parent.getSize();
final Component component = parent.getComponent(0);
final Dimension componentSize = component.getPreferredSize();
final int xx = insets.left + (
Math.max((parentSize.width - insets.left - insets.right
- componentSize.width) / 2, 0)
);
final int yy = insets.top + (
Math.max((parentSize.height - insets.top - insets.bottom
- componentSize.height) / 2, 0));
component.setBounds(xx, yy, componentSize.width,
componentSize.height);
}
}
}
/**
* @param comp The component to work with.
* @return Whether the component should be expanded to fit.
*/
public static boolean shouldTrackViewportHeight(Component comp) {
Container parent = comp.getParent();
if (parent instanceof JViewport) {
Dimension available = parent.getSize();
Dimension prefSize = comp.getPreferredSize();
return prefSize.height < available.height;
}
return false;
}
/**
* Lays out the specified container.
* @param parent the container to be laid out
*/
public void layoutContainer(Container parent) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension size = parent.getSize();
int c = horizontal ? insets.left : insets.top;
int x, y;
int ebx = size.width - insets.right;
size.width -= (insets.left + insets.right);
size.height -= (insets.top + insets.bottom);
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
Dimension pref = comp.getPreferredSize();
if (comp instanceof EnableButton) {
ebx -= 4;
ebx -= pref.width;
x = ebx;
y = (insets.top - pref.height) / 2;
} else if (horizontal) {
x = c;
c += pref.width;
y = insets.top;
pref.height = size.height;
} else {
x = insets.left;
pref.width = size.width;
y = c;
c += pref.height;
}
comp.setBounds(x, y, pref.width, pref.height);
}
}
/**
* Lays out the specified container.
* @param parent the container to be laid out
*/
public void layoutContainer(Container parent) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension size = parent.getSize();
int c = horizontal ? insets.left : insets.top;
int x, y;
int ebx = size.width - insets.right;
size.width -= (insets.left + insets.right);
size.height -= (insets.top + insets.bottom);
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
Dimension pref = comp.getPreferredSize();
if (comp instanceof EnableButton) {
ebx -= 4;
ebx -= pref.width;
x = ebx;
y = (insets.top - pref.height) / 2;
} else if (horizontal) {
x = c;
c += pref.width;
y = insets.top;
pref.height = size.height;
} else {
x = insets.left;
pref.width = size.width;
y = c;
c += pref.height;
}
comp.setBounds(x, y, pref.width, pref.height);
}
}
/**
* Lays out the specified container.
* @param parent the container to be laid out
*/
public void layoutContainer(Container parent) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension size = parent.getSize();
int c = horizontal ? insets.left : insets.top;
int x, y;
int ebx = size.width - insets.right;
size.width -= (insets.left + insets.right);
size.height -= (insets.top + insets.bottom);
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
Dimension pref = comp.getPreferredSize();
if (comp instanceof EnableButton) {
ebx -= 4;
ebx -= pref.width;
x = ebx;
y = (insets.top - pref.height) / 2;
} else if (horizontal) {
x = c;
c += pref.width;
y = insets.top;
pref.height = size.height;
} else {
x = insets.left;
pref.width = size.width;
y = c;
c += pref.height;
}
comp.setBounds(x, y, pref.width, pref.height);
}
}
@Override
public void layoutContainer(Container parent)
{
Dimension size = parent.getSize();
ticketComboBox.setBounds(40, 20, size.width - 80, 20);
int w = size.width - 40;
int y = 60;
infoPane.setBounds(5, 5, w-10, 5);
chunkPane.setBounds(5, 5, w-10, 5);
infoScrollPane.setBounds(20, y, w, Math.min(size.height - 80, infoPane.getPreferredSize().height+10));
y += 10+infoScrollPane.getHeight();
chunkScrollPane.setBounds(20, y, w, Math.max(0, size.height-40-y));
}
@Override
public void layoutContainer(Container parent) {
Dimension size = parent.getSize();
Insets insets = parent.getInsets();
int itop = insets.top;
int ileft = insets.left;
int ibottom = insets.bottom;
int iright = insets.right;
int rightWidth = right.getPreferredSize().width;
int bottomHeight = bottom.getPreferredSize().height;
int centerWidth = size.width - rightWidth - ileft - iright;
int centerHeight = size.height - bottomHeight - itop - ibottom;
center.setBounds(ileft, itop, centerWidth, centerHeight);
right.setBounds(ileft + centerWidth, itop, rightWidth, centerHeight);
// Lay out all status components, in order
for (Component comp : leftOfScrollBar) {
Dimension dim = comp.getPreferredSize();
comp.setBounds(ileft, itop + centerHeight, dim.width, bottomHeight);
ileft += dim.width;
}
bottom.setBounds(ileft, itop + centerHeight, size.width - rightWidth - ileft - iright, bottomHeight);
}
@Override
public void layoutContainer(Container parent) {
// Maximum dimensions available for use
Dimension realDim = parent.getSize();
Insets insets = parent.getInsets();
Dimension preferred = preferredLayoutSize(parent);
final int stretch = d.getPrimary(realDim) - d.getPrimary(preferred);
// remove the insets for the actual area we have in use
realDim.width -= insets.left + insets.right;
realDim.height -= insets.top + insets.bottom;
Dimension position = new Dimension(insets.left, insets.top);
// Check the conditions, and pass the task to the proper layout method
if (stretch >= 0) {
layoutSufficientSpace(parent, realDim, position, stretch);
} else {
Dimension minDim = minimumLayoutSize(parent);
// remove the insets for the actual area we have in use
minDim.width -= insets.left + insets.right;
minDim.height -= insets.top + insets.bottom;
final int squeeze = d.getPrimary(realDim) - d.getPrimary(minDim);
if (squeeze < 0) {
layoutUnderMinimumSpace(parent, realDim, position);
} else {
layoutWithSqueeze(parent, realDim, position, stretch);
}
}
}
/**
* Description of the Method
*
*@param target Description of Parameter
*/
public void layoutContainer(Container target) {
synchronized (target.getTreeLock()) {
Insets insets = target.getInsets();
int maxheight = target.getHeight() - (insets.top + insets.bottom) - 2 * _vgap;
int maxwidth = target.getWidth() - (insets.left + insets.right) - 2 * _hgap;
int nmembers = target.getComponentCount();
Dimension preferredSize = preferredLayoutSize(target);
Dimension targetSize = target.getSize();
int y = (_valign == TOP) ? insets.top
: (_valign == CENTER) ? (targetSize.height - preferredSize.height) / 2
: targetSize.height - preferredSize.height - insets.bottom;
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
if (_hfill ) {
m.setSize(maxwidth, d.height);
d.width = maxwidth;
}
else {
m.setSize(d.width, d.height);
}
if ((y + d.height) <= maxheight) {
if (y > 0) {
y += _vgap;
}
int x = (_halign == LEFT) ? insets.left
: (_halign == CENTER) ? (targetSize.width - d.width) / 2
: targetSize.width - d.width - insets.right;
m.setLocation(x + _hgap, y + _vgap);
y += d.getHeight();
}
else {
break;
}
}
}
}
}
/**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target target to get layout size for
* @param preferred should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(final Container target, final boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null) {
dim.width -= (hgap + 1);
}
return dim;
}
}
@Override
public Dimension preferredLayoutSize(Container container) {
return container.getSize();
}
@Override
public void layoutContainer(Container parent) {
Dimension pref = preferredLayoutSize(parent);
int[] prefRow = this.prefRow;
int[] prefCol = this.prefCol;
Dimension size = parent.getSize();
double y0;
int yRemaining = size.height - pref.height;
double rowWeightTotal = 0.0;
if (yRemaining != 0 && rowWeight != null) {
for (double weight : rowWeight) {
rowWeightTotal += weight;
}
}
if (rowWeightTotal == 0.0 && yRemaining > 0) {
y0 = yRemaining / 2.0;
} else {
y0 = 0;
}
int x0 = (size.width - pref.width) / 2;
if (x0 < 0)
x0 = 0;
double y = y0;
int i = -1;
for (Component[] row : contents) {
i++;
int yRound = (int) (y + 0.5);
int x = x0;
for (int j = 0; j < row.length; j++) {
Component comp = row[j];
if (comp != null) {
row[j].setBounds(x, yRound, prefCol[j], prefRow[i]);
}
x += prefCol[j];
}
y += prefRow[i];
if (rowWeightTotal > 0 && i < rowWeight.length) {
y += yRemaining * rowWeight[i] / rowWeightTotal;
}
}
// TODO Auto-generated method stub
}
/**
* Returns the minimum or preferred dimension needed to layout the target container.
*
* @param target
* target to get layout size for
* @param preferred
* should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width;
if (targetWidth == 0) {
targetWidth = Integer.MAX_VALUE;
}
int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + hgap * 2;
int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
// Can't add the component to current row. Start a new row.
if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
}
// Add a horizontal gap for all components after the first
if (rowWidth != 0) {
rowWidth += hgap;
}
rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
}
addRow(dim, rowWidth, rowHeight);
dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2;
// When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this.
Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
int extra = 1;
if (scrollPane instanceof JScrollPane) {
JScrollPane jsp = (JScrollPane) scrollPane;
JScrollBar vsb = jsp.getVerticalScrollBar();
if (vsb != null) {
extra += Math.max(0, vsb.getWidth());
}
}
dim.width -= hgap + extra;
}
return dim;
}
}