下面列出了java.awt.Component#isVisible ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private List<List<Component>> createGrid(Container container) {
int rowIndex = 0;
List<List<Component>> grid = new ArrayList<>();
for (Component child : container.getComponents()) {
if (child.isVisible()) {
List<Component> currentRow;
if (rowIndex >= grid.size()) {
currentRow = new ArrayList<>(columns);
grid.add(currentRow);
} else {
currentRow = grid.get(rowIndex);
}
currentRow.add(child);
if (currentRow.size() == columns) {
rowIndex++;
}
}
}
return grid;
}
public void layoutContainer(final Container parent) {
final Insets insets = parent.getInsets();
int posX = insets.left;
final int posY = insets.top;
final int height = parent.getHeight() - insets.top - insets.bottom;
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
Dimension pref = comp.getPreferredSize();
if (proportionalHeight) {
int h = Math.min(pref.height, height);
int o = (height - h) / 2;
comp.setBounds(posX, posY + o, pref.width, h);
} else {
comp.setBounds(posX, posY, pref.width, height);
}
posX += hGap;
posX += pref.width;
}
}
}
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 maxHeight = 0;
int visibleCount = 0;
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
final Dimension size = comp.getPreferredSize();
maxHeight = Math.max(maxHeight, size.height);
d.width += size.width;
visibleCount++;
}
}
d.width += (visibleCount - 1) * hGap;
d.height += maxHeight;
return d;
}
public final void handle(Component component) {
if (component == null)
throw new NullPointerException("component cannot be null"); // NOI18N
if (listener != null && component != null)
component.removeHierarchyListener(listener);
this.component = component;
wasVisible = component.isVisible();
if (listener == null) listener = createListener();
component.addHierarchyListener(listener);
}
private static MenuElement nextEnabledChild(MenuElement e[],
int fromIndex, int toIndex) {
for (int i=fromIndex; i<=toIndex; i++) {
if (e[i] != null) {
Component comp = e[i].getComponent();
if ( comp != null
&& (comp.isEnabled() || UIManager.getBoolean("MenuItem.disabledAreNavigable"))
&& comp.isVisible()) {
return e[i];
}
}
}
return null;
}
/**
* Returns the first visible component, aka the component that is currently displayed. Can be used to find the
* currently displayed card in a {@link java.awt.CardLayout}.
*
* @param parent
* the container which contains all the cards
* @return the component or {@code null} if no component of the parent is visible
* @since 9.0.0
*/
public static Component findDisplayedComponent(Container parent) {
for (Component comp : parent.getComponents()) {
if (comp.isVisible()) {
return comp;
}
}
return null;
}
private static MenuElement nextEnabledChild(MenuElement e[],
int fromIndex, int toIndex) {
for (int i=fromIndex; i<=toIndex; i++) {
if (e[i] != null) {
Component comp = e[i].getComponent();
if ( comp != null
&& (comp.isEnabled() || UIManager.getBoolean("MenuItem.disabledAreNavigable"))
&& comp.isVisible()) {
return e[i];
}
}
}
return null;
}
private Dimension sumVertical(final Iterable<Component> components, final DimensionGetter getter) {
final Dimension dim = new Dimension();
for (final Component c : components) {
if (c.isVisible()) {
final Dimension d = getter.get(c);
dim.width = Math.max(d.width, dim.width);
dim.height += d.height + getVgap();
}
}
return dim;
}
private boolean isGlue(Component c) {
if (c.isVisible() && c instanceof Box.Filler) {
Box.Filler f = (Box.Filler)c;
Dimension min = f.getMinimumSize();
Dimension pref = f.getPreferredSize();
return min.width == 0 && min.height == 0 &&
pref.width == 0 && pref.height == 0;
}
return false;
}
private int getMinimumHeight() {
int h = 0;
for( Component c : getComponents() ) {
if( !c.isVisible() )
continue;
Dimension d = c.getMinimumSize();
if( d.height > h )
h = d.height;
}
return h;
}
private boolean isGlue(Component c) {
if (c.isVisible() && c instanceof Box.Filler) {
Box.Filler f = (Box.Filler)c;
Dimension min = f.getMinimumSize();
Dimension pref = f.getPreferredSize();
return min.width == 0 && min.height == 0 &&
pref.width == 0 && pref.height == 0;
}
return false;
}
private int moveComponents(Container parent, int x, int y, int width,
int height, int rowStart, int rowEnd,
boolean ltr) {
switch (align) {
case LEFT:
x += ltr ? 0 : width;
break;
case CENTER:
x += width / 2;
break;
case RIGHT:
x += ltr ? width : 0;
break;
case LEADING:
break;
case TRAILING:
x += width;
break;
}
for (int i = rowStart; i < rowEnd; i++) {
Component c = parent.getComponent(i);
if (c.isVisible()) {
int cy;
cy = y + (height - c.getHeight()) / 2;
if (ltr) {
c.setLocation(x, cy);
} else {
c.setLocation(parent.getWidth() - x - c.getWidth(), cy);
}
x += c.getWidth() + hgap;
}
}
return height;
}
public static void main(String[] args) throws InterruptedException, java.lang.reflect.InvocationTargetException
{
final Frame frame = new Frame("the test");
frame.setLayout(new FlowLayout());
final Button btn1 = new Button("button 1");
frame.add(btn1);
frame.add(new Button("button 2"));
frame.add(new Button("button 3"));
frame.pack();
frame.setVisible(true);
Robot r = Util.createRobot();
Util.waitForIdle(r);
Util.clickOnComp(btn1, r);
Util.waitForIdle(r);
KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
if (kfm.getFocusOwner() != btn1) {
throw new RuntimeException("test error: can not set focus on " + btn1 + ".");
}
EventQueue.invokeAndWait(new Runnable() {
public void run() {
final int n_comps = frame.getComponentCount();
for (int i = 0; i < n_comps; ++i) {
frame.getComponent(i).setVisible(false);
}
}
});
Util.waitForIdle(r);
final Component focus_owner = kfm.getFocusOwner();
if (focus_owner != null && !focus_owner.isVisible()) {
throw new RuntimeException("we have invisible focus owner");
}
System.out.println("test passed");
frame.dispose();
}
private Dimension sumHorizontal(final Iterable<Component> components, final DimensionGetter getter) {
final Dimension dim = new Dimension();
for (final Component c : components) {
if (c.isVisible()) {
final Dimension d = getter.get(c);
dim.width += d.width + getHgap();
dim.height = Math.max(d.height, dim.height);
}
}
return dim;
}
/**
* Lay out the components, when we have sufficient space to do so.
*
* @param parent the parent container of the components
* @param realDim the dimensions of the space in use
* @param startPosition top left corner
* @param stretch the total amount to stretch the components
*/
private void layoutSufficientSpace(Container parent, Dimension realDim,
Dimension startPosition, int stretch) {
int remainingStretch = stretch;
int remainingExpandable = expandable;
// Easy - we got at least the dimensions we asked for
for (Component c : parent.getComponents()) {
// Skip hidden components
if (c.isVisible()) {
Dimension cPref = getPreferred(c);
shrink(cPref, realDim);
int xAlign = 0;
int yAlign = 0;
EnumSet<SLayout> flags = constraints.get(c);
if (flags.contains(SLayout.EXPAND_PERPENDICULAR)) {
d.setSecondary(cPref, d.getSecondary(realDim));
} else {
xAlign = getXAlignment(c, realDim);
yAlign = getYAlignment(c, realDim);
}
if ((remainingStretch > 0) && flags.contains(SLayout.EXPAND_AXIAL)) {
// Stretch the components that allow it, if needed
int add = Math.max(1, remainingStretch / remainingExpandable);
addToPrimary(cPref, add);
remainingStretch -= add;
remainingExpandable--;
}
c.setBounds(startPosition.width + xAlign, startPosition.height + yAlign, cPref.width, cPref.height);
// Move the coordinates of the next component by the size of the
// previous + padding
shiftByPrimary(startPosition, cPref);
addToPrimary(startPosition, padding);
}
}
}
/**
* 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;
}
final int hgap = getHgap();
final int vgap = getVgap();
final Insets insets = target.getInsets();
final int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
final int maxWidth = targetWidth - horizontalInsetsAndGap;
// Fit components into the allowed width
final Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0;
final int nmembers = target.getComponentCount();
for (int i = 0; i < nmembers; i++) {
final Component m = target.getComponent(i);
if (m.isVisible()) {
final 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.
final Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
if (scrollPane != null && target.isValid()) {
dim.width -= (hgap + 1);
}
return dim;
}
}
/**
* Invokes paint and update on target Component with optimal
* rectangular clip region.
* If PAINT bounding rectangle is less than
* MAX_BENEFIT_RATIO times the benefit, then the vertical and horizontal unions are
* painted separately. Otherwise the entire bounding rectangle is painted.
*
* @param target Component to <code>paint</code> or <code>update</code>
* @since 1.4
*/
public void paint(Object target, boolean shouldClearRectBeforePaint) {
Component comp = (Component)target;
if (isEmpty()) {
return;
}
if (!comp.isVisible()) {
return;
}
RepaintArea ra = this.cloneAndReset();
if (!subtract(ra.paintRects[VERTICAL], ra.paintRects[HORIZONTAL])) {
subtract(ra.paintRects[HORIZONTAL], ra.paintRects[VERTICAL]);
}
if (ra.paintRects[HORIZONTAL] != null && ra.paintRects[VERTICAL] != null) {
Rectangle paintRect = ra.paintRects[HORIZONTAL].union(ra.paintRects[VERTICAL]);
int square = paintRect.width * paintRect.height;
int benefit = square - ra.paintRects[HORIZONTAL].width
* ra.paintRects[HORIZONTAL].height - ra.paintRects[VERTICAL].width
* ra.paintRects[VERTICAL].height;
// if benefit is comparable with bounding box
if (MAX_BENEFIT_RATIO * benefit < square) {
ra.paintRects[HORIZONTAL] = paintRect;
ra.paintRects[VERTICAL] = null;
}
}
for (int i = 0; i < paintRects.length; i++) {
if (ra.paintRects[i] != null
&& !ra.paintRects[i].isEmpty())
{
// Should use separate Graphics for each paint() call,
// since paint() can change Graphics state for next call.
Graphics g = comp.getGraphics();
if (g != null) {
try {
g.setClip(ra.paintRects[i]);
if (i == UPDATE) {
updateComponent(comp, g);
} else {
if (shouldClearRectBeforePaint) {
g.clearRect( ra.paintRects[i].x,
ra.paintRects[i].y,
ra.paintRects[i].width,
ra.paintRects[i].height);
}
paintComponent(comp, g);
}
} finally {
g.dispose();
}
}
}
}
}
@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);
}
}
private Dimension calculateFlowLayout(boolean bDoChilds) {
Dimension dim = new Dimension(0, hgap);
int maxWidth;
if (getParent() != null && getParent() instanceof JViewport) {
JViewport viewport = (JViewport) getParent();
maxWidth = viewport.getExtentSize().width;
} else if (getParent() != null){
maxWidth = getParent().getWidth();
} else {
maxWidth = getWidth();
}
synchronized (getTreeLock()) {
int compCount = getComponentCount();
int maxRowWidth = 0;
int maxRowHeight = 0;
int x = 0;
for (int i = 0 ; i < compCount ; i++) {
Component m = getComponent(i);
if (m.isVisible()) {
Dimension d = m.getPreferredSize();
if (x == 0 || (x + hgap + d.width + hgap) <= maxWidth) {
// continuamos con esta linea
x += hgap;
if (bDoChilds) m.setBounds(getPosition(x, maxWidth - d.width), dim.height, d.width, d.height);
x += d.width;
if (d.height > maxRowHeight) {
maxRowHeight = d.height;
}
} else {
// nueva linea
dim.height += maxRowHeight + vgap;
if (bDoChilds) m.setBounds(getPosition(hgap, maxWidth - d.width), dim.height, d.width, d.height);
if (x > maxRowWidth) {
maxRowWidth = x;
}
x = hgap + d.width;
maxRowHeight = d.height;
}
}
}
// calculamos la ultima linea.
dim.height += maxRowHeight + vgap;
if (x > maxRowWidth) {
maxRowWidth = x;
}
dim.width = maxRowWidth;
}
return dim;
}
private static boolean focusable(Component c) {
if (c instanceof JLabel || c instanceof Box.Filler) return false;
return c.isVisible() && c.isEnabled() && c.isFocusable();
}