当窗口失去焦点时隐藏JDialog窗口 [英] Hide JDialog window when the window lost focus

查看:109
本文介绍了当窗口失去焦点时隐藏JDialog窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序中只有一个JDialog框.如果它失去焦点,我想使其不可见. 我尝试了不同的方法,但是无法触发任何窗口焦点事件.这是我的代码:

Hi I have only one JDialog box in my Java application.I want to make it invisible if it lost the focus. I've tried different method, But didn't able to trigger any of the window focus events. Here is my code:

  public void windowGainedFocus(WindowEvent e) {
    System.out.println("gained focus");
  }

  public void windowLostFocus(WindowEvent e) {
    System.out.println("lost focus");
  }

推荐答案

响应Focus事件可能非常棘手.我的经验是,几乎每当有人尝试着眼于做非标准的事情时,他们最终都会后悔.尤其重要的是,它并不是真的具有可移植性-许多基于X-Windows的显示器都使用焦点跟随鼠标,这可能会导致焦点在您不期望的时候移开,从而导致早期取消对话框.

Responding to Focus events can be really tricky. My experience has been that pretty much any time someone has attempted to do non-standard things with focus, they come to regret it eventually. Not least among the issues is that it's not really all that portable - a lot of X-Windows based displays use focus-follows-mouse, which can result in the focus being transferred away when you're not expecting it to, resulting in early dismissal of your dialog.

也就是说,Sun的官方教程在这里: http: //java.sun.com/docs/books/tutorial/uiswing/misc/focus.html .如果我没记错的话,可以将PropertyChangeListener附加到KeyboardFocusManager,并且会因焦点更改而被触发:

That said, Sun's official tutorial is here: http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html . If I remember right, you can attach a PropertyChangeListener to the KeyboardFocusManager, and that will be triggered for focus changes: http://java.sun.com/javase/6/docs/api/java/awt/KeyboardFocusManager.html#addPropertyChangeListener%28java.beans.PropertyChangeListener%29

这篇关于当窗口失去焦点时隐藏JDialog窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆