几件事:
>组合框的外观(显示区域,箭头,下拉)取决于LAF.你的屏幕截图建议使用WinXP.如果你必须支持任何其他LAF,一定要测试它,因为对一个LAF起作用的可能不适用于另一个.我发现对于JComboBoxes来说尤其如此.
>像Twister建议的那样,通过覆盖paint()方法来改变颜色可能不是最好的方法.只需设置组合框本身的背景/前景色.如果要更改下拉列表本身的颜色(我不清楚是否要执行此操作),然后添加一个自定义渲染器来覆盖getListCellRendererComponent以设置背景/前景.
public static class CustomRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected,
cellHasFocus);
setBackground(Color.WHITE);
setForeground(Color.BLACK);
return this;
}
}
>灰色三角形和边框的外观是因为组合框现在具有焦点.你可以让它不可聚焦,着色就会消失.但是,这可能不是您想要的行为.
JComboBox combo = new JComboBox(new Object[]{"Dog", "Cat", "Bird"});
combo.setBackground(Color.WHITE);
combo.setForeground(Color.BLACK);
combo.setFocusable(false);