Remember window positions in swing

前端 未结 5 1566
别那么骄傲
别那么骄傲 2021-01-11 23:22

I have a pretty big swing application and i want to make it remember sizes of all windows, jframes etc. So if user resized window how he likes, next time the window looks ex

5条回答
  •  猫巷女王i
    2021-01-11 23:43

    Here's a start. The following code will find the top-most container and save the bounds of all child components to a preferences file which can then be used to restore. This probably won't handle all situations but it works for my app. Future changes can be tracked here.

    public class WindowBoundsRestorer
    {
        private final String filename;
        private Properties properties;
    
        public WindowBoundsRestorer( String filename )
        {
            this.filename = filename;
        }
    
        private void setBounds( String key, Component c )
        {
            key = key + c.getName();
    
            String position = properties.getProperty( key );
            if ( c.getName() != null && ! StringUtils.isBlank( position ) )
            {
                String[] nums = position.split( "," );
                c.setBounds( Integer.parseInt( nums[0] ), Integer.parseInt( nums[1] ),
                             Integer.parseInt( nums[2] ), Integer.parseInt( nums[3] ) );
            }
    
            if ( c instanceof Container )
            {
                key = key + "/";
                Container container = (Container) c;
                for ( Component child : container.getComponents() )
                   setBounds( key, child );
            }
        }
    
        /**
         * Loads the properties from the .xml file and sets all named windows with a matching
         * name.
         *
         * @param component Any component in the Swing app.  The top-most container will be
         * determined from this component.
         */
        public void restore( Component component )
        {
            properties = new Properties();
            InputStream is = null;
            try
            {
                is = new FileInputStream( filename );
                properties.loadFromXML( is );
            }
            catch ( IOException e )
            {
                e.printStackTrace();
                return;
            }
            finally
            {
                IOUtils.closeQuietly( is );
            }
    
            Component top = component;
            while ( top.getParent() != null )
                top = top.getParent();
    
            setBounds( "", top );
        }
    
        private void getBounds( String key, Component c )
        {
            key = key + c.getName();
            String position = String.format( "%d,%d,%d,%d", c.getX(), c.getY(), c.getWidth(), c.getHeight() );
            properties.setProperty( key, position );
            if ( c instanceof Container )
            {
                key = key + "/";
                Container container = (Container) c;
                for ( Component child : container.getComponents() )
                    getBounds( key, child );
            }
        }
    
        public void save( Component component )
        {
            Component top = component;
            while ( top.getParent() != null )
                top = top.getParent();
    
            properties = new Properties();
            getBounds( "", top );
    
            OutputStream os = null;
            try
            {
                os = new FileOutputStream( filename );
                properties.storeToXML( os, "Browser" );
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }
            finally
            {
                IOUtils.closeQuietly( os );
            }
        }
    }
    

提交回复
热议问题