container.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,280 行 · 第 1/3 页
JAVA
1,280 行
}
/**
* Recursively validates the container tree, recomputing any invalid
* layouts.
*/
protected void validateTree() {
if (valid)
return;
ContainerPeer cPeer = null;
if (peer != null && !(peer instanceof LightweightPeer)) {
cPeer = (ContainerPeer) peer;
cPeer.beginValidate();
}
doLayout();
for (int i = 0; i < ncomponents; ++i) {
Component comp = component[i];
if (!comp.isValid()) {
if (comp instanceof Container) {
((Container) comp).validateTree();
} else {
component[i].validate();
}
}
}
/* children will call invalidate() when they are layed out. It
is therefore imporant that valid is not set to true
before after the children has been layed out. */
valid = true;
if (cPeer != null)
cPeer.endValidate();
}
public void setFont(Font f) {
super.setFont(f);
// FIXME, should invalidate all children with font == null
}
/**
* Returns the preferred size of this container.
*
* @return The preferred size of this container.
*/
public Dimension getPreferredSize() {
if (layoutMgr != null)
return layoutMgr.preferredLayoutSize(this);
else
return super.getPreferredSize();
}
/**
* Returns the preferred size of this container.
*
* @return The preferred size of this container.
*
* @deprecated use {@link #getPreferredSize()} instead
*/
public Dimension preferredSize() {
return getPreferredSize();
}
/**
* Returns the minimum size of this container.
*
* @return The minimum size of this container.
*/
public Dimension getMinimumSize() {
if (layoutMgr != null)
return layoutMgr.minimumLayoutSize(this);
else
return super.getMinimumSize();
}
/**
* Returns the minimum size of this container.
*
* @return The minimum size of this container.
*
* @deprecated use {@link #getMinimumSize()} instead
*/
public Dimension minimumSize() {
return getMinimumSize();
}
/**
* Returns the maximum size of this container.
*
* @return The maximum size of this container.
*/
public Dimension getMaximumSize() {
if (layoutMgr != null && layoutMgr instanceof LayoutManager2) {
LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
return lm2.maximumLayoutSize(this);
} else
return super.getMaximumSize();
}
/**
* Returns the preferred alignment along the X axis. This is a value
* between 0 and 1 where 0 represents alignment flush left and
* 1 means alignment flush right, and 0.5 means centered.
*
* @return The preferred alignment along the X axis.
*/
public float getAlignmentX() {
if (layoutMgr instanceof LayoutManager2) {
LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
return lm2.getLayoutAlignmentX(this);
} else
return super.getAlignmentX();
}
/**
* Returns the preferred alignment along the Y axis. This is a value
* between 0 and 1 where 0 represents alignment flush top and
* 1 means alignment flush bottom, and 0.5 means centered.
*
* @return The preferred alignment along the Y axis.
*/
public float getAlignmentY() {
if (layoutMgr instanceof LayoutManager2) {
LayoutManager2 lm2 = (LayoutManager2) layoutMgr;
return lm2.getLayoutAlignmentY(this);
} else
return super.getAlignmentY();
}
/**
* Paints this container. The implementation of this method in this
* class forwards to any lightweight components in this container. If
* this method is subclassed, this method should still be invoked as
* a superclass method so that lightweight components are properly
* drawn.
*
* @param graphics The graphics context for this paint job.
*/
public void paint(Graphics g) {
if (!isShowing())
return;
super.paint(g);
visitChildren(g, GfxPaintVisitor.INSTANCE, true);
}
/**
* Updates this container. The implementation of this method in this
* class forwards to any lightweight components in this container. If
* this method is subclassed, this method should still be invoked as
* a superclass method so that lightweight components are properly
* drawn.
*
* @param graphics The graphics context for this update.
*/
public void update(Graphics g) {
super.update(g);
}
/**
* Prints this container. The implementation of this method in this
* class forwards to any lightweight components in this container. If
* this method is subclassed, this method should still be invoked as
* a superclass method so that lightweight components are properly
* drawn.
*
* @param graphics The graphics context for this print job.
*/
public void print(Graphics g) {
super.print(g);
visitChildren(g, GfxPrintVisitor.INSTANCE, true);
}
/**
* Paints all of the components in this container.
*
* @param graphics The graphics context for this paint job.
*/
public void paintComponents(Graphics g) {
super.paint(g);
visitChildren(g, GfxPaintAllVisitor.INSTANCE, true);
}
/**
* Prints all of the components in this container.
*
* @param graphics The graphics context for this print job.
*/
public void printComponents(Graphics g) {
super.paint(g);
visitChildren(g, GfxPrintAllVisitor.INSTANCE, true);
}
/**
* Adds the specified container listener to this object's list of
* container listeners.
*
* @param listener The listener to add.
*/
public synchronized void addContainerListener(ContainerListener l) {
containerListener = AWTEventMulticaster.add(containerListener, l);
}
/**
* Removes the specified container listener from this object's list of
* container listeners.
*
* @param listener The listener to remove.
*/
public synchronized void removeContainerListener(ContainerListener l) {
containerListener = AWTEventMulticaster.remove(containerListener, l);
}
/**
* @since 1.4
*/
public synchronized ContainerListener[] getContainerListeners() {
return (ContainerListener[]) AWTEventMulticaster.getListeners(containerListener, ContainerListener.class);
}
/**
* Returns an array of all the objects currently registered as FooListeners
* upon this Container. FooListeners are registered using the addFooListener
* method.
*
* @since 1.3
*/
public EventListener[] getListeners(Class listenerType) {
if (listenerType == ContainerListener.class)
return getContainerListeners();
return super.getListeners(listenerType);
}
/**
* Processes the specified event. This method calls
* <code>processContainerEvent()</code> if this method is a
* <code>ContainerEvent</code>, otherwise it calls the superclass
* method.
*
* @param event The event to be processed.
*/
protected void processEvent(AWTEvent e) {
if (e instanceof ContainerEvent)
processContainerEvent((ContainerEvent) e);
else
super.processEvent(e);
}
/**
* Called when a container event occurs if container events are enabled.
* This method calls any registered listeners.
*
* @param event The event that occurred.
*/
protected void processContainerEvent(ContainerEvent e) {
if (containerListener == null)
return;
switch (e.id) {
case ContainerEvent.COMPONENT_ADDED :
containerListener.componentAdded(e);
break;
case ContainerEvent.COMPONENT_REMOVED :
containerListener.componentRemoved(e);
break;
}
}
/**
* AWT 1.0 event processor.
*
* @param event The event that occurred.
*
* @deprecated use {@link #dispatchEvent(AWTEvent)} instead
*/
public void deliverEvent(Event e) {
}
/**
* Returns the component located at the specified point. This is done
* by checking whether or not a child component claims to contain this
* point. The first child component that does is returned. If no
* child component claims the point, the container itself is returned,
* unless the point does not exist within this container, in which
* case <code>null</code> is returned.
*
* @param x The X coordinate of the point.
* @param y The Y coordinate of the point.
*
* @return The component containing the specified point, or
* <code>null</code> if there is no such point.
*/
public Component getComponentAt(int x, int y) {
synchronized (getTreeLock()) {
if (!contains(x, y))
return null;
for (int i = 0; i < ncomponents; ++i) {
// Ignore invisible children...
if (!component[i].isVisible())
continue;
int x2 = x - component[i].x;
int y2 = y - component[i].y;
if (component[i].contains(x2, y2))
return component[i];
}
return this;
}
}
/**
* Returns the component located at the specified point. This is done
* by checking whether or not a child component claims to contain this
* point. The first child component that does is returned. If no
* child component claims the point, the container itself is returned,
* unless the point does not exist within this container, in which
* case <code>null</code> is returned.
*
* @param point The point to return the component at.
*
* @return The component containing the specified point, or <code>null</code>
* if there is no such point.
*
* @deprecated use {@link #getComponentAt(int, int)} instead
*/
public Component locate(int x, int y) {
return getComponentAt(x, y);
}
/**
* Returns the component located at the specified point. This is done
* by checking whether or not a child component claims to contain this
* point. The first child component that does is returned. If no
* child component claims the point, the container itself is returned,
* unless the point does not exist within this container, in which
* case <code>null</code> is returned.
*
* @param point The point to return the component at.
* @return The component containing the specified point, or <code>null</code>
* if there is no such point.
*/
public Component getComponentAt(Point p) {
return getComponentAt(p.x, p.y);
}
public Component findComponentAt(int x, int y) {
synchronized (getTreeLock()) {
if (!contains(x, y))
return null;
for (int i = 0; i < ncomponents; ++i) {
// Ignore invisible children...
if (!component[i].isVisible())
continue;
int x2 = x - component[i].x;
int y2 = y - component[i].y;
// We don't do the contains() check right away because
// findComponentAt would redundantly do it first thing.
if (component[i] instanceof Container) {
Container k = (Container) component[i];
Component r = k.findComponentAt(x2, y2);
if (r != null)
return r;
} else if (component[i].contains(x2, y2))
return component[i];
}
return this;
}
}
public Component findComponentAt(Point p) {
return findComponentAt(p.x, p.y);
}
/**
* Called when this container is added to another container to inform it
* to create its peer. Peers for any child components will also be
* created.
*/
public void addNotify() {
addNotifyContainerChildren();
super.addNotify();
}
/**
* Called when this container is removed from its parent container to
* inform it to destroy its peer. This causes the peers of all child
* component to be destroyed as well.
*/
public void removeNotify() {
synchronized (getTreeLock()) {
for (int i = 0; i < ncomponents; ++i)
component[i].removeNotify();
super.removeNotify();
}
}
/**
* Tests whether or not the specified component is contained within
* this components subtree.
*
* @param component The component to test.
*
* @return <code>true</code> if this container is an ancestor of the
* specified component, <code>false</code> otherwise.
*/
public boolean isAncestorOf(Component comp) {
synchronized (getTreeLock()) {
while (true) {
if (comp == null)
return false;
if (comp == this)
return true;
comp = comp.getParent();
}
}
}
/**
* Returns a string representing the state of this container for
* debugging purposes.
*
* @return A string representing the state of this container.
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?