有没有办法用一个函数就可以把窗口里的按扭的大小都设为相同?
谢谢!

解决方案 »

  1.   

    /**
         * 统一组件大小,以最大的组件大小为标准
         * @param components 需要统一大小的组件
         */
        public void equalizeComponentSizes( JComponent[] components )
        {
            if ( components.length == 0 || components == null )
            {
                return;
            }
            // Get the largest width and height
            int i = 0;
            Dimension maxPreferred = new Dimension ( 0 , 0 );
            JComponent oneComponent = null;
            Dimension thisPreferred = null;
            for ( i = 0; i < components.length; ++i )
            {
                oneComponent = ( JComponent ) components[ i ];
                thisPreferred = oneComponent.getPreferredSize ();
                maxPreferred.width =
                    Math.max ( maxPreferred.width , ( int ) thisPreferred.getWidth () );
                maxPreferred.height =
                    Math.max ( maxPreferred.height ,
                               ( int ) thisPreferred.getHeight () );
            }        // resize all the conponents with the same size
            for ( i = 0; i < components.length; ++i )
            {
                oneComponent = ( JComponent ) components[ i ];
                oneComponent.setPreferredSize ( ( Dimension ) maxPreferred.clone () );
                oneComponent.setMinimumSize ( ( Dimension ) maxPreferred.clone () );
            }
        }
      

  2.   

    做工具类,
    CommandHelper 一类的东西,然后通过CommandHelper.create()方法来生成按钮/
    这样任何地方的按钮都会统一风格