请问怎么实现模态窗口?给出代码

解决方案 »

  1.   

    JDialog(Dialog owner, String title, boolean modal) 
    创建一个具有指定标题和指定所有者对话框的有模式或无模式对话框。
      

  2.   

    你如果想做个模态窗口里面还有内容,就继承JDialog,然后往里面加个JPanel,在上面绘制你要的即可。
      

  3.   

    给个符合你要求的。不过是JFrame
    ---------------------------------------------------------
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;public class MyJFrame extends JFrame{        JFrame MyOwner ;    public MyJFrame(JFrame owner,String title) {
            super(title) ;
            this.MyOwner=owner; 
            
            this.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    MyOwner.setEnabled(true);
                }           
            });
        }    
        public void show(){
            super.show(); 
            this.MyOwner.setEnabled(false);              
        }        private static void createAndShowGUI(){
            JFrame frame = new
            JFrame("我是父frame");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setBounds(50 , 50 , 480 , 320);
            frame.setVisible(true);
            
            MyJFrame mf = new MyJFrame(frame,"我是模态frame");
            mf.setBounds(50 , 50 , 240 , 180);
            mf.show();        
        }    public static void main(String args[]) {        javax.swing.SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    createAndShowGUI();
                }            
            });
        }
    }