container.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 611 行 · 第 1/2 页

JAVA
611
字号
/* class Container
 *
 * Copyright (C) 2001-2003  R M Pitman
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package charva.awt;

import java.util.Enumeration;
import java.util.Vector;

import charva.awt.event.KeyEvent;

/**
 * Container is the abstract superclass of Window and Panel.
 */
public abstract class Container 
    extends Component
{
    public Container() {}

    /**
     * doLayout is intended to be used by subclasses of Container, such
     * as Window, JPanel and JScrollPane.
     */
    public void doLayout() {

	if (_isValid)
	    return;

	if (_layoutMgr != null) {
	    _layoutMgr.doLayout(this);

	    /* Don't set the _isValid flag if the layout manager flag
	     * is an instance of LayoutManager2; the doLayout method must
	     * be called every time because the parent window may have been
	     * resized.
	     * Instances of LayoutManager, on the other hand, are not affected
	     * be resizing of the parent window.
	     */
	    if (_layoutMgr instanceof LayoutManager2 == false)
		_isValid = true;
	}
    }

    public Dimension getSize() { return new Dimension (_size); }

    public int getHeight() { return _size.height; }

    public int getWidth() { return _size.width; }

    public void setSize(Dimension size_) { 
	_size = new Dimension(size_); 
	invalidate();
    }
    public void setSize(int width_, int height_) {
	_size.width = width_;
	_size.height = height_;
	invalidate();
    }
    public void setHeight(int height_) { 
	_size.height = height_; 
	invalidate();
    }
    public void setWidth(int width_) { 
	_size.width = width_; 
	invalidate();
    }

    public Dimension minimumSize() {
	if (_layoutMgr == null)
	    return _size;

	if (_isValid == false)
	    _minimumSize = _layoutMgr.minimumSize(this);
	return _minimumSize;
    }

    /** Returns the component at the specified index.
     */
    public Component getComponent(int n) {
	return (Component) _components.elementAt(n);
    }

    /** Returns the component that contains the specified point, or null
     * if no component contains the point. The x and y coordinates of
     * the point are relative to the origin of this container.
     */
    public Component getComponentAt(Point p) {
	return getComponentAt(p.x, p.y);
    }

    /** Returns the component that contains the specified point, or null
     * if no component contains the point. The x and y coordinates of
     * the point are relative to the origin of this container.
     */
    public Component getComponentAt(int x, int y) {
	Enumeration e = _components.elements();
	while (e.hasMoreElements()) {
	    Component c = (Component) e.nextElement();
	    if (c.contains(x, y)) {
		if (c instanceof Container) {
		    // Calculate the coordinates of the point relative
		    // to the origin of the container
		    Point origin = c.getLocation();
		    return ((Container) c).getComponentAt(x-origin.x, y-origin.y);
		}
		else
		    return c;
	    }
	}
	return null;
    }

    /** The contained component will inherit the foreground and background 
     * colors of the container if they have not been set yet.
     */
    public Component add(Component component_) {

	_addComponent(component_);
	if (_layoutMgr != null && _layoutMgr instanceof LayoutManager2) {
	    if (_layoutMgr instanceof BorderLayout) {
		((BorderLayout) _layoutMgr).addLayoutComponent(
		    component_, BorderLayout.CENTER);
	    }
	    else {
		throw new IllegalArgumentException(
			"LayoutManager2 requires a constraint object");
	    }
	}
	invalidate();
	return component_;
    }

    /**
     * Removes the specified component from this container.
     */
    public void remove(Component component_) {
	_components.remove(component_);
	component_.setParent(null);
	if (_currentFocus == component_) {
	    _currentFocus = null;
	    _currentFocus = getCurrentFocus();
	}
	invalidate();
    }

    private void _addComponent(Component component_) {

	/* Add the specified component to the list of components in this
	 * container.
	 */
	_components.addElement(component_);

	/* Set this container as the parent of the component.
	 */
	component_.setParent(this);
    }

    /**
     * Adds the specified component to the end of this container. Also
     * notifies the layout manager to add this component to the layout
     * using the specified constraint.
     * If the layout manager does not implement the LayoutManager2 interface,
     * i.e. does not know about layout constraints, we silently ignore the
     * constraint (maybe fix this to throw an exception?).
     */
    public void add(Component component_, Object constraint_) {

	_addComponent(component_);    // add to this container.

	if (_layoutMgr != null && _layoutMgr instanceof LayoutManager2) {
	    ((LayoutManager2) _layoutMgr).addLayoutComponent(
		    component_, constraint_);
	}
    }

    public void setLayout(LayoutManager mgr_) { _layoutMgr = mgr_; }

    /**
     * Returns an array of all the components in this container.
     */
    public Component[] getComponents() { 
	int arraylen = _components.size();
	Component[] array = new Component[arraylen];
	for (int i=0; i<arraylen; i++) {
	    array[i] = (Component) _components.elementAt(i);
	}
	return array;
    }

    /** Returns the number of components in this Container.
     */
    public int getComponentCount() {
	return _components.size();
    }

    /**
     * Draw all the components in this container.
     */
    public void draw() {

	if ( !isVisible())
	    return;
	
	Enumeration e = _components.elements();
	while (e.hasMoreElements()) {
	    Component c = (Component) e.nextElement();
	    if (c.isVisible())
		c.draw();
	}
    }

    /**
     * Sets the foreground color of this container and all its
     * contained components that do not yet have their foreground 
     * color set.  Overrides the same method in the Component class.
     */
    public void setForeground(Color color_)
    {
	super.setForeground(color_);

	Enumeration e = _components.elements();
	while (e.hasMoreElements()) {
	    Component c = (Component) e.nextElement();
	    if (c.getForeground() == null)
	    	c.setForeground(color_);
	}
    }

    /**
     * Sets the background color of this container and all its
     * contained components that do not yet have their background
     * color set.  Overrides the same method in the Component class.
     */
    public void setBackground(Color color_)
    {
	super.setBackground(color_);

	Enumeration e = _components.elements();
	while (e.hasMoreElements()) {
	    Component c = (Component) e.nextElement();
	    if (c.getBackground() == null)
		c.setBackground(color_);
	}
    }

    public void processKeyEvent(KeyEvent ke_) {

	/** Invoke all the KeyListener callbacks that may have been registered
	 * for this Container. 
	 */
	super.processKeyEvent(ke_);
	if (ke_.isConsumed())
	    return;

	/* Propagate the KeyEvent down to the current focus component
	 * inside this container.
	 */
	if (_currentFocus != null) {
	    _currentFocus.processKeyEvent(ke_);
	}
    }

    public void requestFocus() {
	getCurrentFocus().requestFocus();
    }

    /**
     * Return a reference to the (non-container) component inside this 
     * Container that has the keyboard input focus (or would have it, 
     * if the focus was inside this container). If no component inside 
     * the container has the focus, choose the first FocusTraversable 
     * component.
     * @return the Component in this container that would have the focus;
     * never null.
     * @exception IllegalComponentStateException if there is no
     * focus-traversable component in this container.
     */
    public Component getCurrentFocus() {

	if (_currentFocus == null) {
	    /* _currentFocus is not yet set. Try to set it to the first
	     * FocusTraversable component contained in this container.
	     */
	    Enumeration e = _components.elements();
	    while (e.hasMoreElements()) {
		Component c = (Component) e.nextElement();
		if (c.isFocusTraversable()) {
		    _currentFocus = c;
		    break;
		}
	    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?