java awt swing控件,下拉内容,自适应宽度

效果图
在这里插入图片描述
1.使用继成复写父类方法的方式进行下拉宽度修改DefaultComboBox 继承JComboBox

 class DefaultComboBox extends JComboBox {
    
    
  public DefaultComboBox(ComboBoxModel var2) {
    
    
            super(var2);
  }
 }

2.修改下拉画布宽度

 		 /**
         * Small hack to get pop up menu size bigger enough to show items even though
         * the combo box size could be smaller
         * */
        private boolean layingOut = false;
        @Override
        public void doLayout(){
    
    
            try{
    
    
                layingOut = true;
                super.doLayout();
            }finally{
    
    
                layingOut = false;
            }
        }
        @Override
        public Dimension getSize(){
    
    
            Dimension dim = super.getSize();
            if ( !layingOut ) {
    
    
                dim.width = Math.max(dim.width, computeMaxWith());
            }
            return dim;
        }

3.计算下拉框内容宽度,并获取最大宽度

 public Integer computeMaxWith(){
    
    
            Font font = this.getFont();
            int maxWith = 0;
            for (int i = 0; i < this.getModel().getSize(); i++) {
    
    
                Object elementAt = this.getModel().getElementAt(i);
                Integer width = getWidth(elementAt.toString(), font);
                maxWith = Math.max(maxWith,width);
            }
            return maxWith+5;
        }
        public Integer getWidth(String text,Font font){
    
    
            FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
            int width = SwingUtilities.computeStringWidth(fm, text);
            return width;
        }

4.完整代码

 class DefaultComboBox extends JComboBox {
    
    
  public DefaultComboBox(ComboBoxModel var2) {
    
    
            super(var2);
  }
   		/**
         * Small hack to get pop up menu size bigger enough to show items even though
         * the combo box size could be smaller
         * */
        private boolean layingOut = false;
        @Override
        public void doLayout(){
    
    
            try{
    
    
                layingOut = true;
                super.doLayout();
            }finally{
    
    
                layingOut = false;
            }
        }
        @Override
        public Dimension getSize(){
    
    
            Dimension dim = super.getSize();
            if ( !layingOut ) {
    
    
                dim.width = Math.max(dim.width, computeMaxWith());
            }
            return dim;
        }
        public Integer computeMaxWith(){
    
    
            Font font = this.getFont();
            int maxWith = 0;
            for (int i = 0; i < this.getModel().getSize(); i++) {
    
    
                Object elementAt = this.getModel().getElementAt(i);
                Integer width = getWidth(elementAt.toString(), font);
                maxWith = Math.max(maxWith,width);
            }
            return maxWith+5;
        }
        public Integer getWidth(String text,Font font){
    
    
            FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(font);
            int width = SwingUtilities.computeStringWidth(fm, text);
            return width;
        }
 }

猜你喜欢

转载自blog.csdn.net/oXiaoWeiWuDi/article/details/127459949