container.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 611 行 · 第 1/2 页
JAVA
611 行
}
if (_currentFocus == null) {
throw new IllegalComponentStateException(
"no focus-traversable components inside this Container");
}
if (_currentFocus instanceof Container) {
return ((Container) _currentFocus).getCurrentFocus();
}
else
return _currentFocus;
}
/**
* Set the _currentFocus to refer to the next focus-traversable component
* in the list of contained components, and put FocusEvents on the queue,
* one for the component that is losing the focus and one for the component
* gaining the focus.
*/
public void nextFocus() {
/* Put a FOCUS_LOST event on the queue for the component that is
* losing the focus.
* If the current focus is a Container, then this method will have been
* called by that container (which would already have posted a
* FOCUS_LOST event for its own contained component that was losing
* focus).
if ((_currentFocus instanceof Container) == false) {
FocusEvent evt = new FocusEvent(AWTEvent.FOCUS_LOST, _currentFocus);
EventQueue evtQueue =
Toolkit.getDefaultToolkit().getSystemEventQueue();
evtQueue.postEvent(evt);
}
*/
/* Determine which component should get focus next.
*/
int index = _components.indexOf(_currentFocus);
if (index == -1) {
throw new IllegalComponentStateException(
"focus component not found in parent");
}
Component focusCandidate;
for (;;) {
/* If the focus was owned by the last component in this container,
* the new focus should go to the next component in the parent
* container, IF THERE IS A PARENT (this container may be a
* Window, in which case the parent is null).
*/
if (++index >= _components.size()) {
if (getParent() != null) {
getParent().nextFocus();
return;
}
else {
/* Don't need to worry about infinite loops. Worst case, we
* should just end up where we started.
*/
index = 0;
}
}
focusCandidate = (Component) _components.elementAt(index);
/* If the next component will not accept the focus, continue
* trying until we get one that does.
*/
if (focusCandidate.isFocusTraversable())
break;
}
if (focusCandidate instanceof Container)
((Container) focusCandidate).firstFocus();
focusCandidate.requestFocus();
}
/**
* Set the _currentFocus to refer to the previous focus-traversable
* component in the list of contained components, and put FocusEvents on
* the queue, one for the component that is losing the focus and one for
* the component gaining the focus.
*/
public void previousFocus() {
/* Put a FOCUS_LOST event on the queue for the component that is
* losing the focus.
* If the current focus is a Container, then this method will have been
* called by that container (which would already have posted a
* FOCUS_LOST event for its own contained component that was losing
* focus).
if ((_currentFocus instanceof Container) == false) {
FocusEvent evt = new FocusEvent(AWTEvent.FOCUS_LOST, _currentFocus);
EventQueue evtQueue =
Toolkit.getDefaultToolkit().getSystemEventQueue();
evtQueue.postEvent(evt);
}
*/
/* Determine which component should get focus next.
*/
int index = _components.indexOf(_currentFocus);
if (index == -1) {
throw new IllegalArgumentException(
"focus component not found in parent");
}
Component focusCandidate;
for (;;) {
/* If the focus was owned by the first component in this container,
* the new focus should go to the previous component in the parent
* container, IF THERE IS A PARENT (this container may be a
* Window, in which case the parent is null).
*/
if (--index < 0) {
if (getParent() != null) {
getParent().previousFocus();
return;
}
else {
index = _components.size() - 1;
}
}
focusCandidate = (Component) _components.elementAt(index);
/* If the next component will not accept the focus, continue
* trying until we get one that does.
*/
if (focusCandidate.isFocusTraversable())
break;
}
if (focusCandidate instanceof Container)
((Container) focusCandidate).lastFocus();
focusCandidate.requestFocus();
}
/**
* Set this container's current keyboard focus. Called by the
* requestFocus() method of the contained component.
*/
public void setFocus(Component focus_) {
_currentFocus = focus_;
if (getParent() != null)
getParent().setFocus(this);
}
/**
* Return true if any of the components within this Container
* are focus-traversable (i.e. will accept keyboard input focus when
* TAB or SHIFT-TAB is pressed).
*/
public boolean isFocusTraversable() {
if ( !super.isFocusTraversable())
return false;
Enumeration e = _components.elements();
while (e.hasMoreElements()) {
Component c = (Component) e.nextElement();
if (c.isFocusTraversable())
return true;
}
return false;
}
public Insets getInsets() { return _insets; }
/* Default implementation of debug, gets overridden by subclasses.
*/
public void debug(int level_) {
Enumeration e = _components.elements();
while (e.hasMoreElements()) {
Component c = (Component) e.nextElement();
c.debug(level_ + 1);
}
}
/**
* Sets the keyboard focus to the first component that is focusTraversable.
* Called by the nextFocus() method when it runs out of components in
* the current container to move the focus to. The nextFocus() method
* first checks that this container contains a focusTraversable component
* before calling this.
*/
private void firstFocus() {
Enumeration e = _components.elements();
while (e.hasMoreElements()) {
Component c = (Component) e.nextElement();
if (c.isFocusTraversable()) {
if (c instanceof Container) {
((Container) c).firstFocus();
}
_currentFocus = c;
return;
}
}
}
/**
* Sets the keyboard focus to the last component that is focusTraversable.
* Called by the previousFocus() method when it runs out of components in
* the current container to move the focus to. The previousFocus() method
* first checks that this container contains a focusTraversable component
* before calling this.
*/
private void lastFocus() {
for (int i=_components.size() - 1; i >= 0; i--) {
Component c = (Component) _components.elementAt(i);
if (c.isFocusTraversable()) {
if (c instanceof Container) {
((Container) c).lastFocus();
}
_currentFocus = c;
return;
}
}
}
/**
* Validates this container and all of its contained components.
* The programmer must call validate() on a container to cause it
* to re-layout its contained components after components have
* been added, removed or resized.
*/
public void validate() {
if (_isValid)
return;
/* doLayout sets the validate flag (unless the layout manager is
* an instance of LayoutManager2).
*/
doLayout();
}
/**
* Determines whether this component is valid. A container is valid when
* it is correctly sized and positioned within its parent container and
* all its children are also valid.
*/
public boolean isValid() { return _isValid; }
/**
* Marks the container and all parents above it as needing to be laid out
* again.
*/
public void invalidate() {
_isValid = false;
super.invalidate();
}
//====================================================================
// INSTANCE VARIABLES
/**
* The list of components contained within this Container.
*/
protected Vector _components = new Vector();
/** The container's size
*/
protected Dimension _size = new Dimension(1, 1);
/**
* The layout manager that will be used to lay out the components.
*/
protected LayoutManager _layoutMgr = null;
/**
* The component (which may itself be a Container) inside this Container
* that currently has the input focus (or, if the input focus is
* currently outside this Container, the component to which focus will
* return if and when this Container regains focus).
*/
protected Component _currentFocus = null;
/**
* The insets define how much padding to insert inside the Container,
* to take into account the border frame (if any).
* For a Window they will be (1,1); for a Panel, they will be (0,0).
*/
protected Insets _insets = new Insets(0,0,0,0);
/**
* A flag that is set to true when the container is laid out, and set to
* false when a component is added or removed from the container
* (indicating that it needs to be laid out again).
*/
protected boolean _isValid = false;
/**
* Used for caching the minimum size of this container, so that we don't
* have to keep recalculating it. This dimension is valid only if _isValid
* is true.
*/
protected Dimension _minimumSize;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?