下面列出了java.awt.Container#getComponentCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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 );
}
}
}
private static List<Component> components(Container aContainer) {
List<Component> l = new ArrayList();
for (int i = 0; i < aContainer.getComponentCount(); i++) {
Component c = aContainer.getComponent(i);
if (c instanceof JPanel || c instanceof JToolBar)
l.addAll(components((Container)c));
else if (c instanceof JScrollPane)
l.addAll(components((Container)((JScrollPane)c).getViewport()));
// else if (c instanceof JRootPane)
// l.addAll(components((Container)((JRootPane)c).getContentPane()));
else if (focusable(c)) l.add(c);
}
return l;
}
/**
* If the status bar is currently displayed hide it and vice versa.
*/
public void showHideStatusBar() {
Container contentPane = frame.getContentPane();
boolean statusBarShown = false;
for (int i = 0; i < contentPane.getComponentCount(); i++) {
if (contentPane.getComponent(i).equals(jlStatusBar)) {
statusBarShown = true;
break;
}
}
if (statusBarShown) {
frame.getContentPane().remove(jlStatusBar);
applicationSettings.setShowStatusBar(false);
} else {
frame.getContentPane().add(jlStatusBar, BorderLayout.SOUTH);
applicationSettings.setShowStatusBar(true);
}
}
/**
* 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
);
}
}
}
/**
* Returns the minimum size using this layout manager.
*
* @param parent the parent.
*
* @return the minimum size using this layout manager.
*/
public Dimension minimumLayoutSize(Container parent) {
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = ncomponents / COLUMNS;
for (int c = 0; c < COLUMNS; c++) {
for (int r = 0; r < nrows; r++) {
Component component = parent.getComponent(r * COLUMNS + c);
Dimension d = component.getMinimumSize();
if (this.colWidth[c] < d.width) {
this.colWidth[c] = d.width;
}
if (this.rowHeight[r] < d.height) {
this.rowHeight[r] = d.height;
}
}
}
int totalHeight = this.vGap * (nrows - 1);
for (int r = 0; r < nrows; r++) {
totalHeight = totalHeight + this.rowHeight[r];
}
int totalWidth = this.colWidth[0] + this.labelGap
+ this.colWidth[1] + this.buttonGap + this.colWidth[2];
return new Dimension(insets.left + insets.right + totalWidth
+ this.labelGap + this.buttonGap, insets.top
+ insets.bottom + totalHeight + this.vGap);
}
}
private NbSheet findCachedSheet( Container c ) {
NbSheet res = null;
int childrenCount = c.getComponentCount();
for( int i=0; i<childrenCount && res == null; i++ ) {
Component child = c.getComponent(i);
if( child instanceof NbSheet ) {
res = (NbSheet)child;
} else if( child instanceof Container ) {
res = findCachedSheet((Container)child);
}
}
return res;
}
public void layoutContainer(Container parent) {
setParent(parent);
int n = parent.getComponentCount();
getConstraints(parent).reset();
for (int i = 0 ; i < n ; i++) {
getConstraints(parent.getComponent(i)).reset();
}
Insets insets = parent.getInsets();
Constraints pc = getConstraints(parent);
abandonCycles(pc.getX()).setValue(0);
abandonCycles(pc.getY()).setValue(0);
abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
insets.left - insets.right);
abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
insets.top - insets.bottom);
for (int i = 0 ; i < n ; i++) {
Component c = parent.getComponent(i);
Constraints cc = getConstraints(c);
int x = abandonCycles(cc.getX()).getValue();
int y = abandonCycles(cc.getY()).getValue();
int width = abandonCycles(cc.getWidth()).getValue();
int height = abandonCycles(cc.getHeight()).getValue();
c.setBounds(insets.left + x, insets.top + y, width, height);
}
}
void closeFancyBoxes(Container container) {
for (int a = 0; a < container.getComponentCount(); a++) {
Component c = container.getComponent(a);
if (c instanceof JFancyBox) {
c.setVisible(false);
} else if (c instanceof Container) {
closeFancyBoxes((Container) c);
}
}
}
/**
*
* @param usePreferred
* true for preferred size, false for minimum size
*/
private Dimension calculateSize(Container parent, boolean usePreferred) {
Dimension size = new Dimension();
for (int a = 0; a < parent.getComponentCount(); a++) {
Component comp = parent.getComponent(a);
Dimension d = usePreferred ? comp.getPreferredSize() : comp
.getMinimumSize();
size.width = Math.max(size.width, d.width);
size.height += d.height;
}
Insets insets = getInsets();
size.width += insets.left + insets.right;
size.height += insets.top + insets.bottom;
return size;
}
/**
* Description of the Method
*
*@param target Description of Parameter
*@return Description of the Returned Value
*/
public Dimension preferredLayoutSize(Container target) {
synchronized (target.getTreeLock()) {
Dimension dim = new Dimension(0, 0);
int nmembers = target.getComponentCount();
boolean firstVisibleComponent = true;
for (int ii = 0; ii < nmembers; ii++) {
Component m = target.getComponent(ii);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
dim.width = Math.max(dim.width, d.width);
if (firstVisibleComponent) {
firstVisibleComponent = false;
}
else {
dim.height += _vgap;
}
dim.height += d.height;
}
}
Insets insets = target.getInsets();
dim.width += insets.left + insets.right + _hgap * 2;
dim.height += insets.top + insets.bottom + _vgap * 2;
return dim;
}
}
public void layoutContainer(Container parent) {
setParent(parent);
int n = parent.getComponentCount();
getConstraints(parent).reset();
for (int i = 0 ; i < n ; i++) {
getConstraints(parent.getComponent(i)).reset();
}
Insets insets = parent.getInsets();
Constraints pc = getConstraints(parent);
abandonCycles(pc.getX()).setValue(0);
abandonCycles(pc.getY()).setValue(0);
abandonCycles(pc.getWidth()).setValue(parent.getWidth() -
insets.left - insets.right);
abandonCycles(pc.getHeight()).setValue(parent.getHeight() -
insets.top - insets.bottom);
for (int i = 0 ; i < n ; i++) {
Component c = parent.getComponent(i);
Constraints cc = getConstraints(c);
int x = abandonCycles(cc.getX()).getValue();
int y = abandonCycles(cc.getY()).getValue();
int width = abandonCycles(cc.getWidth()).getValue();
int height = abandonCycles(cc.getHeight()).getValue();
c.setBounds(insets.left + x, insets.top + y, width, height);
}
}
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);
}
}
private void collectSaveables(Component component, List<Saveable> saveables) {
if (component instanceof Container) {
Container container = (Container) component;
int count = container.getComponentCount();
for (int i = 0; i < count; i++) {
collectSaveables(container.getComponent(i), saveables);
}
}
if (component instanceof Saveable) {
saveables.add((Saveable) component);
}
}
private void setEditsAction(final Container con) {
if (con instanceof JSpinner)
return;
/*
* if(con instanceof JSplitPane){
* setEditsAction(((JSplitPane)con).getLeftComponent());
* setEditsAction(((JSplitPane)con).getRightComponent()); return; }
*/
for (int i = 0; i < con.getComponentCount(); i++) {
if (con.getComponent(i) instanceof Container)
setEditsAction((Container) con.getComponent(i));
final Component container = con.getComponent(i);
if (container instanceof JTextField) {
((JTextField) container).addActionListener(listener);
} else if (container instanceof JPasswordField) {
((JPasswordField) container).addActionListener(listener);
}
if (container instanceof JList
&& !(container instanceof JTableHeader)) {
((JList) container).addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1
&& e.getClickCount() > 1)
panel.ok();
}
});
}
}
}
/**
* 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 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);
}
/**
* 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 minimumLayoutSize(Container parent) {
Scale scale = Scale.get(parent);
int height = 0;
int width = (mColumns - 1) * scale.scale(mHGap);
synchronized (parent.getTreeLock()) {
Insets insets = parent.getInsets();
int compCount = parent.getComponentCount();
int[] widths = new int[mColumns];
int rows = 1 + (compCount - 1) / mColumns;
int i;
width += insets.left + insets.right;
for (int y = 0; y < rows; y++) {
int rowHeight = 0;
for (int x = 0; x < mColumns; x++) {
i = y * mColumns + x;
if (i < compCount) {
Dimension size = parent.getComponent(i).getMinimumSize();
if (size.width > widths[x]) {
widths[x] = size.width;
}
if (size.height > rowHeight) {
rowHeight = size.height;
}
}
}
height += rowHeight;
}
height += insets.top + insets.bottom;
if (rows > 0) {
height += (rows - 1) * scale.scale(mVGap);
}
for (i = 0; i < mColumns; i++) {
width += widths[i];
}
}
return new Dimension(width, height);
}
@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;
}
}