如何让模态对话框不阻塞我的第二个顶级窗口。

5
我有一个主框架和一个次框架,还有一个以主框架为父级的模态对话框。
但现在两个框架都被模态对话框阻止了。如何让次框架在主框架有模态对话框时仍然可访问?
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 );
            }
        } );
    }

}
1个回答

8

你需要将模态字段设置为文档。

JDialog d = new JDialog( Example.this, "Dialog" ,Dialog.ModalityType.DOCUMENT_MODAL); 

2
+1,另请参阅AWT模态 - aterai

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接