问题解决了一半!
----------
if (dialog==null)
   dialog=new DialogPanel();
else if(!dialog.isVisible())
  {
   dialog.setVisible(true);
    }
else if (dialog.isVisible())
  {
dialog.setVisible(false);
}----------------------------------
可是还是不能定位到父面板上面,,,
调用 super()出错,why?

解决方案 »

  1.   

    首先在Dialog的构造函数中有一个Frame参数你没有填,就选用父面板作参数。第二用setLocation]来定位就可居中了。
      

  2.   

    我添了但报错呀,你这样说我很模糊呀,
    请帮我添一下,再setLocation()一下,谢谢!!!!
      

  3.   

    如果好用,最后别忘了结账。import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;class MySecondProgram extends JFrame {
        public MySecondProgram() {
            setTitle("mysecond");
            setSize(400,300);
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            
            Container contentPane=getContentPane();
            
            JPanel northpanel=new JPanel();
            
            JButton button=new JButton("out");
            
            button.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent event) {
                    Container c = dialog.getParent();
                Point p = c.getLocation();
                Dimension d = c.getSize();
                dialog.setLocation(p.x + (d.width - dialog.getWidth())/2, p.y + (d.height - dialog.getHeight())/2);
                dialog.show();
                    
                }
            });
            northpanel.add(button);
            
            contentPane.add(northpanel ,BorderLayout.NORTH);
        }
        
        class DialogPanel  extends JDialog implements ActionListener {
            public DialogPanel(Frame f) {
                super(f,true);
                setSize(200,150);
                
            }
            public void actionPerformed(ActionEvent event) {
                
            }
        }
        
        public static void main(String[] args)
        {
            JFrame f = new MySecondProgram();
            f.setSize(640, 480);
            f.show();
        }
        private     JButton button;
        private     DialogPanel   dialog = new DialogPanel(this);
    }