📄 component.java
字号:
final public boolean isHierarchyVisible() {
for(Component cursor = this; cursor != null ; cursor = cursor.parent)
if(!cursor.visible) return false;
return true;
}
/** Checks if this component and its parent hierarchy is enabled.
* @see #isEnabled() */
final public boolean isHierarchyEnabled() {
for(Component cursor = this; cursor != null ; cursor = cursor.parent)
if(!cursor.enabled) return false;
return true;
}
/** Checks if this component is a container.
* @see <a href="#containers">Containers</a> */
final public boolean isContainer() {
return isContainer;
}
/**
* Checks if this component accepts focus.<br>
* A component accepts focus if:
* <ul>
* <li>{@link #isHierarchyVisible()} returns true</li>
* <li>{@link #isHierarchyEnabled()} returns true</li>
* <li>{@link #isContainer()} returns false</li>
* <li>{@link #isFocusable()()} returns true</li>
* </ul>
*/
final public boolean acceptsFocus() {
if(isContainer || !focusable) return false;
for(Component cursor = this; cursor != null ; cursor = cursor.parent)
if(!cursor.visible || !cursor.enabled) return false;
return true;
}
/**
* Appends the given component as a child.<br>
* This method is the same as <code>add(component,getComponentCount())</code>.
* If {@link #isContainer()} returns false, an exception may be thrown.
* @param c the component to be appended
*/
final public void add(Component c) { this.add(c,childs.size()); }
/**
* Appends a child component at the given position.<br>
* The index must be a value greater than or equal to 0 and less than or equal
* to the current number of child components.<br>
* If {@link #isContainer()} returns false, an exception may be thrown.
* @param c the component to be appended
* @param index where to insert the component
*/
public void add(Component c,int index) {
if(!isContainer) throw new IllegalStateException(this + " is not a container");
childs.insertElementAt(c,index);
c.parent = this;
}
/**
* Gets the number of components currently added.
* @return the number of components currently added
* @since 1.2
*/
final public int getChildCount() { return childs.size(); }
/** @deprecated use {@link #getChildCount()} instead */
final public int getComponentCount() { return getChildCount(); }
/**
* Gets the component at the specified index.
* @param index The zero-based index of the component
* @return The component at the specified index
* @since 1.2
*/
final public Component getChild(int index) {
return (Component) childs.elementAt(index);
}
/** @deprecated use {@link #getChild(int)} instead */
final public Component getComponent(int index) { return getChild(index); }
/**
* Gets the index of the specified component.
* @param c The component
* @return the index of the specified index, or -1 if it couldn't be found
* @since 1.2
*/
final public int getChild(Component c) {
return childs.indexOf(c);
}
/** @deprecated use {@link #getChild(Component)} instead */
final public int getComponentIndex(Component c) { return getChild(c); }
/**
* Gets the child with the given id.
* If recursive is true, the search is performed beetween all childs.
* @param id
* @param recursive
* @return the component found or null
* @since 1.2
*/
final public Component getChild(String id, boolean recursive) {
return _getComponent(this, id);
}
final private Component _getComponent(Component c, String id) {
if(c.getId().equals(id)) return c;
for(int i=0; i<c.getChildCount() ;i++) {
Component r = _getComponent(c.getChild(i), id);
if(r != null) return r;
}
return null;
}
/**
* Removes the component at the specified index.
* @param index The index of the component to remove
* @since 1.2
*/
final public void removeChild(int index) {
((Component) childs.elementAt(index)).parent = null;
childs.removeElementAt(index);
}
/** @deprecated use {@link #removeChild(int)} instead */
public void remove(int index) { removeChild(index); }
/** Gets the parent component container. */
final public Component getParent() { return parent; }
/**
* Processes a key event.
*
* <p>The higher 32 bits of the key parameter determines the type of this event.<br>
* 1 for key pressed events.<br>
* 0 for key released events.<br>
* Otherwise is a key repeated event, in such case, the value represents how long the key code
* was repeated. See also <a href="Window.html#keys">Keys<a>.</p>
*
* <p>The default implementation of this method returns false.<br>
* An implementation should return true if the key was consumed.</p>
*
* <pre>
* protected void processKey(long key, Window window) {
* System.out.print("Keycode " + ((int) key));
* switch(key >> 32) {
* case 0: System.out.println(" released!"); break;
* case 1: System.out.println(" pressed!"); break;
* default: System.out.println(" repeated " + (key >> 32) + " times!"); break;
* }
* if(window.processInput(key) == {@link Window#FOCUSACTION_FIRE}) ...
*
* // let the window dispatch the key
* if(...) window.dispatchKey(key);
* }
* </pre>
* @param key the key
* @param window the window caller
* @return true if the key was be consumed
*/
protected boolean keyEvent(long key, Window window) { return false; }
//
// /**
// * Called when this component gains focus.
// * By default, this method has an empty implementation.
// * @since 1.2
// */
// protected void onFocus() {}
// /**
// * Called when this component loses focus.<br>
// * By default, this method has an empty implementation.
// * @since 1.2
// */
// protected void onBlur() {}
//
// /**
// * @since 1.2
// */
// final public void focus() {
// if(onFocus != null) Function.call(onFocus, this, null);
// else onFocus();
// }
// /**
// * @since 1.2
// */
// final public void blur() {
// if(onBlur != null) Function.call(onBlur, this, null);
// else onBlur();
// }
// /** @deprecated use {@link #onFocus()} */
// protected void focusGained() { onFocus(); }
//
// /** @deprecated use {@link #onBlur()} */
// protected void focusLost() { onBlur(); }
/**
* Called when this component gains focus.
* By default, this method has an empty implementation.
*/
protected void focusGained() {}
/**
* Called when this component loses focus.<br>
* By default, this method has an empty implementation.
*/
protected void focusLost() {}
/**
* Paints this component.
* <p>The default implementation calls {@link #paintChilds(Graphics, Window)}.<br>
* An implementation should call {@link #paintChilds(Graphics, Window)} once (if the
* component is a container).</p>
* <p>The graphics object was previously transformed to the current x and y, and the clip
* was setted according to this component width and height.</p>
* @param g the graphics to draw the component into
* @param window the window caller
*/
protected void paint(Graphics g, Window window) { paintChilds(g,window); }
/**
* Paints the childs components into the given graphics object.<br>
* This method should be invoked from {@link #paint(Graphics, Window)}.
*/
final protected void paintChilds(Graphics g, Window window) {
for(int i=0; i<childs.size() ;i++)
((Component) childs.elementAt(i))._paint(g,window);
}
// This above paint(Graphics,Window) method is really a handler called by this method.
// This '_paint' sets the graphics' clip and translation determined by the
// component size (w,h) and position (x,y) and calls the paint implementation.
// This method is not visible by the user, and it's invoked by Window's paint and
// Component's paintChilds.
final void _paint(Graphics g, Window w) {
if(!isHierarchyVisible()) return;
final int tx = w.rectX;
final int ty = w.rectY;
final int tw = w.rectW;
final int th = w.rectH;
int xx = g.getTranslateX() + getX();
int yy = g.getTranslateY() + getY();
final int ww = getWidth();
final int hh = getHeight();
if(xx>tx) { w.rectX=xx; w.rectW=tx+tw-xx; if(w.rectW>ww) w.rectW=ww; }
else { w.rectX=tx; w.rectW=xx+ww-tx; if(w.rectW>tw) w.rectW=tw; }
if(yy>ty) { w.rectY=yy; w.rectH=ty+th-yy; if(w.rectH>hh) w.rectH=hh; }
else { w.rectY=ty; w.rectH=yy+hh-ty; if(w.rectH>th) w.rectH=th; }
xx = getX();
yy = getY();
g.translate(xx,yy);
final int cx = g.getClipX();
final int cy = g.getClipY();
final int cw = g.getClipWidth();
final int ch = g.getClipHeight();
g.setClip(w.rectX-g.getTranslateX(),w.rectY-g.getTranslateY(),w.rectW,w.rectH);
paint(g,w);
g.setClip(cx,cy,cw,ch);
g.translate(-xx,-yy);
w.rectX = tx;
w.rectY = ty;
w.rectW = tw;
w.rectH = th;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -