现在程序中dialog都为模态对话框,他们的parent为一个Jframe1,当我打开着某一dialog时,弹出另一个jFrame2窗口(我的程序是一个聊天软件),这个窗口也被那个模态对话框阻塞,我不明白模态的parent是Jframe1,为什么还会阻塞jFrame2?应该怎么解决呢使jFrame2不受模态dialog影响呢? 谢谢大家

解决方案 »

  1.   

    这个在1.6之前的版本里面是没办法的,一个模态对话框肯定会阻塞所有同一个进程内的Frame,在1.6版本中加入了几种不同的阻塞模式,可以实现你的要求,例子:import java.awt.BorderLayout;
    import java.awt.Dialog.ModalityType;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;public class T {
    public static void main(String[] args)
    {
    final JFrame f = new JFrame();
    JButton btn = new JButton("Test");
    btn.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    JDialog dlg = new JDialog(f, "Test", ModalityType.DOCUMENT_MODAL);
    dlg.setSize(100, 100);
    dlg.setLocationRelativeTo(f);
    dlg.setVisible(true);
    }
    });
    f.getContentPane().add(btn, BorderLayout.CENTER);
    f.setSize(200, 200);
    f.setLocation(100, 100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true); final JFrame f1 = new JFrame();
    JButton btn1 = new JButton("Test");
    btn1.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    JDialog dlg = new JDialog(f1, "Test", ModalityType.DOCUMENT_MODAL);
    dlg.setSize(100, 100);
    dlg.setLocationRelativeTo(f1);
    dlg.setVisible(true);
    }
    });
    f1.getContentPane().add(btn1, BorderLayout.CENTER);
    f1.setSize(200, 200);
    f1.setLocation(400, 100);
    f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f1.setVisible(true);
    }
    }