怎么设置我JFrmae在屏幕上显示的位置,我的本意是要程序启动以后弹出的JFream显示在屏蔽的中央,但是它却显示在屏幕的左上角,我应该怎么设置,谢谢

解决方案 »

  1.   

    setBounds(x1,y1,x2,y2);
    前两个参数是位置
    后两个是窗口大小
      

  2.   

    /**
         * centerlize the component,that is locate the component in the
         * center of the screen
         * @param c     the component to be centerlized
         */
        public static void center(Component c) {
            center(c, c.getSize());
        }    
        /**
         * centerlize the component,whose size is dimension me
         * @param c     the component to centerlized
         * @param me    dimension the component to be shown
         */
        public static void center(Component c, Dimension me) {
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            c.setLocation(screenSize.width / 2 - me.width / 2, screenSize.height
                    / 2 - me.height / 2);
        }
        
        
        /**
         * make the component be in center of the parent component,
         * if the component exceeds the borders of the screen,then
         * make it in the screen
         * @param parent        the parent component
         * @param me            the component to be centerlized
         */
        public static void center(Component parent,Component me){
            Point p = parent.getLocation();
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            Dimension psize = parent.getSize();
            Dimension msize = me.getSize();
            Point m = new Point((int)(p.getX()+(psize.width-msize.width)/2),
                                (int)(p.getY()+(psize.height-msize.height)/2));
            if(m.x+msize.width > screen.width ){
                m.x = screen.width-msize.width;
            }
            
            else if(m.x<0){
                m.x=0;
            }
            if(m.y+msize.height > screen.height){
                m.y = screen.height - msize.height;
            }
            else if(m.y<0){
                m.y = 0;
            }
            me.setLocation(m.x,m.y);
        }