FrmConvert_AboutBox dlg = new FrmConvert_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.show();

解决方案 »

  1.   

    这段应该加在哪儿?我的代码是从书上改过来的,用了Jbuilder的XYLayout类import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;public class OKDialog extends Dialog implements ActionListener
    {
    public static int Confirm;
    private XYLayout xYLayout1 = new XYLayout();
    private JButton okButton = new JButton("Ok");
    private JButton canButton = new JButton("Cancel");
    //private JLabel jLabel1 = new JLabel();
    protected static Frame createdFrame;
    public OKDialog(Frame parent, String message)
    {
    super(parent,true);
    Label messageLabel = new Label(message);
    //XYLayout xYLayout1 = new XYLayout();
    setLayout(xYLayout1);

    add(messageLabel,new XYConstraints(20, 26, 224, 21));
    canButton.addActionListener(this);
    add(canButton, new XYConstraints(121, 69, 82, 20));
    okButton.addActionListener(this);
    add(okButton, new XYConstraints(33, 69, 70, 20));
    }
    public void actionPerformed(ActionEvent event)
    {
    Object buttTmp=event.getSource();
    if (buttTmp == okButton)
    {
    hide();
    if (createdFrame != null)
    {
    createdFrame.hide();
    }
    Confirm=1;
    }
    if (buttTmp == canButton)
    {
    hide();
    if (createdFrame != null)
    {
    createdFrame.hide();
    }
    Confirm=0;
    }
    }
    public static void createOKDialog(String dialogString)
    {
    if (createdFrame == null)
    {
    createdFrame = new Frame("Dialog");
    }
    OKDialog okDialog = new OKDialog(createdFrame, dialogString);
    //createdFrame.setSize(270,300);
    createdFrame.setSize(okDialog.getSize().width,okDialog.getSize().height);
    okDialog.show();
    }
    }
      

  2.   

    import java.awt.*;
      
    class testd extends Frame
    {
      Dialog dialog;
     
     testd(String str)
     {
       super(str);
       dialog=new Dialog(this,"mydialog");
       dialog.setSize(100,100);   int x=(int)((1024-dialog.getWidth())/2);
       int y=(int)((768-dialog.getHeight())/2);   dialog.setLocation(x,y);
       dialog.show();
      } public static void main(String [] args)
     {
      testd dobj=new testd("myFrame");
      dobj.setSize(1024,768); //根据你的屏幕的分辨率定,如800*600
      dobj.setVisible(true);
     }}
      

  3.   

    在你的扩展Dailog的class的init()方法下任何地方调用均可。
    CenterShowDialog csd = new CenterShowDialog(this);
    import java.awt.*;
    /**
     * 描述:进入参数Dialog,使显示的图形界面显示居中 
     **/
    public class CenterShowDialog {
      public CenterShowDialog(Dialog dialog) {//目标显示为对话框
        //Center the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension dialogSize = dialog.getSize();
        if (dialogSize.height > screenSize.height) {
          dialogSize.height = screenSize.height;
        }
        if (dialogSize.width > screenSize.width) {
          dialogSize.width = screenSize.width;
        }
        dialog.setLocation((screenSize.width - dialogSize.width) / 2,
          (screenSize.height - dialogSize.height) / 2);
      }
    }
    //end
      

  4.   


    dig.setbounds((800-dig.getX()),(600-dig.getY()),dig.getX(),dig.getY())---其中
    dig.getX() 为dialog的宽