Java(仅限Windows的问题):当弹出modal dialog并单击阻止的JFrame时,alwaysOnTop JFrame会落到z-order的底部

我有一个Java应用程序,其基本UI由两个JFrame组成:客户区和工具调色板,它应始终显示在客户区上方。 为了实现这一点,工具调色板设置为alwaysOnTop(true),在所有情况下都可以很好地保存一个独有的Windows:当弹出模态JDialog时,单击客户区和/或调色板(两者都是阻止)导致调色板落在客户区后面。 关闭modal dialog后,调色板会重新出现,但其“始终在顶部”已丢失:单击客户区会遮挡调色板。

这是一个最小的单源文件演示:

// FloatingPaletteExample.java // 4/11/2012 sorghumking // // Demo of a Windows-only issue that has me puzzled: When a modal dialog is // opened, and user clicks in blocked JFrames (sequence depends on ownership of // modal dialog: follow the instructions in modal dialog to see the problem // behavior), the floating tool palette loses its "always on top-ness". import javax.swing.*; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class FloatingPaletteExample { public static void main( String [] args ) { final JFrame clientAreaJFrame = new JFrame( "client JFrame" ); clientAreaJFrame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent windowevent ) { clientAreaJFrame.dispose(); System.exit( 0 ); } }); clientAreaJFrame.setSize( 800, 600 ); clientAreaJFrame.setVisible( true ); final JFrame floatingToolFrame = new JFrame("tool JFrame"); final JCheckBox ownerCheckbox = new JCheckBox("Owned by tool frame (otherwise, null owner)"); JButton popModalButton = new JButton("Open Modal Dialog"); popModalButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JFrame owner = ownerCheckbox.isSelected() ? floatingToolFrame : null; JDialog modalDialog = new JDialog(owner, "Modal Dialog", true); final String labelText = ownerCheckbox.isSelected() ? "Click anywhere in the client JFrame." : "Click the tool JFrame, then anywhere in the client JFrame"; modalDialog.add(new JLabel(labelText)); modalDialog.pack(); modalDialog.setLocation(100, 100); modalDialog.setVisible(true); } }); floatingToolFrame.add(popModalButton, BorderLayout.NORTH); floatingToolFrame.add(ownerCheckbox, BorderLayout.SOUTH); floatingToolFrame.pack(); floatingToolFrame.setLocationRelativeTo(clientAreaJFrame); floatingToolFrame.setAlwaysOnTop(true); floatingToolFrame.setVisible(true); } } 

我尝试了floatingToolFrame.setFocusableWindowState(false),根据文档是“应用程序向AWT识别将用作浮动调色板或工具栏的窗口的标准机制”,但行为保持不变。

我找到了一个解决方法:在弹出模式对话框之前调用floatingToolFrame.setAlwaysOnTop(false),并在关闭后调用floatingToolFrame.setAlwaysOnTop(true)。 但是,任何时候打开modal dialog都需要这个包装似乎很荒谬。 是的,我可以inheritanceJDialog,并从所述子类派生我的所有对话框,以便在幕后执行此操作,但同样,为什么这是必要的?

没有解决方法的任何想法如何解决这个问题? 我创建一个永远在顶部的调色板的方法完全被误导了吗? (还有一点需要注意:这似乎与此处描述的问题相反: Java 6,JFrame卡住了alwaysontop )。

任何想法将不胜感激!

令我绝对高兴的是,这个问题似乎在Java 7 SE中得到了解决! 以上示例针对7u4 Build b20正常运行。