component.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 659 行 · 第 1/2 页
JAVA
659 行
/* class Component
*
* Copyright (C) 2001 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.lang.ref.WeakReference;
import java.util.Enumeration;
import java.util.Vector;
import charva.awt.event.AWTEvent;
import charva.awt.event.FocusEvent;
import charva.awt.event.FocusListener;
import charva.awt.event.KeyEvent;
import charva.awt.event.KeyListener;
import charva.awt.event.MouseEvent;
import charva.awt.event.PaintEvent;
import charva.awt.event.SyncEvent;
/**
* Component is the abstract superclass of all the other CHARVA widgets.
*/
public abstract class Component
{
/** Constructor
*/
public Component() {
}
/**
* Shows or hides this component depending on the value of the
* parameter <code>visible_</code>
*/
public void setVisible(boolean visible_)
{
if (visible_)
show();
else
hide();
}
/**
* @deprecated This method has been replaced by
* <code>setVisible(boolean)</code>.
*/
public void show()
{
if ( !_visible) {
_visible = true;
repaint(); // post a PaintEvent
}
}
/**
* @deprecated This method has been replaced by
* <code>setVisible(boolean)</code>.
*/
public void hide()
{
if (_visible) {
_visible = false;
// try to move focus to next focusTraversable component
if (hasFocus()) {
getParent().nextFocus();
if (hasFocus()) {
// there was no next focusTraversable component
getParent().previousFocus();
if (hasFocus()) {
throw new IllegalComponentStateException(
"cannot hide component; it was the only " +
"focusTraversable component in this window");
}
}
}
repaint(); // post a PaintEvent
}
}
/**
* Returns true if this component is displayed when its parent
* container is displayed.
*/
public boolean isVisible() { return _visible; }
/** To be implemented by concrete subclasses.
*/
public abstract void draw();
/**
* A component is "displayed" if it is contained within a displayed Window.
* Even though it may be "displayed", it may be obscured by other
* Windows that are on top of it; and it may not be visible to the user
* because the <code>_visible</code> flag may be false.
*/
public boolean isDisplayed() {
/*
* Every component that has been added to a Container has a parent.
* The Window class overrides this method because it is never added to
* a Container.
*/
Container parent = getParent();
if (parent == null)
return false;
return parent.isDisplayed();
}
public Point getLocation() {
return new Point(_origin);
}
public void setLocation(Point origin_) {
_origin = new Point(origin_);
}
public void setLocation(int x_, int y_) {
_origin.x = x_;
_origin.y = y_;
}
/**
* Return the absolute coordinates of this component's origin.
* Note that Window (which is a subclass of Container)
* has a _parent value of null, but it overrides this method.
*/
public Point getLocationOnScreen() {
Container parent = getParent();
if (parent == null) {
throw new IllegalComponentStateException(
"cannot get component location " +
"before it has been added to a container");
}
return parent.getLocationOnScreen().addOffset(_origin);
}
public abstract Dimension getSize();
public abstract int getWidth();
public abstract int getHeight();
/**
* Get the bounding rectangle of this component, relative to
* the origin of its parent Container.
*/
public Rectangle getBounds() {
return new Rectangle(_origin, getSize());
}
/** Checks whether this component "contains" the specified point,
* where the point's x and y coordinates are defined to be relative
* to the top left corner of the parent Container.
*/
public boolean contains(Point p) {
return this.contains(p.x, p.y);
}
public boolean contains(int x, int y) {
return this.getBounds().contains(x, y);
}
public abstract Dimension minimumSize();
/**
* Set the parent container of this component. This is intended to
* be called by Container objects only.
* Note that we use a WeakReference so that the parent can be garbage-
* collected when there are no more strong references to it.
*/
public void setParent(Container container_) {
_parent = new WeakReference(container_);
// If this component's colors have not been set yet, inherit
// the parent container's colors.
if (getForeground() == null)
setForeground(container_.getForeground());
if (getBackground() == null)
setBackground(container_.getBackground());
}
/**
* Get the parent container of this component. Can return null if the
* component has no parent.
*/
protected Container getParent() {
if (_parent == null)
return null;
/* Note that _parent is a WeakReference.
*/
return (Container) _parent.get();
}
/**
* Register a KeyListener object for this component.
*/
public void addKeyListener(KeyListener kl_) {
if (_keyListeners == null)
_keyListeners = new Vector();
_keyListeners.add(kl_);
}
/**
* Register a FocusListener object for this component.
*/
public void addFocusListener(FocusListener fl_) {
if (_focusListeners == null)
_focusListeners = new Vector();
_focusListeners.add(fl_);
}
/**
* Process events that are implemented by all components.
* This can be overridden by subclasses, to handle custom events.
*/
protected void processEvent(AWTEvent evt_) {
if (evt_ instanceof KeyEvent) {
KeyEvent ke = (KeyEvent) evt_;
/* Find the ancestor Window that contains the component that
* generated the keystroke.
* Then we call the processKeyEvent method
* of the ancestor Window, which calls the same method in its
* current-focus container, and so on, until the KeyEvent
* gets down to the component that generated the keystroke.
* This allows KeyEvents to be processed by outer enclosing
* containers, then by inner containers, and finally by the
* component that generated the KeyEvent.
*/
this.getAncestorWindow().processKeyEvent(ke);
}
else if (evt_ instanceof FocusEvent)
processFocusEvent((FocusEvent) evt_);
else if (evt_ instanceof MouseEvent) {
MouseEvent e = (MouseEvent) evt_;
// if (e.getModifiers() != MouseEvent.MOUSE_PRESSED)
// return;
processMouseEvent(e);
}
}
/** Invoke all the KeyListener callbacks that may have been registered
* for this component. The KeyListener objects may modify the
* keycodes, and can also set the "consumed" flag.
*/
public void processKeyEvent(KeyEvent ke_) {
if (_keyListeners != null) {
for (Enumeration e = _keyListeners.elements();
e.hasMoreElements(); ) {
KeyListener kl = (KeyListener) e.nextElement();
if (ke_.getID() == AWTEvent.KEY_PRESSED)
kl.keyPressed(ke_);
else if (ke_.getID() == AWTEvent.KEY_TYPED)
kl.keyTyped(ke_);
if (ke_.isConsumed())
break;
}
}
}
/** Process a MouseEvent that was generated by clicking the mouse
* somewhere inside this component.
*/
public void processMouseEvent(MouseEvent e) {
// The default for a left-button-press is to request the focus;
// this is overridden by components such as buttons.
if (e.getButton() == MouseEvent.BUTTON1 &&
e.getModifiers() == MouseEvent.MOUSE_PRESSED &&
this.isFocusTraversable())
requestFocus();
}
/**
* Invoke all the FocusListener callbacks that may have been registered
* for this component.
*/
public void processFocusEvent(FocusEvent fe_) {
if (_focusListeners != null) {
for (Enumeration e = _focusListeners.elements();
e.hasMoreElements(); ) {
FocusListener fl = (FocusListener) e.nextElement();
if (fe_.getID() == AWTEvent.FOCUS_GAINED)
fl.focusGained(fe_);
else
fl.focusLost(fe_);
}
}
}
/** Get the Window that contains this component.
*/
public Window getAncestorWindow() {
Container ancestor;
Container nextancestor;
if (this instanceof Window)
return (Window) this;
for (ancestor = getParent();
(ancestor instanceof Window) == false;
ancestor = nextancestor) {
if (ancestor == null)
return null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?