1 如何设定了JComboBox宽度2  如何让JComboBox下来出来的菜单大于本身的宽度!假设设定了JComboBox宽度,所以如果下拉选项的内容特别多,就显示不出来了,有没有办法让它都显示出来

解决方案 »

  1.   

    没做过UI的东西,把代码发出来看看,他自身应该有很多方法吧,这个setMaximumRowCount(int count) 是不是你想要的?
      

  2.   

    class DateUI extends MetalComboBoxUI {
        @Override
        protected ComboPopup createPopup() {
            BasicComboPopup bc = new BasicComboPopup(jcombobox);
            bc.setPopupSize(x, y); // 设置大小
            return new BasicComboPopup(jcombobox);
        }
    }
    jcombobox.setUI(new DateUI());
    代码不多,自己试试
      

  3.   

    设置大小OK了 那我的第二个问题呢?大家帮帮忙!!!!!!如何让JComboBox下来出来的菜单大于本身的宽度!假设设定了JComboBox宽度,所以如果下拉选项的内容特别多,就显示不出来了,有没有办法让它都显示出来
      

  4.   

    package april.test0414;import java.awt.Dimension;import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;public class TestJcombox extends JFrame{


    String str[]={"aa","bb","cc","dd","ee"};
    public TestJcombox() {
    JComboBox box=new JComboBox(str);

    box.setPreferredSize(new Dimension(10,20));//设置下拉框的高和宽
    box.setEditable(false);

    this.add(new JPanel().add(box));
    this.pack();
    this.setVisible(true);


    }


    public static void main(String[] args) {
    new TestJcombox();
    }
    }主要是这个方法setPreferredSize(new Dimension(int width,int heigh))
      

  5.   

    getComponent(1)就是下面LIST的组建,getComponent(0)就是上面的按钮有控件了,你干什么都应该不成问题
      

  6.   

    我也贴段代码。仿照html的。带Value的。可能不是很好。仅供参考
    import java.awt.Dimension;
    import java.util.ArrayList;
    import java.util.Vector;import javax.swing.ComboBoxModel;
    import javax.swing.JComboBox;public class WideComboBox extends JComboBox{    
        
        /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private ArrayList<String> arrvalue = new ArrayList<String>();
    public WideComboBox() {    
        }    
        
        public WideComboBox(final Object items[], final String[] values){    
            super(items);  
            for(int i=0; i < values.length; i++){
             this.arrvalue.add(values[i]);
            }
        }    
        
        public WideComboBox(Vector items) {    
            super(items);    
        }    
        
        public WideComboBox(ComboBoxModel aModel) {    
            super(aModel);         
        }  
        public void addItem(Object anObject,String value){
         super.addItem(anObject);
         this.arrvalue.add(value);
        }
        public String getValue(){
         int x = this.getSelectedIndex();
         return (String)arrvalue.get(x);
        }
        private boolean layingOut = false;    
        
        public void doLayout(){    
            try{    
                layingOut = true;    
                super.doLayout();    
            }finally{    
                layingOut = false;    
            }    
        }    
        
        public Dimension getSize(){    
            Dimension dim = super.getSize();    
            if(!layingOut)    
                dim.width = Math.max(dim.width, getPreferredSize().width);    
            return dim;    
        }    
      

  7.   

    setPreferredSize(new Dimension(int width,int heigh))这个只是设置Jcombox 的大小我是想设置 它的下拉框里面的值的显示大小
      

  8.   

        class   MyComboBoxUI   extends   WindowsComboBoxUI   {
            protected   ComboPopup   createPopup()   {
                return   new   MyComboBoxPopup(comboBox);
            }
        }    class   MyComboBoxPopup   extends   BasicComboPopup   {
            public   MyComboBoxPopup(JComboBox   combo)   {
                super(combo);
            }
            protected   JScrollPane   createScroller()   {
                JScrollPane   sp   =   new   JScrollPane(   list,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED   );
                return   sp;
            }        public   void   show()   {
                setListSelection(comboBox.getSelectedIndex());
                Insets   insets   =   getInsets();
                int   popupPrefWid   =   list.getPreferredSize().width   +   insets.left   +   insets.right;            Dimension   scrollSize   =   new   Dimension(
                comboBox.getWidth(),   getPopupHeightForRowCount(   comboBox.getMaximumRowCount()));
                if   (popupPrefWid   >   scrollSize.width)   {
                    scrollSize.height   +=   scroller.getHorizontalScrollBar().getPreferredSize().height;
                }
                scroller.setMaximumSize(   scrollSize   );
                scroller.setPreferredSize(   scrollSize   );
                scroller.setMinimumSize(   scrollSize   );
                list.revalidate();
                show(   comboBox,   0,   comboBox.getHeight());
            }        private   void   setListSelection(int   selectedIndex)   {
                if   (   selectedIndex   ==   -1   )   {
                    list.clearSelection();
                }else{
                    list.setSelectedIndex(selectedIndex);
                    list.ensureIndexIsVisible(selectedIndex);
                }
            }
        }
    用的时候
    jComboBox1.setUI(new   MyComboBoxUI());