下面列出了怎么用java.awt.event.WindowFocusListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Processes window focus event occurring on this window by
* dispatching them to any registered WindowFocusListener objects.
* NOTE: this method will not be called unless window focus events
* are enabled for this window. This happens when one of the
* following occurs:
* <ul>
* <li>a WindowFocusListener is registered via
* {@code addWindowFocusListener}
* <li>Window focus events are enabled via {@code enableEvents}
* </ul>
* <p>Note that if the event parameter is {@code null}
* the behavior is unspecified and may result in an
* exception.
*
* @param e the window focus event
* @see Component#enableEvents
* @since 1.4
*/
protected void processWindowFocusEvent(WindowEvent e) {
WindowFocusListener listener = windowFocusListener;
if (listener != null) {
switch (e.getID()) {
case WindowEvent.WINDOW_GAINED_FOCUS:
listener.windowGainedFocus(e);
break;
case WindowEvent.WINDOW_LOST_FOCUS:
listener.windowLostFocus(e);
break;
default:
break;
}
}
}
/**
* Adds the specified window focus listener to receive window events
* from this window.
* If l is null, no exception is thrown and no action is performed.
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
* >AWT Threading Issues</a> for details on AWT's threading model.
*
* @param l the window focus listener
* @see #removeWindowFocusListener
* @see #getWindowFocusListeners
* @since 1.4
*/
public synchronized void addWindowFocusListener(WindowFocusListener l) {
if (l == null) {
return;
}
windowFocusListener = AWTEventMulticaster.add(windowFocusListener, l);
newEventsOnly = true;
}
/**
* Removes the specified window focus listener so that it no longer
* receives window events from this window.
* If l is null, no exception is thrown and no action is performed.
* <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
* >AWT Threading Issues</a> for details on AWT's threading model.
*
* @param l the window focus listener
* @see #addWindowFocusListener
* @see #getWindowFocusListeners
* @since 1.4
*/
public synchronized void removeWindowFocusListener(WindowFocusListener l) {
if (l == null) {
return;
}
windowFocusListener = AWTEventMulticaster.remove(windowFocusListener, l);
}
/**
* Returns an array of all the objects currently registered
* as <code><em>Foo</em>Listener</code>s
* upon this {@code Window}.
* <code><em>Foo</em>Listener</code>s are registered using the
* <code>add<em>Foo</em>Listener</code> method.
*
* <p>
*
* You can specify the {@code listenerType} argument
* with a class literal, such as
* <code><em>Foo</em>Listener.class</code>.
* For example, you can query a
* {@code Window w}
* for its window listeners with the following code:
*
* <pre>WindowListener[] wls = (WindowListener[])(w.getListeners(WindowListener.class));</pre>
*
* If no such listeners exist, this method returns an empty array.
*
* @param listenerType the type of listeners requested; this parameter
* should specify an interface that descends from
* {@code java.util.EventListener}
* @return an array of all objects registered as
* <code><em>Foo</em>Listener</code>s on this window,
* or an empty array if no such
* listeners have been added
* @exception ClassCastException if {@code listenerType}
* doesn't specify a class or interface that implements
* {@code java.util.EventListener}
* @exception NullPointerException if {@code listenerType} is {@code null}
*
* @see #getWindowListeners
* @since 1.3
*/
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
EventListener l = null;
if (listenerType == WindowFocusListener.class) {
l = windowFocusListener;
} else if (listenerType == WindowStateListener.class) {
l = windowStateListener;
} else if (listenerType == WindowListener.class) {
l = windowListener;
} else {
return super.getListeners(listenerType);
}
return AWTEventMulticaster.getListeners(l, listenerType);
}
/**
* Installs a {@link WindowFocusListener} on the given {@link JComponent}'s
* parent {@link Window}. If the {@code JComponent} doesn't yet have a
* parent, then the listener will be installed when the component is added
* to a container.
*
* @param component the component who's parent frame to listen to focus
* changes on.
* @param focusListener the {@code WindowFocusListener} to notify when focus
* changes.
*/
public static void installWeakWindowFocusListener(JComponent component, WindowFocusListener focusListener) {
WindowListener weakFocusListener = createWeakWindowFocusListener(focusListener);
AncestorListener ancestorListener = createAncestorListener(component, weakFocusListener);
component.addAncestorListener(ancestorListener);
}
/**
* Returns an array of all the window focus listeners
* registered on this window.
*
* @return all of this window's {@code WindowFocusListener}s
* or an empty array if no window focus
* listeners are currently registered
*
* @see #addWindowFocusListener
* @see #removeWindowFocusListener
* @since 1.4
*/
public synchronized WindowFocusListener[] getWindowFocusListeners() {
return getListeners(WindowFocusListener.class);
}