我需要实现一个例程,确保应用程序永远不会失去其他应用程序的焦点(但是当它被最小化时,我不应该强迫它返回)。所以我决定在主窗口中实现WindowFocusListener:
public class DialogoPrincipal extends JFrame implements WindowFocusListener {
public DialogoPrincipal() {
initComponents();
this.addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent e) {
//Do nothing
}
@Override
public void windowLostFocus(WindowEvent e) {
this.toFront();
}
/*hidden code*/
}
当主窗口没有显示任何子窗口时,它工作得很好。但是当打开一些子窗口时,我无法强调应用程序的焦点。 有没有办法可以强制关注应用程序,即使应用程序有子窗口,或者我需要在所有对话框中实现WindowFocusListener? 如果我需要实现这个接口,我可以为JOptionPane.showMessageDialog(...)做什么不会失去焦点?
答案 0 :(得分:3)
无法正确维护Focus, FocusRecycle, FocusSubSystem, ZOrder betweens JFrames,脏黑客必须将JFrames
添加到two_dimmensional array
,即FocusWhatever
和proper ordering
< / p>
FocusWhatever
很简单asynchronous
,因为基于来自Native OS
的属性,必须在FocusWhatever
内延迟invokeLater
,
Dialog Focus (AncestorListener) by @camickr (have to hold ancesors in Array)的逻辑对于正确的容器层次JFrame
&amp; JDialog
使用JDialog with Parent and proper ModalityType,然后focus
可以返回Parent
答案 1 :(得分:0)
我解决了它实现一个由所有子窗口继承的抽象类:
public abstract class DialogoFocado extends JDialog implements WindowFocusListener {
public DialogoFocado(Frame owner) {
super(owner);
this.addWindowFocusListener(this);
}
@Override
public void windowGainedFocus(WindowEvent e) {
}
@Override
public void windowLostFocus(WindowEvent e) {
this.toFront();
}
}