Leo*_*eon 5 java applet swing modal-dialog jdialog
我有一个 Swing 应用程序,基本上是一个可以弹出模态对话框的主框架。当显示模态对话框时,如果我切换到另一个窗口,如 firefox。然后切换回摆动应用程序。在JDialog前面是没有任何更多。
我不想将对话框 AlwaysOnTop 设置为 true。因为那样对话框将在所有窗口之上,包括其他进程中的窗口。
那么我应该怎么做,以便当我切换回来时,模态对话框仍然在顶部?
BTW:它是一个Applet,所以主框架实际上是这样设置的:
private static Frame findParentFrame(Container owner){
Container c = owner;
while(c != null){
if (c instanceof Frame)
return (Frame)c;
c = c.getParent();
}
return (Frame)null;
}
Run Code Online (Sandbox Code Playgroud)
我不确定对话的方式是否是这里的关键问题。我已经测试了这种行为,当应用程序最大化时,无论它是否处于模式状态,对话框总是会在前面弹出。
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
public class AppletTest extends JApplet
implements ActionListener
{
private static final long serialVersionUID = 1L;
private Frame findParentFrame()
{
Container c = this;
while(c != null)
{
if(c instanceof Frame)
return (Frame) c;
c = c.getParent();
}
return (Frame) null;
}
private void createGUI()
{
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
JDialog d = new JDialog(findParentFrame());
d.setModal(true);
d.setVisible(true);
}
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
}catch(Exception e)
{
System.err.println("createGUI didn't successfully complete");
}
}
@Override
public void actionPerformed(ActionEvent e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
查看我提供的示例。您可以注释该行d.setModal(true);,结果将完全相同。我建议您再次检查您的代码或将其显示给我们,因为您可能错过了其中的某些内容。
PS:我在网上发现了一些其他类似黑客的解决方案http://www.codeguru.com/forum/showthread.php?t=41536 不过,我仍然会专注于检查你的代码。
喂,祝你好运,波罗。