Java 如何让模态对话框不阻塞第二个顶层框架

Java 如何让模态对话框不阻塞第二个顶层框架,java,swing,jframe,modal-dialog,Java,Swing,Jframe,Modal Dialog,我有一个主框架和第二个框架,还有一个主框架作为父框架的模态对话框 但是现在两个帧都被模态对话框阻止了 当主框架具有模态对话框时,如何让第二个框架可访问 public class Example extends JFrame { public Example() { super("MainFrame"); JButton btn1 = new JButton( new AbstractAction( "Frame" ) { @Ov

我有一个主框架和第二个框架,还有一个主框架作为父框架的模态对话框

但是现在两个帧都被模态对话框阻止了
当主框架具有模态对话框时,如何让第二个框架可访问

public class Example extends JFrame {

    public Example() {
        super("MainFrame");

        JButton btn1 = new JButton( new AbstractAction( "Frame" ) {
            @Override
            public void actionPerformed( ActionEvent e ) {
                EventQueue.invokeLater( new Runnable() {
                    @Override
                    public void run() {
                        JFrame f = new JFrame( "Frame" );
                        f.getContentPane().add( new JLabel("This shoud be not blocked by ModalDialog.") );
                        f.setLocation( 50, 200 );
                        f.setSize( 300, 200 );
                        f.setVisible( true );
                    }
                } );
            }
        } );

        JButton btn2 = new JButton( new AbstractAction( "Modal" ) {
            @Override
            public void actionPerformed( ActionEvent e ) {
                EventQueue.invokeLater( new Runnable() {
                    @Override
                    public void run() {
                        JDialog d = new JDialog( Example.this, "Dialog" );
                        d.getContentPane().add( new JLabel("This shoud block only MainFrame.") );
                        d.setModal( true );
                        d.setLocation( 50, 100 );
                        d.setSize( 300, 200 );
                        d.setVisible( true );
                    }
                } );
            }
        } );

        setDefaultCloseOperation( EXIT_ON_CLOSE );
        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add( btn1, BorderLayout.NORTH );
        getContentPane().add( new JLabel("MainFrame"), BorderLayout.CENTER );
        getContentPane().add( btn2, BorderLayout.SOUTH);
        setLocation( 50, 50 );
        setSize( 200, 150 );

        btn1.doClick();
        btn2.doClick();
    }

    public static void main( String[] args ) {
        EventQueue.invokeLater( new Runnable() {
            @Override
            public void run() {
                new Example().setVisible( true );
            }
        } );
    }

}

您需要将“模态”字段设置为“文档”

JDialog d = new JDialog( Example.this, "Dialog" ,Dialog.ModalityType.DOCUMENT_MODAL); 
+1,另见