下面列出了怎么用java.awt.event.WindowListener的API类实例代码及写法,或者点击链接到github查看源代码。
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
public boolean open () {
dd = new DialogDescriptor(panel, NbBundle.getMessage(RevisionPicker.class, "LBL_RevisionPickerDialog.title"), //NOI18N
true, new Object[] { okButton, DialogDescriptor.CANCEL_OPTION }, okButton, DialogDescriptor.DEFAULT_ALIGN, new HelpCtx("org.netbeans.modules.git.ui.repository.RevisionPickerDialog"), null); //NOI18N
Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);
updateDialogState();
browserPanel.addPropertyChangeListener(this);
Preferences prefs = GitModuleConfig.getDefault().getPreferences();
WindowListener windowListener = new DialogBoundsPreserver(prefs, this.getClass().getName());
dialog.addWindowListener(windowListener);
windowListener.windowOpened(new WindowEvent(dialog, WindowEvent.WINDOW_OPENED));
dialog.pack();
updateSliders(prefs);
dialog.setVisible(true);
persistSliders(prefs);
browserPanel.removePropertyChangeListener(this);
return dd.getValue() == okButton;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
/**
* Create a frame for SwingSet2 to reside in if brought up
* as an application.
*
* @param gc the gc
* @return the j frame
*/
public static JFrame createFrame(GraphicsConfiguration gc) {
JFrame frame = new JFrame(gc);
if (numSSs == 0) {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} else {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
numSSs--;
swingSets.remove(this);
}
};
frame.addWindowListener(l);
}
//由Jack Jiang于2012-09-22加上,防止因JToolBar的大小而影响JFrame的prefereSize使得没法再缩小
frame.setMinimumSize(new Dimension(100,100));
return frame;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
private JDialog createDialog(Component parentComponent, String title,
int style)
throws HeadlessException {
final JDialog dialog;
Window window = JOptionPane.getWindowForComponent(parentComponent);
if (window instanceof Frame) {
dialog = new JDialog((Frame)window, title, true);
} else {
dialog = new JDialog((Dialog)window, title, true);
}
if (window instanceof SwingUtilities.SharedOwnerFrame) {
WindowListener ownerShutdownListener =
SwingUtilities.getSharedOwnerFrameShutdownListener();
dialog.addWindowListener(ownerShutdownListener);
}
initDialog(dialog, style, parentComponent);
return dialog;
}
public static void addClosingListener( JFrame frame, IOnCloseListener onCloseListener ) {
WindowListener exitListener = new WindowAdapter(){
@Override
public void windowClosing( WindowEvent e ) {
if (!onCloseListener.canCloseWithoutPrompt()) {
int confirm = JOptionPane.showOptionDialog(frame, "Are you sure you want to exit?", "Exit Confirmation",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
if (confirm == JOptionPane.YES_OPTION) {
onCloseListener.onClose();
System.exit(0);
}
} else {
onCloseListener.onClose();
System.exit(0);
}
}
};
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(exitListener);
}
protected static Frame createVncClientMainWindow(BufferedImageCanvas canvas, String title, WindowListener windowListener) {
// Create AWT windows
Frame frame = new Frame(title + " - RDP");
// Use scrolling pane to support screens, which are larger than ours
scroller = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
scroller.add(canvas);
scroller.setSize(canvas.getWidth(), canvas.getHeight());
frame.add(scroller);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(windowListener);
return frame;
}
public static void main(String[] argv) {
JFrame f = new JFrame();
f.setSize(400, 300);
int size = 30;
List<Double> values = new ArrayList<Double>( size );
List<String> names = new ArrayList<String>( size );
for ( int i = 0; i < size; i++ ) {
values.add( i * 1d );
names.add( "Item " + i );
}
f.getContentPane().add(new ChartPanel( values,
names, "A title"));
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
f.addWindowListener(wndCloser);
f.setVisible(true);
}
private void exitCurrentApps() {
final Frame[] prevFrames = Frame.getFrames();
for(int i = 0, n = prevFrames.length; i<n; i++) {
if(existingFrames.contains(prevFrames[i])) {
continue; // don't mess with pre-existing frames such as the applet plugin
}
if(!(prevFrames[i] instanceof LauncherFrame)) {
WindowListener[] listeners = prevFrames[i].getWindowListeners();
for(int j = 0; j<listeners.length; j++) {
listeners[j].windowClosing(null);
}
if(prevFrames[i].isVisible()) {
prevFrames[i].dispose();
}
}
}
}
/**
* This Method is responsible for removing the old MouseGrabber from the AppContext, to be able to add our own
* implementation for it that is a bit more generous with closing the popup.
*/
public static void uninstallOldMouseGrabber(final ChangeListener oldMouseGrabber) {
if (oldMouseGrabber == null) return;
MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
menuSelectionManager.removeChangeListener(oldMouseGrabber);
if (oldMouseGrabber instanceof AWTEventListener) {
Toolkit tk = Toolkit.getDefaultToolkit();
java.security.AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
tk.removeAWTEventListener((AWTEventListener) oldMouseGrabber);
return null;
});
}
MenuElement[] path = menuSelectionManager.getSelectedPath();
if (path.length != 0 && path[0] != null) {
Component invoker = path[0].getComponent();
if (invoker instanceof JPopupMenu) {
invoker = ((JPopupMenu) invoker).getInvoker();
}
Window grabbedWindow = DarkUIUtil.getWindow(invoker);
if (oldMouseGrabber instanceof WindowListener) {
grabbedWindow.removeWindowListener((WindowListener) oldMouseGrabber);
}
if (oldMouseGrabber instanceof ComponentListener) {
grabbedWindow.removeComponentListener((ComponentListener) oldMouseGrabber);
}
}
}
@Override
public WindowListener createWindowListener() {
return new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent e) {
LafManager.enabledPreferenceChangeReporting(false);
}
};
}
@Override
public void startup() {
WindowListener exitListener = new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Window window = e.getWindow();
window.setVisible(false);
window.dispose();
try {
Thread.sleep(1000);
} catch (InterruptedException ignore) {
} finally {
System.exit(0);
}
}
};
addWindowListener(exitListener);
setIconImage(Icons.getIcon(Icons.OPT4J).getImage());
setTitle(title);
setLayout(new BorderLayout());
JSeparator jSeparator = new JSeparator();
JPanel toolBarPanel = new JPanel(new BorderLayout());
toolBarPanel.add(toolBar, BorderLayout.CENTER);
toolBarPanel.add(jSeparator, BorderLayout.SOUTH);
add(toolBarPanel, BorderLayout.NORTH);
add(contentPanel, BorderLayout.CENTER);
setJMenuBar(menu);
menu.startup();
toolBar.startup();
contentPanel.startup();
pack();
setVisible(true);
}
/**
* Processes window events occurring on this window by
* dispatching them to any registered WindowListener objects.
* NOTE: This method will not be called unless window events
* are enabled for this component; this happens when one of the
* following occurs:
* <ul>
* <li>A WindowListener object is registered via
* {@code addWindowListener}
* <li>Window 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 event
* @see Component#enableEvents
*/
protected void processWindowEvent(WindowEvent e) {
WindowListener listener = windowListener;
if (listener != null) {
switch(e.getID()) {
case WindowEvent.WINDOW_OPENED:
listener.windowOpened(e);
break;
case WindowEvent.WINDOW_CLOSING:
listener.windowClosing(e);
break;
case WindowEvent.WINDOW_CLOSED:
listener.windowClosed(e);
break;
case WindowEvent.WINDOW_ICONIFIED:
listener.windowIconified(e);
break;
case WindowEvent.WINDOW_DEICONIFIED:
listener.windowDeiconified(e);
break;
case WindowEvent.WINDOW_ACTIVATED:
listener.windowActivated(e);
break;
case WindowEvent.WINDOW_DEACTIVATED:
listener.windowDeactivated(e);
break;
default:
break;
}
}
}
/**
* Maps {@code Window.addWindowListener(WindowListener)} through queue
*/
public void addWindowListener(final WindowListener windowListener) {
runMapping(new MapVoidAction("addWindowListener") {
@Override
public void map() {
((Window) getSource()).addWindowListener(windowListener);
}
});
}
/**
* Maps {@code Window.removeWindowListener(WindowListener)} through queue
*/
public void removeWindowListener(final WindowListener windowListener) {
runMapping(new MapVoidAction("removeWindowListener") {
@Override
public void map() {
((Window) getSource()).removeWindowListener(windowListener);
}
});
}
private void waitForEvent(LWThread thr, Stack stack) {
k.rt.Channel<Object> vargs = c_vargs.get(stack);
FutureCounter counter = new FutureCounter(vargs.size());
for (Object arg : vargs) {
if (!(arg instanceof List)) {
throw new ExecutionException("Each argument must be a list");
}
List<?> list = (List<?>) arg;
if (list.size() != 3) {
throw new ExecutionException("Each argument must be a list containing 3 items");
}
try {
String ret = (String) list.get(0);
String type = (String) list.get(1);
Object source = list.get(2);
if ("java.awt.events.ActionEvent".equals(type)) {
addListener(source, "addActionListener", ActionListener.class, counter);
}
else if ("java.awt.events.WindowEvent".equals(type)) {
addListener(source, "addWindowListener", WindowListener.class, counter);
}
else {
throw new ExecutionException("Unknown event type: " + type);
}
}
catch (Exception e) {
throw new ExecutionException("Exception caught while adding listener", e);
}
}
throw new ConditionalYield(2, 3, counter);
}