component.java

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

JAVA
659
字号
	    if ((nextancestor = ancestor.getParent()) == null)
		return null;
	}
	return (Window) ancestor;
    }

    /** This method should be invoked by all subclasses of Component
     * which override this method; because this method generates the
     * FOCUS_GAINED event when the component gains the keyboard focus.
     */
    public void requestFocus() {

	/* Generate the FOCUS_GAINED only if the component does not 
	 * already have the focus.
	 */
	Window ancestor = getAncestorWindow();
	Component currentFocus = ancestor.getCurrentFocus();
	if ( currentFocus != this) {
	    EventQueue evtQueue =
		    Toolkit.getDefaultToolkit().getSystemEventQueue();
	    FocusEvent evt = new FocusEvent(AWTEvent.FOCUS_LOST, currentFocus);
	    evtQueue.postEvent(evt);

	    evt = new FocusEvent(AWTEvent.FOCUS_GAINED, this);
	    evtQueue.postEvent(evt);

//	    if (getParent() != null)
		getParent().setFocus(this);

//	    requestSync();
	    repaint();
	}
    }

    /**
     * Returns true if this Component has the keyboard input focus.
     */
    public boolean hasFocus()
    {
	// Modified 19-Feb-02 by rgittens to handle null ancestor.
	Window ancestor = getAncestorWindow();
	if (ancestor == null)
	    return false;

	return (ancestor.getCurrentFocus() == this);
    }

    /**
     * Indicates whether this component can be traversed using Tab or 
     * Shift-Tab keyboard focus traversal. If this
     * method returns "false" it can still request focus using requestFocus(),
     * but it will not automatically be assigned focus during keyboard focus
     * traversal.
     */
    public boolean isFocusTraversable()
    {
	return (_enabled && _visible);
    }

    /**
     * Return true if this component is totally obscured by one or more
     * windows that are stacked above it.
     */
    public boolean isTotallyObscured() {
	Rectangle bounds = getBounds();
	Window ancestor = getAncestorWindow();

	Vector windowList = Toolkit.getDefaultToolkit().getWindowList();
	boolean obscured = false;
	synchronized (windowList) {

	    /* Ignore windows that are stacked below this component's 
	     * ancestor.
	     */
	    int i;
	    for (i=0; i<windowList.size(); i++) {
		Window w = (Window) windowList.elementAt(i);

		if (w == ancestor)
		    break;
	    }
	    i++;

	    /* Return true if any of the overlying windows totally obscures
	     * this component.
	     */
	    for ( ; i<windowList.size(); i++) {
		Window w = (Window) windowList.elementAt(i);
		Rectangle windowRect = w.getBounds();
		if (bounds.equals(windowRect.intersection(bounds))) {
		    obscured = true;
		    break;
		}
	    }
	}
	return obscured;
    }

    /** Returns the alignment along the X axis.  This indicates how the
     * component would like to be aligned relative to ther components.
     * 0 indicates left-aligned, 0.5 indicates centered and 1 indicates
     * right-aligned.
     */
    public float getAlignmentX() { return _alignmentX; }

    /** Returns the alignment along the Y axis.  This indicates how the
     * component would like to be aligned relative to ther components.
     * 0 indicates top-aligned, 0.5 indicates centered and 1 indicates
     * bottom-aligned.
     */
    public float getAlignmentY() { return _alignmentY; }

    /**
     * Get the foreground color of this component. If it is null,
     * the component will inherit the foreground color of its
     * parent container.
     */
    public Color getForeground() {
	return _foreground;
    }

    /**
     * Get the background color of this component. If it is null,
     * the component will inherit the background color of its
     * parent container.
     */
    public Color getBackground() {
	return _background;
    }

    /** Set the foreground color of this component.
     */
    public void setForeground(Color color_) { 
	_foreground = color_;
	validateCursesColor();
    }

    /** Set the background color of this component.
     */
    public void setBackground(Color color_) { 
	_background = color_;
	validateCursesColor();
    }

    /** Enable this component to react to user input. Components
     * are enabled by default.
     */
    public void setEnabled(boolean flag_) {
	_enabled = flag_;

	/* If this component is already displayed, generate a PaintEvent
	 * and post it onto the queue.
	 */
	this.repaint();
    }

    /**
     * Determine whether this component can react to user input.
     */
    public boolean isEnabled() { return _enabled; }

    /**
     * Marks the component and all parents above it as needing to be laid out
     * again. This method is overridden by Container.
     */
    public void invalidate() {
	Container parent = getParent();
	if (parent != null)
	    parent.invalidate();
    }

    /**
     * Ensures that this component is laid out correctly.
     * This method is primarily intended to be used on instances of 
     * Container. The default implementation does nothing; it is 
     * overridden by Container.
     */
    public void validate() { }

    /** Causes this component to be repainted as soon as possible
     * (this is done by posting a RepaintEvent onto the system queue).
     */
    public void repaint()
    {
	if (isDisplayed() == false)
	    return;

	PaintEvent evt = new PaintEvent(this, getBounds());
	EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
	queue.postEvent(evt);
    }

    /** Causes a SyncEvent to be posted onto the AWT queue, thus requesting
     * a refresh of the physical screen.
     */
    public void requestSync() {
	SyncEvent evt = new SyncEvent(this);
	EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
	queue.postEvent(evt);
    }

    /**
     * Determines whether this component has a valid layout.  A component 
     * is valid when it is correctly sized and positioned within its 
     * parent container and all its children (in the case of a Container) 
     * are also valid. The default implementation returns true; this method 
     * is overridden by Container.
     */
    public boolean isValid() { return true; }

    public abstract void debug(int level_);

    /** Sets the name of the component.
     */
    public void setName(String name_) {
	_name = name_;
    }

    /** Returns the name of the component.
     */
    public String getName() {
	return _name;
    }

    /** Compute the component's ncurses color-pair from its foreground
     * and background colors. If either color is null, it means that the
     * component has not been added to a container yet, so don't do 
     * anything (the colors will be validated when the component is added
     * to the container).
     */
    public void validateCursesColor() {
	if (_foreground == null || _background == null)
	    return;

	_cursesColor = Color.getCursesColor(_foreground, _background);
    }

    public int getCursesColor() {
	return _cursesColor;
    }

    //====================================================================
    // PRIVATE METHODS

    //====================================================================
    // INSTANCE VARIABLES

    /**
     * The coordinates of the top-left corner of the component, relative to
     * its parent container.
     */
    protected Point _origin = new Point(0,0);

    /**
     * A WeakReference to the Container (e.g Window, Panel or Dialog) 
     * that contains us. The reason that we use a WeakReference is to 
     * allow the parent to be garbage-collected when there are no more
     * strong references to it.
     */
    protected WeakReference _parent = null;

    /**
     * This flag is true if this component can react to user input.
     */
    protected boolean _enabled = true;

    /**
     * A flag that determines whether this component should be displayed
     * (if its parent is displayed).
     * This flag is set to true by default, except for Window which is
     * initially invisible.
     * @see #setVisible(boolean)
     * @see #isVisible()
     */
    protected boolean _visible = true;

    /**
     * A list of KeyListeners registered for this component.
     */
    protected Vector _keyListeners = null;

    /**
     * A list of FocusListeners registered for this component.
     */
    protected Vector _focusListeners = null;

    /**
     * the X-alignment of this component
     */
    protected float _alignmentX = LEFT_ALIGNMENT;

    /**
     * the Y-alignment of this component
     */
    protected float _alignmentY = TOP_ALIGNMENT;

    /** The name of this component.
     */
    private String _name = "";

    /** If the foreground color is null, this component inherits the
     * foreground color of its parent Container.
     */
    protected Color _foreground = null;

    /** If the background color is null, this component inherits the
     * background color of its parent Container.
     */
    protected Color _background = null;

    /**
     * The number of this component's color-pair, as computed by the
     * ncurses COLOR_PAIR macro.  This is set when the component
     * is added to a container and whenever the colors are changed after 
     * that, so we don't have to re-determine it every time we draw the 
     * component.<p>
     * A value of -1 indicates that the color-pair number needs to be
     * recomputed.
     */
    protected int _cursesColor = 0;

    public static final float TOP_ALIGNMENT = (float) 0.0;
    public static final float CENTER_ALIGNMENT = (float) 0.5;
    public static final float BOTTOM_ALIGNMENT = (float) 1.0;
    public static final float LEFT_ALIGNMENT = (float) 0.0;
    public static final float RIGHT_ALIGNMENT = (float) 1.0;

}

⌨️ 快捷键说明

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