下面列出了java.awt.Container#getComponents ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void enumerateCycle(Container container, List<Component> cycle) {
if (!(container.isVisible() && container.isDisplayable())) {
return;
}
cycle.add(container);
Component[] components = container.getComponents();
for (Component comp : components) {
if (comp instanceof Container) {
Container cont = (Container)comp;
if (!cont.isFocusCycleRoot() &&
!cont.isFocusTraversalPolicyProvider() &&
!((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus()))
{
enumerateCycle(cont, cycle);
continue;
}
}
cycle.add(comp);
}
}
private void enumerateCycle(Container container, List<Component> cycle) {
if (!(container.isVisible() && container.isDisplayable())) {
return;
}
cycle.add(container);
Component[] components = container.getComponents();
for (Component comp : components) {
if (comp instanceof Container) {
Container cont = (Container)comp;
if (!cont.isFocusCycleRoot() &&
!cont.isFocusTraversalPolicyProvider() &&
!((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus()))
{
enumerateCycle(cont, cycle);
continue;
}
}
cycle.add(comp);
}
}
public static void registerForMouseEvent(Component comp,
MouseListener mouseListener) {
if(comp instanceof JScrollPane) {
JScrollPane pane = (JScrollPane) comp;
comp = pane.getViewport().getView();
}
if(comp instanceof Container) {
Container container = (Container) comp;
Component[] components = container.getComponents();
for(int i = 0; i < components.length; i++) {
registerForMouseEvent(components[i], mouseListener);
}
}
//No registration for XOpenTypedata that are themselves clickable.
//No registration for JButton that are themselves clickable.
if(comp != null &&
(!(comp instanceof XOpenTypeViewer.XOpenTypeData) &&
!(comp instanceof JButton)) )
comp.addMouseListener(mouseListener);
}
/**
* Calculates the preferred size dimensions for the specified
* container, given the components it contains.
*
* @param parent the container to be laid out
* @return the preferred size computed for the parent.
* @see #minimumLayoutSize
*/
public Dimension preferredLayoutSize(final Container parent) {
synchronized (parent.getTreeLock()) {
final Insets ins = parent.getInsets();
final Component[] comps = parent.getComponents();
int height = 0;
int width = 0;
for (int i = 0; i < comps.length; i++) {
if ((comps[i].isVisible() == false) && this.ignoreInvisible) {
continue;
}
final Dimension pref = comps[i].getPreferredSize();
if (pref.height > height) {
height = pref.height;
}
if (pref.width > width) {
width = pref.width;
}
}
return new Dimension(width + ins.left + ins.right,
height + ins.top + ins.bottom);
}
}
public static void registerForMouseEvent(Component comp,
MouseListener mouseListener) {
if(comp instanceof JScrollPane) {
JScrollPane pane = (JScrollPane) comp;
comp = pane.getViewport().getView();
}
if(comp instanceof Container) {
Container container = (Container) comp;
Component[] components = container.getComponents();
for(int i = 0; i < components.length; i++) {
registerForMouseEvent(components[i], mouseListener);
}
}
//No registration for XOpenTypedata that are themselves clickable.
//No registration for JButton that are themselves clickable.
if(comp != null &&
(!(comp instanceof XOpenTypeViewer.XOpenTypeData) &&
!(comp instanceof JButton)) )
comp.addMouseListener(mouseListener);
}
private void enumerateCycle(Container container, List<Component> cycle) {
if (!(container.isVisible() && container.isDisplayable())) {
return;
}
cycle.add(container);
Component[] components = container.getComponents();
for (Component comp : components) {
if (comp instanceof Container) {
Container cont = (Container)comp;
if (!cont.isFocusCycleRoot() &&
!cont.isFocusTraversalPolicyProvider() &&
!((cont instanceof JComponent) && ((JComponent)cont).isManagingFocus()))
{
enumerateCycle(cont, cycle);
continue;
}
}
cycle.add(comp);
}
}
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 maxHeight = 0;
int visibleCount = 0;
for (Component comp : parent.getComponents()) {
if (comp.isVisible() && !(comp instanceof Box.Filler)) {
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 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 static void registerForMouseEvent(Component comp,
MouseListener mouseListener) {
if(comp instanceof JScrollPane) {
JScrollPane pane = (JScrollPane) comp;
comp = pane.getViewport().getView();
}
if(comp instanceof Container) {
Container container = (Container) comp;
Component[] components = container.getComponents();
for(int i = 0; i < components.length; i++) {
registerForMouseEvent(components[i], mouseListener);
}
}
//No registration for XOpenTypedata that are themselves clickable.
//No registration for JButton that are themselves clickable.
if(comp != null &&
(!(comp instanceof XOpenTypeViewer.XOpenTypeData) &&
!(comp instanceof JButton)) )
comp.addMouseListener(mouseListener);
}
/**
* Adds all {@code JEditorPanes} under {@code container} tagged by {@code
* FrameEditorPaneTag} to the {@code list}. It adds only top
* level {@code JEditorPanes}. For instance if there is a frame
* inside the frame it will return the top frame only.
*
* @param c the container to find all frames under
* @param list {@code List} to append the results too
*/
private static void getFrames(final Container container, List<JEditorPane> list) {
for (Component c : container.getComponents()) {
if (c instanceof FrameEditorPaneTag
&& c instanceof JEditorPane ) { //it should be always JEditorPane
list.add((JEditorPane) c);
} else {
if (c instanceof Container) {
getFrames((Container) c, list);
}
}
}
}
@Override
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
int maxX = parent.getWidth() - insets.right;
int maxY = parent.getHeight() - insets.bottom;
for (Component c : parent.getComponents()) {
int x = MathHelper.clamp(c.getX(), insets.top, maxX - c.getWidth());
int y = MathHelper.clamp(c.getY(), insets.left, maxY - c.getHeight());
c.setLocation(x, y);
}
}
static void prepareMenuBridgeItemsInContainer(Container c) {
Component[] comps = c.getComponents();
for (int i = 0; i < comps.length; i++) {
if (comps[i] instanceof JComponent) {
JComponent cop = (JComponent) comps[i];
MenuBridge bridge = (MenuBridge) cop.getClientProperty("menubridgeresizehack");
if (bridge != null) {
bridge.updateState(null);
}
}
}
}
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;
}
}
private static void setEnabled(Container container, boolean enabled) {
for (Component component : container.getComponents()) {
component.setEnabled(enabled);
if (component instanceof Container) {
setEnabled((Container) component, enabled);
}
}
}
public static List<Component> getAllComponents(final Container c) {
Component[] comps = c.getComponents();
List<Component> compList = new ArrayList<Component>();
for (Component comp : comps) {
compList.add(comp);
if (comp instanceof Container) {
compList.addAll(getAllComponents((Container) comp));
}
}
return compList;
}
private void processAddition(Component c) {
if (components.add(c)) {
register(c);
if (c instanceof Container) {
Container container = (Container) c;
container.addContainerListener(containerListener);
for (Component child : container.getComponents()) {
processAddition(child);
}
}
}
}
/**
* Adds all {@code JEditorPanes} under {@code container} tagged by {@code
* FrameEditorPaneTag} to the {@code list}. It adds only top
* level {@code JEditorPanes}. For instance if there is a frame
* inside the frame it will return the top frame only.
*
* @param c the container to find all frames under
* @param list {@code List} to append the results too
*/
private static void getFrames(final Container container, List<JEditorPane> list) {
for (Component c : container.getComponents()) {
if (c instanceof FrameEditorPaneTag
&& c instanceof JEditorPane ) { //it should be always JEditorPane
list.add((JEditorPane) c);
} else {
if (c instanceof Container) {
getFrames((Container) c, list);
}
}
}
}
private static void setEnabledRecursive(Container container, boolean enabled) {
for (Component component : container.getComponents()) {
component.setEnabled(enabled);
if (component instanceof Container) {
setEnabledRecursive((Container) component, enabled);
}
}
}
private static void setEnabledRecursive(Container container, boolean enabled) {
for (Component component : container.getComponents()) {
component.setEnabled(enabled);
if (component instanceof Container) {
setEnabledRecursive((Container) component, enabled);
}
}
}
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;
}
}