我有一個Java應用程序,其基本UI由兩個JFrame組成:一個客戶區和一個工具調色板應始終顯示在客戶區域上方。爲了達到這個目的,工具面板被設置爲alwaysOnTop(true),在所有情況下都可以很好地工作,除了Windows獨佔之外:當彈出模態JDialog時,單擊客戶區和/或調色板(兩者都是阻止)導致調色板落在客戶區域後面。一旦模式對話框關閉,調色板會再次出現,但其「始終處於最佳狀態」已丟失:單擊客戶區會遮擋調色板。Java(僅限Windows)問題:alwaysOnTop當彈出和阻止模式對話框時,JFrame落在z順序的底部
這裏的一個最小的,單個源文件示範:
// 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(假),根據the doc這就是「標準機制,應用程序,以確定到一個AWT窗口,這將用作浮動調色板或工具欄「,但行爲保持不變。
我找到了一個解決方法:在彈出模態對話框之前調用floatingToolFrame.setAlwaysOnTop(false),並在關閉之後調用floatingToolFrame.setAlwaysOnTop(true)。然而,在任何時候打開模態對話框時都要求這個包裝看起來很荒謬。是的,我可以繼承JDialog的子類,並從所有子類派生所有對話框,以便在幕後執行此操作,但同樣,爲什麼這是必需的?
關於如何在沒有解決方法的情況下解決此問題的任何想法?我的創建永遠在上面的調色板的方法是完全錯誤的嗎? (還有一點需要注意:這似乎與此處所述的問題相反:Java 6, JFrame stuck alwaysontop)。
任何想法將不勝感激!