如题

解决方案 »

  1.   

    public static void centerWindow(Component component) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension scmSize = toolkit.getScreenSize();
        Dimension size = component.getPreferredSize();
        int width = component.WIDTH,
            height = component.HEIGHT;    component.setLocation(scmSize.width / 2 - (width / 2),
                              scmSize.height / 2 - (height / 2));
      }
      

  2.   

    Window 类:
    public void setLocationRelativeTo(Component c) {}使用可以简单的:win.setLocationRelativeTo(null)就行了
      

  3.   

    这个用JBuilder创建一个application时都会自动生成的。
      

  4.   

    public static void centerWindow(Component component) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension scmSize = toolkit.getScreenSize();
        Dimension size = component.getPreferredSize();
        int width = component.WIDTH,
            height = component.HEIGHT;    component.setLocation(scmSize.width / 2 - (width / 2),
                              scmSize.height / 2 - (height / 2));
      }
      

  5.   

    只要加这句代码就行了:
    f.setLocationRelativeTo(null);
      

  6.   

    public static void centerWindow(Component component) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension scmSize = toolkit.getScreenSize();
        Dimension size = component.getPreferredSize();
        int width = component.WIDTH,
            height = component.HEIGHT;    component.setLocation(scmSize.width / 2 - (width / 2),
                              scmSize.height / 2 - (height / 2));
      }
      

  7.   

    你用一下JBuilder就知道怎么做了。
      

  8.   

    只要加这句代码就行了:
    f.setLocationRelativeTo(null);
    不是吧?就是这样就可以啦?这么简单吗? setLocationRelateiveTo(null) 这在API中那里可以找到啊?
      

  9.   

    我刚才作了一个程序测试过了,我知道JB里面是像一楼的写法,但是f.setLocationRelativeTo(null);
    这样的写法和那种写法,在什么时候会发生不同的变化吗?那为什么这么简单的方法不运用而去用一楼的那种JB里面自动生成的方法呢?
      

  10.   

    import java.awt.*;
    import java.awt.event.*;public class test extends Frame
    {
      Frame f = new Frame("测试中间");
      Panel p = new Panel();
      
       test()
      {
         Button b1 = new Button("确定");
         p.add(b1);
         f.add(p);
         f.setSize(300,300);
         f.setLocationRelativeTo(null); //使得窗体能够显示在屏幕中间
         f.show();
      
     }
      public static void main(String args[])
     {
        new test();
     }
    }这个是我写的,也测试过了,能成功.
      

  11.   

    注意:
    方法setLocationRelativeTo(null);
    不是对所有的都适用,
    如对装有Image图象的ImagePanelPanel面板的窗口就不行 
    还是用一楼就可以