让窗口居中的一个static方法

    技术2022-05-11  75

    这里的component可以是你的那个Frame

    public   static   void   centerWindow(Component   component)      {        Dimension   screenSize   =   Toolkit.getDefaultToolkit().getScreenSize();        Dimension   componentSize   =   component.getSize();        if   (componentSize.height   >   screenSize.height)     {            componentSize.height   =   screenSize.height;        }        if   (componentSize.width   >   screenSize.width)     {            componentSize.width   =   screenSize.width;        }        component.setLocation((screenSize.width-componentSize.width)/2,   (screenSize.height-componentSize.height)   /   2);        component.setVisible(true);    } 


    最新回复(0)