你自己写一个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);
这样就好了
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);
这样就好了
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;
}
}
你可以试一下!(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();
}
}你编译一下就知道了!
你的代码是由布局来自动控制组件大小的,我用的是null加指定大小,所以不会效果,再说也不可能真的让List老长一个.