你自己写一个Renderer而后设置 Renderer 的大小就好了如: class MyCellRenderer extends JLabel implements ListCellRenderer {
     public MyCellRenderer() {
         setOpaque(true);
     }
     public Component getListCellRendererComponent(
         JList list,
         Object value,
         int index,
         boolean isSelected,
         boolean cellHasFocus)
     {
         setText(value.toString());
         setBackground(isSelected ? Color.red : Color.white);
         setForeground(isSelected ? Color.white : Color.black);
         return this;
     }
 }
////// 
JComboBox patternList = new JComboBox(patternExamples);
MyCellRenderer comboxRenderer = new MyCellRenderer();
comboxRenderer.setPreferredSize(new Dimension(600, 300));
patternList.setRenderer(comboxRenderer);
这样就好了

解决方案 »

  1.   

    JListCombo?怎么没见过这个东西?
      

  2.   

    JListCombo就是类似网页里的下拉选单一样的东西可是comboxRenderer.setPreferredSize(new Dimension(600, 300));这个方法只对高度有效,我是想让它的宽度调大些,有办法吗?
      

  3.   

    哦,是我打错了,其实就是JComboBox
      

  4.   

    你是想让combox弹出来选择数据的窗口的大小和combox在界面上显示的不一样大?
      

  5.   

    UI ui = new UI();
            jComboBox1.setUI(ui);
            ((Popup)ui.getPopup()).setDisplaySize(100, 50);
    **************
        class UI extends javax.swing.plaf.basic.BasicComboBoxUI {
            protected javax.swing.plaf.basic.ComboPopup createPopup() {
                Popup popup = new Popup( comboBox );
                popup.getAccessibleContext().setAccessibleParent(comboBox);
                return popup;
            }
            public javax.swing.plaf.basic.ComboPopup getPopup() {
                return popup;
            }
        }    class Popup extends javax.swing.plaf.basic.BasicComboPopup {
            public Popup(JComboBox combo) {
                super(combo);
            }
            public void setDisplaySize(int width, int height) {
                scroller.setSize(width, height);
                scroller.setPreferredSize(new Dimension(width, height));
            }
            public void show() {
                setListSelection(comboBox.getSelectedIndex());
                Point location = getPopupLocation();
                show( comboBox, location.x, location.y );
            }
            private void setListSelection(int selectedIndex) {
                if ( selectedIndex == -1 ) {
                    list.clearSelection();
                }
                else {
                    list.setSelectedIndex( selectedIndex );
                    list.ensureIndexIsVisible( selectedIndex );
                }
            }        private Point getPopupLocation() {
                Dimension popupSize = comboBox.getSize();
                Insets insets = getInsets();            // reduce the width of the scrollpane by the insets so that the popup
                // is the same width as the combo box.
                popupSize.setSize(popupSize.width - (insets.right + insets.left),
                                  getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
                Rectangle popupBounds = computePopupBounds( 0, comboBox.getBounds().height,
                    popupSize.width, popupSize.height);
                Dimension scrollSize = popupBounds.getSize();
                Point popupLocation = popupBounds.getLocation();//            scroller.setMaximumSize( scrollSize );
    //            scroller.setPreferredSize( scrollSize );
    //            scroller.setMinimumSize( scrollSize );            list.revalidate();            return popupLocation;
            }
        }
      

  6.   

    JComboBox的宽度好像是和嵌入到里面的字符串的长度有关系吧!——以最长的字符串为参照
    你可以试一下!(CM.java)import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class CM extends JFrame
    {
    String []name={"张三","李四","王五","赵六"};
    String []name2={"张三","李四","王五","铁木尔达达买其"};
    JComboBox id1,id2;
    JPanel panel;
    CM()
    {
    id1=new JComboBox(name);
    id2=new JComboBox(name2);
    panel=new JPanel();
    getContentPane().add(panel);

    panel.add(id1);
    panel.add(id2);
    setSize(300,200);
    setVisible(true);
    }
    public static void main(String args[])
    {
    new CM();
    }
    }你编译一下就知道了!
      

  7.   

    谢谢Acylas(Acylas) 的代码.to: VincentSailing(文森特)
    你的代码是由布局来自动控制组件大小的,我用的是null加指定大小,所以不会效果,再说也不可能真的让List老长一个.