给你一段我用过的代码:private void _initialize() {
//居中显示
java.awt.Dimension d = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation(
(d.width - this.getSize().width) / 2, 
(d.height - this.getSize().height) / 2 - 40); 
//......
}看明白我的意思,相应修改九行了,good luck!

解决方案 »

  1.   

    我的代码遇到一些问题,如下:
    void jBtnShowMap_actionPerformed(ActionEvent e) {
        NewFrame theFrame = new NewFrame();
        Dimension dlgSize = theMapFrame.getPreferredSize();
        javax.swing.JOptionPane.showMessageDialog(null,String.valueOf(dlgSize.getHeight())); //这里的dlgSize.getHeight()的值远比设计状态时显示的要小事,这是怎么回事?
        theFrame.setSize(dlgSize);
        theFrame.show();
      }
      

  2.   

    new theFrame 后 theFrame .pack();
    这是因为theFrame才创建,还没有计算过自己和组件的大小
      

  3.   

    public void setLogUISize(){
    Toolkit theKit=this.getToolkit();
    Dimension wndSize=theKit.getScreenSize();
    int width=(int)wndSize.getWidth();
    int height=(int)wndSize.getHeight();
    Insets inset=theKit.getScreenInsets(gc);
    this.setBounds(inset.left+width/2-WIDTH,inset.top+height/2-HEIGHT/2,
    2*WIDTH,HEIGHT);
    }//end of setLogUISizeprivate static int WIDTH=200;
    private static int HEIGHT=200;根据此法自己调整一下你的程序即可!
      

  4.   

    dlgSize = theMapFrame.getPreferredSize
    这里是PreferredSize,不是实际的size,你之前没有设置它,结果是0
      

  5.   

    dlgSize = theMapFrame.getPreferredSize
    这里是PreferredSize,不是实际的size,你之前没有设置它,结果是0