下面列出了java.awt.Container#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
/**
* Lays out the specified container.
*
* @param parent the container to be laid out
* @throws IllegalStateException if any of the components added to
* this layout are not in both a horizontal and vertical group
*/
public void layoutContainer(Container parent) {
// Step 1: Prepare for layout.
prepare(SPECIFIC_SIZE);
Insets insets = parent.getInsets();
int width = parent.getWidth() - insets.left - insets.right;
int height = parent.getHeight() - insets.top - insets.bottom;
boolean ltr = isLeftToRight();
if (getAutoCreateGaps() || getAutoCreateContainerGaps() ||
hasPreferredPaddingSprings) {
// Step 2: Calculate autopadding springs
calculateAutopadding(horizontalGroup, HORIZONTAL, SPECIFIC_SIZE, 0,
width);
calculateAutopadding(verticalGroup, VERTICAL, SPECIFIC_SIZE, 0,
height);
}
// Step 3: set the size of the groups.
horizontalGroup.setSize(HORIZONTAL, 0, width);
verticalGroup.setSize(VERTICAL, 0, height);
// Step 4: apply the size to the components.
for (ComponentInfo info : componentInfos.values()) {
info.setBounds(insets, width, ltr);
}
}
UI(Strobe strobe){
this.strobe=strobe;
setBounds(400,50,100,100);
Container c=this.getContentPane();
int
xfat=100-c.getHeight(),
yfat=100-c.getWidth();
//1080i is 1920 horizontal and 540 vertical pixels
setBounds(400,50,1280+xfat,720+yfat);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
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 + o, posY, w, pref.height);
} else {
comp.setBounds(posX, posY, width, pref.height);
}
pref.height += vGap;
posY += pref.height;
}
}
}
public void layoutContainer(Container container) {
Dimension tabSize = tabDisplayer.getPreferredSize();
Insets ins = container.getInsets();
int w = container.getWidth() - (ins.left + ins.right);
tabDisplayer.setBounds (ins.left, ins.top,
w,
tabSize.height);
if( tabDisplayer.getModel().size() > 1 ) {
//#214427 - check the preferred size again, during the first pass
//the tab displayer may not know the available width
Dimension newTabSize = tabDisplayer.getPreferredSize();
if( newTabSize.height != tabSize.height ) {
tabSize = newTabSize;
tabDisplayer.setBounds (ins.left, ins.top,
w,
tabSize.height);
}
}
contentDisplayer.setBounds(ins.left,
ins.top + tabSize.height, w,
container.getHeight() - (ins.top + ins.bottom + tabSize.height));
}
public void layoutContainer(Container parent) {
int height = border;
int width = parent.getWidth() - 2 * border;
Component[] components = parent.getComponents();
int prefsize = 2 * border;
for (int i = 0; i < components.length; i++) {
prefsize += components[i].getPreferredSize().height;
}
double exp = 0;
if (homogenous) {
exp = (double) parent.getHeight() / components.length;
}
for (int i = 0; i < components.length; i++) {
if (!homogenous) {
exp = (double) parent.getHeight() / prefsize
* components[i].getPreferredSize().height;
}
if ((components[i] instanceof Container)
&& ((((Container) components[i]).getLayout() instanceof VerticalLayout))
|| (((Container) components[i]).getLayout() instanceof HorizontalLayout)) {
components[i].setSize(width, (int) exp);
}
else {
if (components[i] instanceof JSeparator) {
components[i].setSize(width, 2);
}
else {
components[i].setSize(components[i].getPreferredSize());
}
}
double ha = components[i].getAlignmentX();
double va = components[i].getAlignmentY();
int left = (int) ((width - components[i].getWidth()) * ha) + border;
components[i].setLocation(left, height + (int) ((exp - components[i].getHeight()) * va));
height += exp;
}
}
@Override
public void layoutContainer(Container parent) {
synchronized (parent.getTreeLock()) {
int startX = GAP;
// lay out the checkbox
Dimension cbSize = checkBox.getPreferredSize();
checkBox.setBounds(startX, (height - cbSize.height) / 2, cbSize.width, cbSize.height);
startX += (cbSize.width + GAP - LayerButton.BORDER_WIDTH);
// lay out the layer icon
int labelStartY = GAP - LayerButton.BORDER_WIDTH;
if (isImageLayer) {
layerLabel.setBounds(startX, labelStartY, labelSize, labelSize);
startX += labelSize;
} else {
layerLabel.setBounds(startX, (height - SMALL_THUMB_SIZE) / 2, SMALL_THUMB_SIZE, SMALL_THUMB_SIZE);
startX += SMALL_THUMB_SIZE;
}
// lay out the mask
if (maskLabel != null) {
// no need to add distance between the layer and mask icons
// because there will be a visual distance due to the borders
maskLabel.setBounds(startX, labelStartY, labelSize, labelSize);
startX += (labelSize + GAP);
} else {
startX += GAP; // the distance between the layer icon and the text field
}
int editorHeight = (int) nameEditor.getPreferredSize().getHeight();
int remainingWidth = parent.getWidth() - startX;
int adjustment = 2; // the textfield in Nimbus has two invisible pixels around it
nameEditor.setBounds(startX - adjustment, (height - editorHeight) / 2, remainingWidth - 3, editorHeight);
}
}
@Override
public void layoutContainer(Container parent) {
JComponent[] components = getComponents(parent);
if (components.length == 0) {
return;
}
Border border = ((JComponent) parent).getBorder();
Insets i = border == null ? new Insets(0, 0, 0, 0) : border
.getBorderInsets(parent);
int x = i.left;
int y = i.top;
int width = parent.getWidth() - i.left - i.right;
int height = parent.getHeight() - i.top - i.bottom;
int separators = components.length - 1;
int requestedSize = horizontal ? separators * separatorSize.width
: separators * separatorSize.height;
LinkedHashMap<JComponent, Dimension> preferredSize = new LinkedHashMap<>(
components.length);
for (JComponent c : components) {
Dimension d = c.getPreferredSize();
preferredSize.put(c, d);
requestedSize += horizontal ? d.width : d.height;
}
int max = horizontal ? width : height;
Plan plan;
if (requestedSize <= max) {
plan = layoutContainerWithExtra((JComponent) parent, preferredSize,
requestedSize, x, y, width, height);
} else {
plan = layoutContainerWithConstrainedSize((JComponent) parent,
preferredSize, x, y, width, height);
}
plan.install();
}
@Override
public boolean getScrollableTracksViewportWidth() {
Container parent = SwingUtilities.getUnwrappedParent(this);
if (parent instanceof JViewport) {
return parent.getWidth() > getPreferredSize().width;
}
return false;
}
@Override
public Dimension minimumLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
Insets insets = target.getInsets();
int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
int width = 0;
int height = 0;
int component = 0;
for (int i = 0; i < nmembers; i++, component++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
if (component > 0)
{
if (width + d.width > maxwidth)
{
dim.width = Math.max(dim.width, width);
dim.height += height + getVgap();
width = 0;
height = 0;
component = 0;
}
width += getHgap();
}
height = Math.max(height, d.height);
width += d.width;
}
}
dim.width = Math.max(dim.width, width);
dim.height += height;
dim.width += insets.left + insets.right + getHgap() * 2;
dim.height += insets.top + insets.bottom + getVgap() * 2;
return dim;
}
}
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;
}
@Override
public Dimension preferredLayoutSize(Container target)
{
synchronized (target.getTreeLock())
{
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
Insets insets = target.getInsets();
// Provide a default if the panel has not been displayed yet (i.e. in a dialog)
int targetWidth = target.getWidth() == 0 ? 400 : target.getWidth();
int maxwidth = targetWidth - (insets.left + insets.right + getHgap() * 2);
int width = 0;
int height = 0;
int component = 0;
for (int i = 0; i < nmembers; i++, component++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getPreferredSize();
if (component > 0)
{
if (width + d.width > maxwidth)
{
dim.width = Math.max(dim.width, width);
dim.height += height + getVgap();
width = 0;
height = 0;
component = 0;
}
width += getHgap();
}
height = Math.max(height, d.height);
width += d.width;
}
}
dim.width = Math.max(dim.width, width);
dim.height += height;
dim.width += insets.left + insets.right + getHgap() * 2;
dim.height += insets.top + insets.bottom + getVgap() * 2;
return dim;
}
}
public void layoutContainer(Container parent) {
int ncomponents = parent.getComponentCount();
if (ncomponents == 0) {
return;
}
int w = 0;
int h = 0;
for (int i = 0; i < ncomponents; i++) {
Component comp = parent.getComponent(i);
Dimension d = PREF_SIZE.getSize(comp, maxButtonWidth);
if (w < d.width) {
w = d.width;
}
if (h < d.height) {
h = d.height;
}
}
int parentWidth = parent.getWidth();
// use up all the width or at least one column
int columns = Math.max(1, Math.min(ncomponents, parentWidth / w));
int rows = ncomponents / columns;
if (ncomponents % columns != 0) {
rows++;
}
int x = 1;
int currentRow = 0;
for (int i = 0; i < ncomponents; i++) {
Component c = parent.getComponent(i);
int currentWidth = w;
if (x + currentWidth > parent.getWidth()) {
x = 1;
currentRow++;
}
// add 1 pixel margin to every component
//int x = currentColumn * w + 1 + currentColumn;
int y = currentRow * h + 1 + currentRow;
c.setBounds(x, y, currentWidth, h);
x += currentWidth + 1;
}
}
/**
* 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;
}
}
}
}
}
protected Rectangle getControlButtonsRectangle( Container parent ) {
Component c = getControlButtons();
return new Rectangle( parent.getWidth()-c.getWidth()-4, 2, c.getWidth(), c.getHeight() );
}
protected Rectangle getControlButtonsRectangle( Container parent ) {
Component c = getControlButtons();
return new Rectangle( parent.getWidth()-c.getWidth()-4, 4, c.getWidth(), c.getHeight() );
}
@Override
public void layoutContainer(Container target)
{
synchronized (target.getTreeLock())
{
Insets insets = target.getInsets();
int maxwidth = target.getWidth() - (insets.left + insets.right + getHgap() * 2);
int nmembers = target.getComponentCount();
int x = 0;
int y = insets.top + getVgap();
int rowh = 0;
int start = 0;
boolean ltr = target.getComponentOrientation().isLeftToRight();
SizeRequirements[] xChildren = new SizeRequirements[nmembers];
SizeRequirements[] yChildren = new SizeRequirements[nmembers];
for (int i = 0; i < nmembers; i++)
{
Component c = target.getComponent(i);
if (!c.isVisible())
{
xChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentX());
yChildren[i] = new SizeRequirements(0, 0, 0, c.getAlignmentY());
}
else
{
Dimension min = c.getMinimumSize();
Dimension typ = c.getPreferredSize();
Dimension max = c.getMaximumSize();
xChildren[i] = new SizeRequirements(min.width, typ.width, max.width, c.getAlignmentX());
yChildren[i] = new SizeRequirements(min.height, typ.height, max.height, c.getAlignmentY());
if ((x == 0) || ((x + typ.width) <= maxwidth))
{
if (x > 0)
{
x += getHgap();
}
x += typ.width;
rowh = Math.max(rowh, typ.height);
}
else
{
layoutComponents(target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren,
start, i, ltr);
x = typ.width;
y += getVgap() + rowh;
rowh = typ.height;
start = i;
}
}
}
layoutComponents(target, insets.left + getHgap(), y, maxwidth, rowh, xChildren, yChildren, start,
nmembers, ltr);
}
}
protected Rectangle getControlButtonsRectangle(Container parent) {
Component c = getControlButtons();
return new Rectangle(parent.getWidth() - c.getWidth() - ICON_X_PAD,
(parent.getHeight() - c.getHeight()) / 2, c.getWidth(), c.getHeight());
}