下面列出了java.awt.Component#getPreferredSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void layoutContainer(final Container parent) {
final Insets insets = parent.getInsets();
final int posX = insets.left;
int posY = insets.top;
final int width = parent.getWidth() - insets.left - insets.right;
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
Dimension pref = comp.getPreferredSize();
if (proportionalWidth) {
int w = Math.min(pref.width, width);
int o = (width - w) / 2;
comp.setBounds(posX, posY + o, w, pref.height);
} else {
comp.setBounds(posX, posY, width, pref.height);
}
pref.height += vGap;
posY += pref.height;
}
}
}
public Dimension preferredLayoutSize(final Container parent) {
final Insets insets = parent.getInsets();
final Dimension d = new Dimension(insets.left + insets.right,
insets.top + insets.bottom);
int maxWidth = 0;
int visibleCount = 0;
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
final Dimension size = comp.getPreferredSize();
maxWidth = Math.max(maxWidth, size.width);
d.height += size.height;
visibleCount++;
}
}
d.height += (visibleCount - 1) * vGap;
d.width += maxWidth;
return d;
}
public Dimension minimumLayoutSize(final Container parent) {
final Insets insets = parent.getInsets();
final Dimension d = new Dimension(insets.left + insets.right,
insets.top + insets.bottom);
int maxWidth = 0;
int visibleCount = 0;
for (Component comp : parent.getComponents()) {
if (comp.isVisible() && !(comp instanceof Box.Filler)) {
final Dimension size = comp.getPreferredSize();
maxWidth = Math.max(maxWidth, size.width);
d.height += size.height;
visibleCount++;
}
}
d.height += (visibleCount - 1) * vGap;
d.width += maxWidth;
return d;
}
/**
* Returns true if the given point is outside the preferredSize of the
* item at the given row of the table. (Column must be 0).
* Does not check the "Table.isFileList" property. That should be checked
* before calling this method.
* This is used to make Windows {@literal L&F} JFileChooser act
* like native dialogs.
*/
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
return true;
}
TableCellRenderer tcr = table.getCellRenderer(row, column);
Object value = table.getValueAt(row, column);
Component cell = tcr.getTableCellRendererComponent(table, value, false,
false, row, column);
Dimension itemSize = cell.getPreferredSize();
Rectangle cellBounds = table.getCellRect(row, column, false);
cellBounds.width = itemSize.width;
cellBounds.height = itemSize.height;
// See if coords are inside
// ASSUME: mouse x,y will never be < cell's x,y
assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
return p.x > cellBounds.x + cellBounds.width ||
p.y > cellBounds.y + cellBounds.height;
}
/**
* Returns the preferred size.
*
* @param parent the parent.
*
* @return the preferred size.
*/
public Dimension preferredLayoutSize(final Container parent) {
synchronized (parent.getTreeLock()) {
final Insets insets = parent.getInsets();
if (parent.getComponentCount() > 0) {
final Component component = parent.getComponent(0);
final Dimension d = component.getPreferredSize();
return new Dimension(
(int) d.getWidth() + insets.left + insets.right,
(int) d.getHeight() + insets.top + insets.bottom
);
}
else {
return new Dimension(
insets.left + insets.right, insets.top + insets.bottom
);
}
}
}
public Dimension minimumLayoutSize(Container c) {
Insets insets = c.getInsets();
int height = yInset + insets.top;
int width = 0 + insets.left + insets.right;
Component[] children = c.getComponents();
Dimension compSize = null;
for (Component child : children) {
compSize = child.getPreferredSize();
height += compSize.height + yGap;
width = Math.max(width, compSize.width + insets.left + insets.right + xInset
* 2);
}
height += insets.bottom;
return new Dimension(width, height);
}
private void layoutContainerH(Container target) {
int maxHeight = 0;
Dimension size;
int height;
Insets insets = target.getInsets();
for (Component component : target.getComponents()) {
if (component.isVisible()) {
size = component.getPreferredSize();
if (fillComponents.contains(component)) {
height = target.getHeight() - insets.top - insets.bottom - topGap - bottomGap;
} else {
height = size.height;
if (height > maxHeight) {
maxHeight = height;
}
}
component.setSize(size.width, height);
}
}
moveComponentsH(target, maxHeight);
}
public void test(final Component component) {
final Dimension psize = component.getPreferredSize();
psize.width += 200;
if (Objects.equals(psize, component.getPreferredSize())) {
throw new RuntimeException("PreferredSize is wrong");
}
final Dimension msize = component.getMaximumSize();
msize.width += 200;
if (Objects.equals(msize, component.getMaximumSize())) {
throw new RuntimeException("MaximumSize is wrong");
}
final Dimension misize = component.getMinimumSize();
misize.width += 200;
if (Objects.equals(misize, component.getMinimumSize())) {
throw new RuntimeException("MinimumSize is wrong");
}
}
public Dimension preferredLayoutSize(final Container parent) {
final Insets insets = parent.getInsets();
final Dimension d = new Dimension(insets.left + insets.right,
insets.top + insets.bottom);
int maxWidth = 0;
int visibleCount = 0;
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
final Dimension size = comp.getPreferredSize();
maxWidth = Math.max(maxWidth, size.width);
d.height += size.height;
visibleCount++;
}
}
d.height += (visibleCount - 1) * vGap;
d.width += maxWidth;
return d;
}
public void layoutContainer(Container c) {
Insets insets = c.getInsets();
int height = yInset + insets.top;
Component[] children = c.getComponents();
Dimension compSize = null;
for (Component child : children) {
compSize = child.getPreferredSize();
child.setSize(compSize.width, compSize.height);
child.setLocation(xInset + insets.left, height);
height += compSize.height + yGap;
}
}
public void doLayout() {
super.doLayout();
Component c = getComponent(1);
int h = c.getPreferredSize().height;
Rectangle b = c.getBounds();
b.y = (b.height - h) / 2;
b.height = h;
c.setBounds(b);
}
/**
* Returns the preferred dimensions for this layout given the components
* in the specified target container.
* @param parent the component which needs to be laid out
* @return a <code>Dimension</code> object containing the
* preferred dimensions
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(Container parent) {
Component view = ((JViewport)parent).getView();
if (view == null) {
return new Dimension(0, 0);
}
else if (view instanceof Scrollable) {
return ((Scrollable)view).getPreferredScrollableViewportSize();
}
else {
return view.getPreferredSize();
}
}
/**
* Set the location of a component to center it on the screen.
*
* @param component
* the component to center.
*/
public static void centerOnScreen( Component component ) {
Dimension prefSize = component.getPreferredSize();
Dimension parentSize;
java.awt.Point parentLocation = new java.awt.Point(0, 0);
parentSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
component.setLocation(x, y);
}
MaximizedCellRenderer(Component comp) {
this.comp = comp;
Dimension d = comp.getPreferredSize();
if (d.getHeight() > 220) {
comp.setPreferredSize(new Dimension((int) d.getWidth(), 220));
}
}
public Dimension getSize(Container parent, boolean minimum) {
int n = parent.getComponentCount();
Insets insets = parent.getInsets();
Dimension d = new Dimension();
for (int i = 0; i < n; i++) {
Component comp = parent.getComponent(i);
if (comp instanceof EnableButton) {
continue;
}
Dimension p = (minimum
? comp.getMinimumSize()
: comp.getPreferredSize());
if (horizontal) {
d.width += p.width;
if (d.height < p.height) {
d.height = p.height;
}
} else {
if (d.width < p.width) {
d.width = p.width;
}
d.height += p.height;
}
}
d.width += (insets.left + insets.right);
d.height += (insets.top + insets.bottom);
return d;
}
/**
* 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);
}
}
MaximizedCellRenderer(Component comp) {
this.comp = comp;
Dimension d = comp.getPreferredSize();
if (d.getHeight() > 220) {
comp.setPreferredSize(new Dimension((int) d.getWidth(), 220));
}
}
private boolean updateTooltipImage(RenderedImage contents, int row, int col) {
TableCellRenderer renderer = listClasses.getCellRenderer(row, col);
Component component = listClasses.prepareRenderer(renderer, row, col);
if (!(component instanceof JComponent)) {
// sorry.
return false;
}
Rectangle cellRect = listClasses.getCellRect(row, col, false);
Dimension size = component.getPreferredSize();
Rectangle visibleRect = listClasses.getVisibleRect();
// The visible region is wide enough, hide the tooltip
if (cellRect.width >= size.width) {
hidePopup();
return false;
}
// Hide if the cell does not vertically fit
if (cellRect.y + size.height > visibleRect.y + visibleRect.height + 1) {
hidePopup();
return false;
}
BufferedImage img = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setClip(null);
g.setColor(listClasses.getBackground());
g.fillRect(0, 0, size.width, size.height);
g.translate(-cellRect.x, -cellRect.y);
// g.translate(-visibleRect.x, -visibleRect.y);
cellRect.width = size.width;
// prevent some repaing issues, see javadoc for CellRendererPane.
rendererPane.paintComponent(g, component, listClasses, cellRect);
// if table displays lines, frame the cell's display using lines.
if (listClasses.getShowHorizontalLines()) {
int rightX = size.width - 1;
g.translate(cellRect.x, cellRect.y);
g.setColor(listClasses.getForeground());
g.drawLine(0, 0, rightX, 0);
g.drawLine(rightX, 0, rightX, size.height);
g.drawLine(rightX, size.height - 1, 0, size.height - 1);
}
g.dispose();
rendererPane.remove(component);
contents.setImage(img);
return true;
}
private static void addSize(List<Object> list, Component component, int x, int y, int w, int h) {
Dimension size = component.getPreferredSize();
int width = (size.width + 1) / w - x;
int height = (size.height + 1) / h - y;
list.add(new Dimension(width, height));
}
/**
* Lays out the container.
*
* @param target
* the container to lay out.
*/
public void layoutContainer(Container target) {
Insets insets = target.getInsets();
int maxheight = target.getSize().height
- (insets.top + insets.bottom + vgap * 2);
int maxwidth = target.getSize().width
- (insets.left + insets.right + hgap * 2);
int numcomp = target.getComponentCount();
int x = insets.left + hgap, y = 0;
int colw = 0, start = 0;
for (int i = 0; i < numcomp; i++) {
Component m = target.getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
// fit last component to remaining height
if ((this.vfill) && (i == (numcomp - 1))) {
d.height = Math.max((maxheight - y),
m.getPreferredSize().height);
}
// fit component size to container width
if (this.hfill) {
m.setSize(maxwidth, d.height);
d.width = maxwidth;
} else {
m.setSize(d.width, d.height);
}
if (y + d.height > maxheight) {
placethem(target, x, insets.top + vgap, colw,
maxheight - y, start, i);
y = d.height;
x += hgap + colw;
colw = d.width;
start = i;
} else {
if (y > 0)
y += vgap;
y += d.height;
colw = Math.max(colw, d.width);
}
}
}
placethem(target, x, insets.top + vgap, colw, maxheight - y, start,
numcomp);
}